Thursday, November 11, 2010

How to set a keyboard shortcut in VB 6?

How can I set a keyboard shortcut, such as- Alt + F1, that performs the same function as a particular button?



For example, I want to close a window and I have coded the button for it already. How can I make a keyboard shortcut so that pressing Alt + F1 closes the window.How to set a keyboard shortcut in VB 6?
set a ';KeyDown'; sub for your button and check for the key combo you want to test for. If the combo is entered, then issue a ';double-click'; event to your button.



for example, assume your button is called testbutton and you want to test for Alt-X (or Ctrl-X) you could do something like:



Private Sub testbutton_KeyDown(KeyCode As Integer, Shift As Integer)

If (KeyCode = vbKeyX) And (Shift = vbCtrlMask Or Shift = vbAltMask) Then

testbutton_DblClick

End If

End Sub



in your case you would want shift = vbAltMask and KeyCode=vbKeyF1



(I'm not 100% sure vbKeyF1 is a built-in constant for F1...I haven't tested it but it probably will work).



hope this helps...

No comments:

Post a Comment