KeyDown event not firing for Return key in textbox


I

ink

Hi all,

i am trying to trap the Return key in the KeyDown event, but all my tests
seem to show that the key down event does not fire.

I tested using a simple form with 1 textbox and 3 check boxes. And then
calling the API method keybd_event.

can someone explain why it is not firing.

i have tried it with the AcceptsReturn=true also.

Thanks,
ink



Test Code:

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (Down.Checked) {
MessageBox.Show("KeyDown: KeyCode=" + e.KeyCode + "
KeyValue=" + e.KeyValue);
}
}

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (Press.Checked)
{
MessageBox.Show("KeyPress: KeyChar=" + e.KeyChar);
}
}

private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
if (up.Checked)
{
MessageBox.Show("KeyUp: KeyCode=" + e.KeyCode + " KeyValue="
+ e.KeyValue);
}
}


VK_RETURN = 0x0D , // ENTER key

[DllImport("coredll.dll", EntryPoint = "keybd_event", SetLastError = true)]
internal static extern void keybd_event(byte bVk, byte bScan, int dwFlags,
int dwExtraInfo);


public void PressKey(VirtualKeyCodes keyCode)
{
const int KEYEVENTF_EXTENDEDKEY = 0x1;
const int KEYEVENTF_KEYUP = 0x2;

byte KeyCode = (byte)keyCode;

keybd_event(KeyCode, 0x45, KEYEVENTF_EXTENDEDKEY, 0);
keybd_event(KeyCode, 0x45, KEYEVENTF_EXTENDEDKEY |
KEYEVENTF_KEYUP, 0);
}
 
Ad

Advertisements

J

Jerod Houghtelling

Hi all,

i am trying to trap the Return key in the KeyDown event, but all my tests
seem to show that the key down event does not fire.

I tested using a simple form with 1 textbox and 3 check boxes. And then
calling the API method keybd_event.

can someone explain why it is not firing.

i have tried it with the AcceptsReturn=true also.

Thanks,
ink

Test Code:

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (Down.Checked) {
MessageBox.Show("KeyDown: KeyCode=" + e.KeyCode + "
KeyValue=" + e.KeyValue);
}
}

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (Press.Checked)
{
MessageBox.Show("KeyPress: KeyChar=" + e.KeyChar);
}
}

private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
if (up.Checked)
{
MessageBox.Show("KeyUp: KeyCode=" + e.KeyCode + " KeyValue="
+ e.KeyValue);
}
}

VK_RETURN = 0x0D , // ENTER key

[DllImport("coredll.dll", EntryPoint = "keybd_event", SetLastError = true)]
internal static extern void keybd_event(byte bVk, byte bScan, int dwFlags,
int dwExtraInfo);

public void PressKey(VirtualKeyCodes keyCode)
{
const int KEYEVENTF_EXTENDEDKEY = 0x1;
const int KEYEVENTF_KEYUP = 0x2;

byte KeyCode = (byte)keyCode;

keybd_event(KeyCode, 0x45, KEYEVENTF_EXTENDEDKEY, 0);
keybd_event(KeyCode, 0x45, KEYEVENTF_EXTENDEDKEY |
KEYEVENTF_KEYUP, 0);
}

Is your form's KeyPreview set to true?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top