Monday, October 27, 2008 1:22 PM
I would like to know how to handle keyboard events in Excel VBA forms. I would like to capture the KeyPress, KeyDown or KeyUp events when a Excel VBA form has focus.
Monday, October 27, 2008 2:22 PM
The form controls have events to which you can attach event handling procs. If you right click the control and select to see its properties, there should be an events tab at the properties pane. Just press the "..." button next to the event you wish to handle to make such a proc
Tuesday, October 28, 2008 1:46 PM
Hi George...thanks for the post..but here I am not talking abt events associated to controls on the form. I just want to respond to some keys which are not directly associated to any control on the form..i.e when the form is active. From what I have read on the net so far it seems to be a bit tricky and looks like there is no easy way out. I would be grateful if I can get some help.
Thursday, October 30, 2008 2:26 PM
Thursday, October 30, 2008 6:04 PM
Maybe this is in conformity with what you are looking for…Code Snippet
Private Sub UserForm_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Friday, October 31, 2008 8:08 AM
I suppose you want to "preview" keypresses at the form, even though other control has the focus. This is possible (if I remember well it's a form property called KeyPreview or something like that you have to set to true). Checkout how I implement F1/F2/etc. hotkey support at LVS application (http://www.codeplex.com/lvs)
Friday, October 31, 2008 9:57 AM
There is no KeyPreview property in Excel VBA.
Friday, October 31, 2008 12:08 PM
Sunday, November 02, 2008 3:01 PM
Thanks for the posts.
Cathrine..the Form_keypress doesnt work at all and that is the very reason I was caught up here. The hidden button seems to be one solution, which I didnt want to try out as I thought there might be something better..I dont know how to get that form keypress event to work. It just never executes
Sunday, November 02, 2008 6:42 PM
You were asking to capture the KeyPress events when an Excel VBA form has the focus. Well, the form can only have focus if it has no controls, so… if you have controls on your form set the controls visible or enabled property to false… and the code snippet will execute.