Rashka
08-15-2004, 04:33 PM
how would I go about having a function or sub that is always waiting for specific keys to be pressed?
Lets say CTRL + O to open a file, like in many Windows apps. How can I program for this event?
RegisterHotKey and UnregisterHotKey, there are examples of how to use them in the links:
http://www.mentalis.org/apilist/RegisterHotKey.shtml
http://www.mentalis.org/apilist/UnregisterHotKey.shtml
MikeJ
08-15-2004, 06:15 PM
Or, to cheat, you can use the Menu Editor, but make the menu invisible - the hotkeys would still work. Then in the Click event for the menu with the Hotkey that you've set up, you do what you want - this way you'd have it without the API method. Also, you can just check in the KeyDown sub (Shift = vbCtrlMask and KeyCode = vbKeyO - but make sure KeyPreview is set to true for the form.)
Rashka
08-15-2004, 08:00 PM
Thanks for both replies. I decided to use the HotKeys API method. Wasnt sure if there was any easier way of doing it. Might be eaiser ways, but I know, and am comfortable with, HotKeys API.
MikeJ
08-15-2004, 08:24 PM
If it's just for your form, then yes, the hotkeys API is way overkill... This works just as well:
'Be sure to set the KeyPreview property of the form to True
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyO And Shift = vbCtrlMask Then
MsgBox "Ctrl+O pressed."
End If
End Sub
:-\
Rashka
08-15-2004, 08:50 PM
Set to true, and that code is not doing anything. I have never used this peice of code before.
Something I am faced with now, that I have never had to do before....How to register 2 (or more) hotkeys to run in the same time. Like CTRL+O & CTRL+P & CTRL+S...just like in most microsoft programs. I have used hotkeys before, but never had to have more than one at any given time. I just tried doing so, and go errors.
MikeJ
08-15-2004, 10:30 PM
Well, in that case, try using the Menu Editor option that I outlined earlier - it makes for less messy API stuff that you have to remember to Unregister later on.