|
-
Oct 8th, 2003, 02:58 PM
#1
Thread Starter
Frenzied Member
How to capture Arrow Keys?
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)
-
Oct 8th, 2003, 04:03 PM
#2
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;
}
~
if a post is resolved, please mark it as [Resolved]
protected string get_Signature(){return Censored;}
[vbcode][php] please use code tags when posting any code [/php][/vbcode]
-
Oct 9th, 2003, 12:29 AM
#3
Thread Starter
Frenzied Member
-
Oct 9th, 2003, 03:33 AM
#4
it will work for all keyboard presses , to check which keys are being pressed you can do this...
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;
}
hope it helps , and thanks for the congrats ( hard work but fantastic )
~
if a post is resolved, please mark it as [Resolved]
protected string get_Signature(){return Censored;}
[vbcode][php] please use code tags when posting any code [/php][/vbcode]
-
Oct 9th, 2003, 01:44 PM
#5
Thread Starter
Frenzied Member
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?
Last edited by moinkhan; Oct 9th, 2003 at 01:57 PM.
-
Oct 17th, 2003, 07:16 AM
#6
Lively Member
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."
-
Oct 17th, 2003, 08:14 AM
#7
PowerPoster
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");
}
}
-
Oct 17th, 2003, 11:25 AM
#8
Lively Member
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.
-
Oct 20th, 2003, 01:29 PM
#9
Thread Starter
Frenzied Member
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");
}
}
This would not work.. because keydown of form doesn't trigger when we press arrow keys..
Crunch.. your guidance seem to be the one which addresses my problem.. but let me check it..
Thanx anyways everyone..
-
Oct 20th, 2003, 01:56 PM
#10
PowerPoster
Originally posted by moinkhan
This would not work.. because keydown of form doesn't trigger when we press arrow keys..
Sure it does....
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..)
Last edited by Lethal; Oct 20th, 2003 at 02:10 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|