.NET Forum / Windows Forms / WinForm General / March 2007
Arrow keys don't seem to match Keys.Down
|
|
Thread rating:  |
MarcG - 02 Mar 2007 22:43 GMT This has to be something silly but...
I have a breakpoint at the start of my KeyDown event handler where I want to check for the Down Arrow key (which I can't seem to match)
When I press the Down Arrow key on the arrow keypad of my keyboard, I get
e.KeyCode == RButton | ShiftKey and e.KeyData == RButton | ShiftKey | Alt
but, Keys.Down == Back | Space
so my check if (e.KeyCode == Keys.Down) is testing False
??
justin creasy - 03 Mar 2007 01:47 GMT > This has to be something silly but... > [quoted text clipped - 13 lines] > > ?? I put together a quick little application in VS 2005 and was able to get the KeyDown event to catch the Keys.Down KeyCode. Could you show the rest of the event and where you subscribe to the event in your code?
MarcG - 04 Mar 2007 15:51 GMT Justin,
In TaskControl.Designer.cs ...
this.KeyDown += new System.Windows.Forms.KeyEventHandle(this.TaskControl_KeyDown);
In TaskControl.cs ...
private void TaskControl_KeyDown(object sender, KeyEventArgs e) { -> currentState.KeyDown(sender, e); e.Handled = true; }
Break at "->" ... <immediate window...> e.KeyCode RButton | ShiftKey e.KeyData RButton | ShiftKey | Alt Keys.Down Back | Space
justin creasy - 04 Mar 2007 16:55 GMT > Justin, > [quoted text clipped - 19 lines] > Keys.Down > Back | Space MarcG,
Nothing major seems to be wrong with your code. When you subscribe to the event it should be "KeyEventHandler" instead of "KeyEventHandle" but I'm guessing that's just a typo b/c your project wouldn't compile like that.
This is just a guess, but are you pressing the down arrow that is part of the number pad on your keyboard? I know that the number 2 which is the down arrow will give a KeyCode of "RButton | ShiftKey | Space". Try a few letters and let me know what KeyCode is coming through for each of them. Also, below you'll find my code that is working, hopefully this will help but it looks very similar to what you have.
TextBox textBox1 = new TextBox(); this.textBox1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.textBox1_KeyDown);
private void textBox1_KeyDown(object sender, KeyEventArgs e) { if(e.KeyCode == Keys.Down) { MessageBox.Show("it was KeyDown, KeyCode, and Keys.Down"); } }
Linda Liu [MSFT] - 05 Mar 2007 03:59 GMT Hi MarcG,
I performed a test based on your description but didn't reproduce the problem you mentioned.
I create a WinForms application project and subscribe the KeyDown event of the form. In the KeyDown event handler, I can detect if the Down Arrow key is pressed by checking whether the e.KeyCode equals to Keys.Down or not.
In addition, when the Num Lock is locked and then the Down Arrow key in the number pad is pressed, the e.KeyCode equals to Keys.NumPad2.
The following is the code of my test. public partial class Form1 : Form { public Form1() { InitializeComponent(); this.KeyDown += new KeyEventHandler(Form1_KeyDown); }
void Form1_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Down) { MessageBox.Show("You have pressed the Down Arrow key!"); } } }
FYI, if there're controls on the form, to get the KeyDown event of the form to be raised when the user presses a key, we should set the KeyPreview property of the form to true.
I notice that you mentioned the control 'TaskControl' in your sample code. Is it a custom control in your project? If your problem is still not resolved, you may send me your sample project that could just reproduce the problem. To get my actual email address, remove 'online' from my displayed email address.
Sincerely, Linda Liu Microsoft Online Community Support
================================================== Get notification to my posts through email? Please refer to http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif ications. Note: The MSDN Managed Newsgroup support offering is for non-urgent issues where an initial response from the community or a Microsoft Support Engineer within 1 business day is acceptable. Please note that each follow up response may take approximately 2 business days as the support professional working with you may need further investigation to reach the most efficient resolution. The offering is not appropriate for situations that require urgent, real-time or phone-based interactions or complex project analysis and dump analysis issues. Issues of this nature are best handled working with a dedicated Microsoft Support Engineer by contacting Microsoft Customer Support Services (CSS) at http://msdn.microsoft.com/subscriptions/support/default.aspx. ================================================== This posting is provided "AS IS" with no warranties, and confers no rights.
MarcG - 05 Mar 2007 17:27 GMT Linda, Justin,
So this is cute, and now I'm really confused...
In my app, I have a form and at runtime I select a user control and place it on the form. The specific user control I pick is in fact derived from a base usercontrol that I created.
The KeyPreview property of the form itself is FALSE and the keydown event handler is set up in the derived usercontrol. Neither the base control or the form have any key related event handlers set up. The user control covers the entire form which itself is maximized.
Now, what is weird is this.... I tried to create a sample, starting first with just a default form, and I set a keydown event handler on it. I was surprised when it did not detect the arrow keys at all. Going to the docs, I worked out that I needed to set up a KeyPreview event handler where I could check for the arrow keys and set e.IsInputKey to true. I did this, set a breakpoint in the KeyDown handler, and lo and behold, it went off when I hit the down arrow key and the test (e.KeyCode==Keys.Down) ran TRUE and e.KeyCode.ToString was "Back | Space"
So, I removed all event handlers from my sample form, built an empty base usercontrol and derived a usercontrol from it. Using the designer, I added a KeyDown event handler. This matched by previous design. In the form, after InitializeComponent(), I new'd my derived usercontrol and added it to the form.
When I ran it and hit the down arrow key, my breakpoint in the KeyDown handler DID NOT go off. I then added a PreviewKeyDown event handler and set e.IsInputKey=true for the down arrow. When I reran the test, the downarrow was trapped and matched properly in the keyDown event handler.
NOW, the control that made me post this problem in the first place DOES NOT have a PreviewKeyDown event handler, nor does its base control, nor does the form that the control is loaded into. Also, the form itself has KeyPreview=false.
In this configuration, my KeyDown event handler DOES execute when the down arrow is hit, BUT the keyCode is "RButton | ShiftKey", NOT "Back | Space"
When I went back and added a PreviewKeyDown event handler to my control and with the following internal code:
if (e.KeyCode == Keys.Down) { e.IsInputKey = true; }
my KeyDown event handler did successfully match e.KeyCode==Keys.Down
So, somehow my KeyDown event handler was getting invoked when I hit the down arrow key (when it should not have been because I had no PreviewKeyDown handler), and the KeyCode value was getting mangled.
Any thoughts?? I am running VS2005 but without SP1 (which made my entire VS environment unusable and I had to completely remove and reinstall VS).
Thx Marc
Linda Liu [MSFT] - 07 Mar 2007 02:01 GMT Hi MarcG,
Firstly, it is recommended to handle the keyborad stroke events within a form level, i.e. we should subscribe and handle the KeyDown event of the form. If there're controls on the form, the form's KeyDown event won't be raised when the user presses a key. To get the form informed of the key stroke, we should set the form's KeyPreview property to true.
In addition, as you have mentioned, when the user presses some certain keys, such as the TAB, RETURN, ESCAPE, and arrow keys are handled by controls on the form automatically. 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 hte special keys is pressed and return a value of true.
To sum up:
1. Create a WinForm application project. 2. Add a UserControl into the project. Override the IsInputKey method in the UserControl and return true in the override method. 3. Build the project. Drag&drop the UserControl onto the form. 4. Set the KeyPreview property of the form to true. 5. Subscribe and handle the KeyDown event of the form. 6. Build the project.
Run the program. When I press the Down Arrow key, the KeyDown event of the form is raised and the e.KeyCode equals to Keys.Down.
Hope this helps. If you have any question, please feel free to let me know.
Sincerely, Linda Liu Microsoft Online Community Support
MarcG - 07 Mar 2007 14:43 GMT Linda,
I (now) understand the rules, although I'm not sure where I should have gone to find the design pattern.
My app now works properly, although I've chosen to tie all event handlers and properties to the user control and not the form, since for me, the form is an artifact of my development environment and the one I'm using here isn't the one that the control will reside on eventually.
I'm still interested in the weird behaviour I got from my initial design. My KeyDown handler should never have been invoked and I can't come up with ANY explaination for
e.KeyCode == RButton | ShiftKey and e.KeyData == RButton | ShiftKey | Alt
Any thoughts on (a) why the handler got called, and (b) what "RButton|ShiftKey" means??
Thanks for the help.
Marc
> Hi MarcG, > [quoted text clipped - 30 lines] > Linda Liu > Microsoft Online Community Support Linda Liu [MSFT] - 08 Mar 2007 03:40 GMT Hi Marc,
Since I couldn't reproduce the werid behaviour you got from your initial design, could you please send me a sample project that could just reproduce the problem?
To get my actual email address, remove 'online' from my displayed email address.
Only after I can reproduce the problem on my side, I may give you possible assistance.
Thank you for your understanding and cooperation!
Sincerely, Linda Liu Microsoft Online Community Support
MarcG - 08 Mar 2007 12:57 GMT Linda,
You suggested this earlier, and when I started to do that, I got exactly the correct behaviour. There's obviously something screwy in my actual project, and I don't think you'd want to sort through the whole thing.
I now have a PreviewKeyDown event handler in place and everything seems to be working - that is, I've masked whatever is actually wrong. Much as I hate to do it, I think I'm going to just "declare victory" and move on.
Thanks for your help.
Marc
> Hi Marc, > [quoted text clipped - 13 lines] > Linda Liu > Microsoft Online Community Support Linda Liu [MSFT] - 09 Mar 2007 10:36 GMT Hi Marc,
Thank you for your prompt response.
I am sorry that I asked for a sample project twice, because it seems to me that you'd like to make clear the weird behavior you got from your initial design. However, if I couldn't reproduce the problem on my side, it's hard for me to explain this matter.
Since you have known how to address this matter correctly, you may let it go. If you reproduce the problem some day, you may re-open this particular issue or post a new issue and we will continue to work with you.
Good luck to your project and have a nice weekend!
Sincerely, Linda Liu Microsoft Online Community Support
Vivek - 22 Mar 2007 11:40 GMT Hi, This is the issue with the RichTextBox control, keypress event does not handle Arrow keys. You have to rely on PreviewKeyPress events.
Regards, Vivek
> Hi Marc, > [quoted text clipped - 14 lines] > Linda Liu > Microsoft Online Community Support
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|