How to detect arrows key in key press event handler?
-
dear all, I would like to know how can i detect the arrows key (left and right) when key press event handler in window application? thanks in advance. regards cocoonwls
The KeyPress event is not raised by noncharacter keys; however, the noncharacter keys do raise the KeyDown and KeyUp events. in KeyDown or KeyUp events you can detect arrows by e.KeyCode.
When you're alone in the Dark, Fear will protect you...
-
dear all, I would like to know how can i detect the arrows key (left and right) when key press event handler in window application? thanks in advance. regards cocoonwls
Simple. With ASCII Code. lets take an example of pressing the keys in just form1:
if(e.KeyChar==37) txtStatus.Text = "You've pressed the left arrow key."; if(e.KeyChar==38) txtStatus.Text = "You've pressed the up arrow key."; if(e.KeyChar==39) txtStatus.Text = "You've pressed the right arrow key.";
This is just a small example to get you started and so that u get to know the use of ASCII char tables. Edit: Well it would be, if it would work... kinda forgot how i used this stuff. Heres a good link on explanating the keypress control: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keypress(VS.71).aspx[^] Greets, MatjažForça Barça!
modified on Wednesday, November 5, 2008 1:54 AM
-
dear all, I would like to know how can i detect the arrows key (left and right) when key press event handler in window application? thanks in advance. regards cocoonwls
Hi Pedram Behroozi and max00slo, I have tried it before, for other char is OK, but i just can't detect the arrows key.It never fire the keydown event. What i am do in my application is, i create the keydown event handler in one of my custom UI control, and it will pass the event args to one of my class via a abstract class. any ideas are welcome. thanks in advance cocoonwls
modified on Wednesday, November 5, 2008 2:16 AM
-
dear all, I would like to know how can i detect the arrows key (left and right) when key press event handler in window application? thanks in advance. regards cocoonwls
-
Hi Pedram Behroozi and max00slo, I have tried it before, for other char is OK, but i just can't detect the arrows key.It never fire the keydown event. What i am do in my application is, i create the keydown event handler in one of my custom UI control, and it will pass the event args to one of my class via a abstract class. any ideas are welcome. thanks in advance cocoonwls
modified on Wednesday, November 5, 2008 2:16 AM
cocoonwls wrote:
i just can't detect the arrows key
Didn't this work?
if (e.KeyCode == Keys.Right)
{
...
}When you're alone in the Dark, Fear will protect you...
-
cocoonwls wrote:
i just can't detect the arrows key
Didn't this work?
if (e.KeyCode == Keys.Right)
{
...
}When you're alone in the Dark, Fear will protect you...
-
You will need to override
ProcessCmdKey
method to catch the arrow key press. -
Hi d@nish, Can you please give me a simple explain on it, where should i put the override code, and should i add the virtual method on it(for override)? thanks in advance cocoonwls
You just need to override the method in your class. This VB.Net[^] code should get you going with it.
-
You just need to override the method in your class. This VB.Net[^] code should get you going with it.
-
dear all, I would like to know how can i detect the arrows key (left and right) when key press event handler in window application? thanks in advance. regards cocoonwls
you must derive a new class that is based on the class of the control that you want, and you override the ProcessCmdKey().
Syntax:
C#:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
//handle your keys here
}Full source code..C# Arrow Key Press Vayne