What is the way to capture arrow keys in KeyPress Event in c#?
I am trying e.KeyChar.. but no luck,.. it seems that keypress event doesn't trigger on arrow keys.... (left,right,up,down)
Printable View
What is the way to capture arrow keys in KeyPress Event in c#?
I am trying e.KeyChar.. but no luck,.. it seems that keypress event doesn't trigger on arrow keys.... (left,right,up,down)
you need to override the keypress event ( and have keypreview set to true ) , here's a quick example i knocked you up, hope it helps.
VB Code:
private void Form1_Load(object sender, System.EventArgs e) { this.KeyPreview=true; } protected override bool ProcessKeyPreview(ref System.Windows.Forms.Message m) { switch(m.WParam.ToInt32()) { case 37: // <--- left arrow. Console.Write("you pressed the left arrow!\n" ); // do stuff for Left Arrow here. break; case 38: // <--- up arrow. Console.Write("you pressed the up arrow!\n"); // do stuff for Up Arrow here. break; case 39: // <--- right arrow. Console.Write("you pressed the right arrow!\n"); // do stuff for Right Arrow here. break; case 40: // <--- down arrow. Console.Write("you pressed the down arrow!\n"); // do stuff for Down Arrow here. break; } return false; }
Thanx!! I will check the code... i hope it would be same for keypress of a button...
And Congratulations for the Beautiful Addition to your Family.... :wave: :wave: Twins are always CUTE:)
it will work for all keyboard presses , to check which keys are being pressed you can do this...
hope it helps , and thanks for the congrats ( hard work but fantastic :D )VB Code:
protected override bool ProcessKeyPreview(ref System.Windows.Forms.Message m) { Console.Write(m.WParam.ToString()) '/// this will show you the numeric value of the key's when they get pressed, then you can build them in to your switch statement. return false; }
OK..it's working fine.. but the event is triggered after i have pressed and released the key... what i want is to move a bar.. while i have pressed the button(left or right arrow)....keypreview is working like keypress..any idea regarding this????
is there anyway that i can detect these keys in normal keypress and keydown events?
I don't have a fix for your problem, but in answer to your last question...
//is there anyway that i can detect these keys in normal keypress and keydown events?
ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/cpref/html/frlrfSystemWindowsFormsControlClassKeyDownTopic.htm
"Certain keys, such as the TAB, RETURN, ESCAPE, and arrow keys are handled by controls automatically. In order to have these keys raise the KeyDown event, you must override the IsInputKey method in each control on your form. The code for the override of the IsInputKey would need to determine if one of the special keys is pressed and return a value of true."
You need to handle the KeyDown event. The keypress event only the standard keys within the ASCII range.
Code:private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) {
if (e.KeyCode == Keys.Left) {
MessageBox.Show("Left Arrow Pressed");
}
}
Oh duh you're still talking about handling the event at the form level. I'd actually like to do it by control for another purpose, and I thought the original poster was asking about that.
This would not work.. because keydown of form doesn't trigger when we press arrow keys..Quote:
Originally posted by Lethal
You need to handle the KeyDown event. The keypress event only the standard keys within the ASCII range.
Code:private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) {
if (e.KeyCode == Keys.Left) {
MessageBox.Show("Left Arrow Pressed");
}
}
Crunch.. your guidance seem to be the one which addresses my problem.. but let me check it..
Thanx anyways everyone..
Sure it does....Quote:
Originally posted by moinkhan
This would not work.. because keydown of form doesn't trigger when we press arrow keys..
All you need to do is is set the KeyPreview property of the form to true (I assumed you knew this from the good ol' days of VB6). The KeyDown and KeyUp Events are primarly used for capturing Control keys (Esc, Delete, Ctrl, Alt, etc..) :rolleyes: