Click here to Skip to main content
14,920,219 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I Have an array of buttons with an event handler on click for each button ((programmed using (Button) sender )). I want to use the keyboard in such way that the key pressed do the same thing as if a button was clicked!
How to do this without rewriting the event? (seeing that the button click is an eventhandler and the key press is a keyeventhandler)

Create some method implementing the action and call if and call if from different event handler. And yes, don't use Designer, add all event handlers to event invocation list ("+=" operator) in code.

Go through this-

If you don't mind using a combination of keys, try mapping shortcut keys to the buttons.
To do so, just put an amersand ("&") in the button's caption. That will make the next letter to react to "Alt" + Letter.

E.g. if you have a button "Do Something", make it's caption "Do So&mething" and it will get pressed by "ALT" + "M".
OUEISJ 16-Jan-12 16:11pm
The buttons represent the alphabet letters so on each click the corresponding letter appears in a label! so pressing alt for each letter is not an option!:S
There are two ways to automatically assign a keystroke to fire the 'click' event (misnamed, because it can be fired by non-click interactions):
- as lukeer states, if you put an & in the caption, that becomes the mnemonic letter which is underlined and becomes and Alt+X keystroke. E.g. button.Caption = "Ex&cellent"; => caption "Excellent", hotkey Alt+C.
- if you have a menu which duplicates the available functionality, you can also explicitly set the MenuItem.Shortcut or ToolStripMenuItem.ShortcutKeys[^] property

You can also put a key handler on the form and fire the same method as the event handler calls, for example you can adapt this article[^].

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900