About Event Handlig - KeyDown
-
Hello, I have written a method to process shortcut entries in my main form that simply dispatches messages to related methods for the button presses. But my problem is that when Key.Left or Key.Right is pressed for the "first" time, KeyDown event is not triggered for my following method: KeyPreview property of main form is set to true,
private void shortcutKeyDown(object sender, KeyEventArgs e)
{if (!e.Control && !e.Alt && !e.Shift) // I want to process if Ctrl Alt or Shift Keys are not pressed.
{
switch (e.KeyCode)// Examine KeyCode
{
case (Keys.Left):
{
CallRelatedMethod(this, e);
break;
}
case (Keys.Right):
{
CallRelatedMethod(this, e);
break;
}
case (Keys.E):
{
CallRelatedMethod(this, e);
break;
}
case (Keys.W):
{
CallRelatedMethod(this, e);
break;
}
case (Keys.P):
{
CallRelatedMethod(this, e);
break;
}
default:
break;
} // switch (e.KeyValue)
} //!e.Control || !e.Alt || !e.Shift} // shortcutKeyDown
Test Notes: Other keys "W, E, P" triggers the Keydown event without any problems. When Left or Right key is pressed for the second time Keydown event is triggered. Could not find similar problems in google or here so probably I am missing a simple point but I cannot see. Any help is appreciated. Thank you,
-
Hello, I have written a method to process shortcut entries in my main form that simply dispatches messages to related methods for the button presses. But my problem is that when Key.Left or Key.Right is pressed for the "first" time, KeyDown event is not triggered for my following method: KeyPreview property of main form is set to true,
private void shortcutKeyDown(object sender, KeyEventArgs e)
{if (!e.Control && !e.Alt && !e.Shift) // I want to process if Ctrl Alt or Shift Keys are not pressed.
{
switch (e.KeyCode)// Examine KeyCode
{
case (Keys.Left):
{
CallRelatedMethod(this, e);
break;
}
case (Keys.Right):
{
CallRelatedMethod(this, e);
break;
}
case (Keys.E):
{
CallRelatedMethod(this, e);
break;
}
case (Keys.W):
{
CallRelatedMethod(this, e);
break;
}
case (Keys.P):
{
CallRelatedMethod(this, e);
break;
}
default:
break;
} // switch (e.KeyValue)
} //!e.Control || !e.Alt || !e.Shift} // shortcutKeyDown
Test Notes: Other keys "W, E, P" triggers the Keydown event without any problems. When Left or Right key is pressed for the second time Keydown event is triggered. Could not find similar problems in google or here so probably I am missing a simple point but I cannot see. Any help is appreciated. Thank you,
-
Hello, I have written a method to process shortcut entries in my main form that simply dispatches messages to related methods for the button presses. But my problem is that when Key.Left or Key.Right is pressed for the "first" time, KeyDown event is not triggered for my following method: KeyPreview property of main form is set to true,
private void shortcutKeyDown(object sender, KeyEventArgs e)
{if (!e.Control && !e.Alt && !e.Shift) // I want to process if Ctrl Alt or Shift Keys are not pressed.
{
switch (e.KeyCode)// Examine KeyCode
{
case (Keys.Left):
{
CallRelatedMethod(this, e);
break;
}
case (Keys.Right):
{
CallRelatedMethod(this, e);
break;
}
case (Keys.E):
{
CallRelatedMethod(this, e);
break;
}
case (Keys.W):
{
CallRelatedMethod(this, e);
break;
}
case (Keys.P):
{
CallRelatedMethod(this, e);
break;
}
default:
break;
} // switch (e.KeyValue)
} //!e.Control || !e.Alt || !e.Shift} // shortcutKeyDown
Test Notes: Other keys "W, E, P" triggers the Keydown event without any problems. When Left or Right key is pressed for the second time Keydown event is triggered. Could not find similar problems in google or here so probably I am missing a simple point but I cannot see. Any help is appreciated. Thank you,
i modify your code to :
if (e.Control || e.Alt || e.Shift) return;
switch (e.KeyCode)// Examine KeyCode
{
case (Keys.Left):
{
CallRelatedMethod(this, e);
break;
}
case (Keys.Right):
{
CallRelatedMethod(this, e);
break;
}
case (Keys.E):
{
CallRelatedMethod(this, e);
break;
}
case (Keys.W):
{
CallRelatedMethod(this, e);
break;
}
case (Keys.P):
{
CallRelatedMethod(this, e);
break;
}
default:
break;
} // switch (e.KeyValue)hope it help
dhaim program is hobby that make some money as side effect :)
-
i modify your code to :
if (e.Control || e.Alt || e.Shift) return;
switch (e.KeyCode)// Examine KeyCode
{
case (Keys.Left):
{
CallRelatedMethod(this, e);
break;
}
case (Keys.Right):
{
CallRelatedMethod(this, e);
break;
}
case (Keys.E):
{
CallRelatedMethod(this, e);
break;
}
case (Keys.W):
{
CallRelatedMethod(this, e);
break;
}
case (Keys.P):
{
CallRelatedMethod(this, e);
break;
}
default:
break;
} // switch (e.KeyValue)hope it help
dhaim program is hobby that make some money as side effect :)
Thank you for your time guys, With your approach to the method here are some more details: - I have assigned this method to every control that can have focus. (I have only a toolbar and a form thats all.) (Subscribed to form, toolbar keyDown events.) - dhaim, thank you for your modification but unfortunately I dont have a problem as the code does not run in the if() statement. When I debug the code and insert a break to the beginning of the method, I saw that the Keydown event is not triggered when I first press left or right button. Which means the method is never executed although a keydown event occurs. Thank you again, for your precious time.
-
Thank you for your time guys, With your approach to the method here are some more details: - I have assigned this method to every control that can have focus. (I have only a toolbar and a form thats all.) (Subscribed to form, toolbar keyDown events.) - dhaim, thank you for your modification but unfortunately I dont have a problem as the code does not run in the if() statement. When I debug the code and insert a break to the beginning of the method, I saw that the Keydown event is not triggered when I first press left or right button. Which means the method is never executed although a keydown event occurs. Thank you again, for your precious time.
-
some controls takes the control when you focused. you must lostfocus before catch the keypress. like webBrowser, datagridview. You must override theirs events..
-
I have tested your suggestion and I am pretty sure that main form has the focus when the program is first executed and left/right arrow is pressed as a shortcut. Thank you,
yes sir, I found your problem :) use FormKeyUp event. It works for all. also here is a simple sample.
private void frmMain_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyValue == 114) // F3
SendKeys.Send("^{TAB}"); // CTRL + TAB
if (e.KeyValue == 115) // F4
SendKeys.Send("^+{TAB}"); // CTRL + SHIFT + TAB
} -
yes sir, I found your problem :) use FormKeyUp event. It works for all. also here is a simple sample.
private void frmMain_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyValue == 114) // F3
SendKeys.Send("^{TAB}"); // CTRL + TAB
if (e.KeyValue == 115) // F4
SendKeys.Send("^+{TAB}"); // CTRL + SHIFT + TAB
}Even if I use KeyUp or KeyDown event the result does not change. Issue is still there. When I debug the code and insert a break to the beginning of the method, I saw that the Keydown/KeyUp event is not triggered when I first press left or right button. Which means the method is never executed although a keydown/KeyUp event occurs. I think I am missing something different than catching the pressed key because when I press the left/right twice everything work just fine. Thank you Engin,
-
Even if I use KeyUp or KeyDown event the result does not change. Issue is still there. When I debug the code and insert a break to the beginning of the method, I saw that the Keydown/KeyUp event is not triggered when I first press left or right button. Which means the method is never executed although a keydown/KeyUp event occurs. I think I am missing something different than catching the pressed key because when I press the left/right twice everything work just fine. Thank you Engin,