Assume that i am creating a button for counter application, here i need to click the button with the help of a keyboard key instead of mouse click. Anyone can help with that function.
-
3is that WPF or Winforms? – JayV Jun 20 '18 at 12:19
-
Why not separate the shared logic of button click/key press into its own method, and call that method from both? – ProgrammingLlama Jun 20 '18 at 12:23
-
When the button has the focus the space bar will activate it, or did you want specific use of 'A'? You should really provide examples of the code you have already tried. – Daniel S. Fowler Jun 20 '18 at 12:23
-
is button clicking important.. you can always call logic to be performed on key press . – Amit Jun 20 '18 at 12:26
3 Answers
If you are working on windows app You should add an event to the form, where the user is expected to press the key Right click on the object that should receive the key press (the body of the form) Go to properties page Click on the thunder icon (Events) Go to the KEY section Double click on Key Down Write the following:
if (e.KeyValue == 65) MessageBox.Show("You clicked the letter A. Code of B is 66, and so on");
To discover the KeyValue of the key pressed by the user:
MessageBox.Show(e.KeyValue.ToString());
-
Are you sure this is what OP wants? This takes a lot of assumptions... – eye_am_groot Jun 20 '18 at 12:31
-
If you want to use Alt + A instead of button , which is access key for your button you can refer to the https://msdn.microsoft.com/en-in/library/ms178233.aspx

- 45
- 6
As said in another answer Alt + the key is a button shortcut. One way in WinForms is to stick & in front of the character in the button text that you want to use to activate the button. E.g. if the button text is "ABC", change it to "&ABC". Then Alt-A can be used to activate the button. & is how button shortcuts are defined.
For WPF use AccessText or, without using Alt, key bindings: Assign Short Cut Key to a button in WPF

- 1,982
- 15
- 12