Keyboard focus in Visual Studio C#
-
Can anyone explain to me how I can detect a key pressed and deal with it in one piece of code whatever component has the current focus. I have trawed through the help files but most of what is thrown up pre-supposes a certain level of knowledge (i.e. that you know what you are doing!). I have a little program which controls the speed and timing (on/off) of a motor and I need to be able to stop the motor when any key is pressed as quickly as possible for safetly reasons whatever control currently has the focus. regards, Ted
Ted Edwards
-
Can anyone explain to me how I can detect a key pressed and deal with it in one piece of code whatever component has the current focus. I have trawed through the help files but most of what is thrown up pre-supposes a certain level of knowledge (i.e. that you know what you are doing!). I have a little program which controls the speed and timing (on/off) of a motor and I need to be able to stop the motor when any key is pressed as quickly as possible for safetly reasons whatever control currently has the focus. regards, Ted
Ted Edwards
Hello, I'm not sure if it works in every situation (actually I know it's not, because I had problems with a PDF viewer Control in this case), but you can try this in your main form. this.KeyPreview = true; this.KeyDown += new KeyEventHandler(...); this.KeyPress + = ... Hope that helps a little! All the best, Martin
-
Hello, I'm not sure if it works in every situation (actually I know it's not, because I had problems with a PDF viewer Control in this case), but you can try this in your main form. this.KeyPreview = true; this.KeyDown += new KeyEventHandler(...); this.KeyPress + = ... Hope that helps a little! All the best, Martin
Thanks Martin, I think you have pointed me in the right direction. It works if I point each component's KeyDown event handler at the new form KeyDown event handler. I will have to differentiate between keys or the components which are suppose to accept new values from the keyboard - won't! Thanks for your help,
Ted Edwards
-
Thanks Martin, I think you have pointed me in the right direction. It works if I point each component's KeyDown event handler at the new form KeyDown event handler. I will have to differentiate between keys or the components which are suppose to accept new values from the keyboard - won't! Thanks for your help,
Ted Edwards
-
Thanks Martin, I think you have pointed me in the right direction. It works if I point each component's KeyDown event handler at the new form KeyDown event handler. I will have to differentiate between keys or the components which are suppose to accept new values from the keyboard - won't! Thanks for your help,
Ted Edwards
-
Hello, I wanted to say that it's a feature of the Forms class. You don't have to hook on the Controls eventhandler. Try it out on your main form and you will see. All the best, Martin
Yes, I think I see what you mean - I am trying it but the new form keyboard event handler is having trouble with the statment: if (e.KeyChar >= 48 && e.KeyChar <= 57) which gives an error Error: 'System.Windows.Forms.KeyEventArgs' does not contain a definition for 'KeyChar' What you both suggested seems to be the correct approach for what I need. I will investigate further...
Ted Edwards
-
Yes, I think I see what you mean - I am trying it but the new form keyboard event handler is having trouble with the statment: if (e.KeyChar >= 48 && e.KeyChar <= 57) which gives an error Error: 'System.Windows.Forms.KeyEventArgs' does not contain a definition for 'KeyChar' What you both suggested seems to be the correct approach for what I need. I will investigate further...
Ted Edwards
-
Thanks Martin, I think you have pointed me in the right direction. It works if I point each component's KeyDown event handler at the new form KeyDown event handler. I will have to differentiate between keys or the components which are suppose to accept new values from the keyboard - won't! Thanks for your help,
Ted Edwards