none
Handling keyboard events in Excel VBA

    Question

  •  

    Hi,

    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.

     

    Thanks

    Kaizenn

     

    .

    Monday, October 27, 2008 1:22 PM

All replies

  • 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

    Monday, October 27, 2008 2:22 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.

     

    Thanks

    Kaizenn.

    Tuesday, October 28, 2008 1:46 PM
  •  

    Just to bring the topic to notice again....Can someone help me out?
    Thursday, October 30, 2008 2:26 PM
  • Hey,

     

    Maybe this is in conformity with what you are looking for…

     

    Code Snippet

    Private Sub UserForm_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)

        MsgBox Chr(KeyAscii)

    End Sub

     

    Best Regards

     

    Cathrine

     

    Thursday, October 30, 2008 6:04 PM
  • 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 8:08 AM
  • Hey,

     

    There is no KeyPreview property in Excel VBA.

     

    Cath

    Friday, October 31, 2008 9:57 AM
  •  

    Oops, sorry, didn't notice the forum was on VBA (I was checking the "unanswered" questions forum). You can still define HotKeys somehow I think, you may need to add some hidden button on the form and set a shortcut key to it. What exactly do you want to do (which keys to grab? can you describe the scenario?)
    Friday, October 31, 2008 12:08 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 Sad

     

    Sunday, November 02, 2008 3:01 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.

     

    Cath

     

    Sunday, November 02, 2008 6:42 PM