Determine which Global Hot Key was Pressed; API in VBA
I am trying to find a clean way to close up the registered hot key that initiates an end user script.
The problem is when the script is stopped, the hot key is not unregistered and sits out in computer neverland until the application I am using for the script is completely shut down.
I would like the user to be able to just press the play button again for the hot key to be reregistered and set the script in a waiting status until the hotkey is pressed. Currently, I have to either press the play button 2 or 3 times or it just won't register at all.
Thanks,
Garrett
Re: Determine which Global Hot Key was Pressed; API in VBA
Questions
1. Can you provide some code?
2. Can you also define exactly "script"?
a. Is it a VBA module or
b. Are you calling a VB script from a module or
c. Something else - be specific
3. Is the hotkey generated in the script or does your code pass it to the script?
As much detail you can provide would be helpful
Re: Determine which Global Hot Key was Pressed; API in VBA
1)
'Checks to see if the atom is available and if yes, then adds the atom
atomAvail = GlobalFindAtom(strAtom)
If atomAvail <> 0 Then
blnAtomAvail = False
setHotKey = False
GoTo setHotKey_Error_Handler
Else
atomNum = GlobalAddAtom(strAtom)
blnAtomAvail = True
End If
'Process to see if the registration worked.
ret = RegisterHotKey(Handle, atomNum, MOD_UNMODIFIED, vbKeyF11)
If ret = 0 Then
blnHKAvail = False
setHotKey = False
'This will go and delete the atom that was previously created in the last If Then
'adHocCloseHotKey "F5"
GoTo setHotKey_Error_Handler
Else
GlobalGetAtomName atomNum, atomBuffer, Len(atomBuffer)
atomBuffer = Trim(Left$(atomBuffer, Len(atomBuffer)))
blnHKAvail = True
setHotKey = True
End If
----> This is just the part of my setHotKey function that actually assigns the hotkey and I have another function just like this for Alt + F11<------
Do While Not blnCancel = True
WaitMessage
If PeekMessage(Message, Handle, WM_HOTKEY, WM_HOTKEY, PM_REMOVE) Then
blnCancel = True
Step = "*********"
End If
DoEvents
Loop
-----> This part is another function that is called and basically it keeps looping until the current hotkey(F11) is pressed and then it jumps out of the loop and proceeds to the next line of code<----
2) I have a VBA module entitled API where I store any of my user created API functions/subs and that is where I have my setHotKey, setKillKey(I don't have this one working at this time), and msgProcessing.
I am using a third party software called Boston Workstation and it uses VBA in the background
3) In the program that I use, the front end is basically If...Then statements where basically a screen condition is met and then code is executed. When my script starts, I have an Initialize function that makes sure all of my variables are clear, class modules are instantiated, and my hotkey set and running.
________________________________________________
I would like to be able to tell if the F11 hotkey was pressed which would set the script on a path of doing what it is supposed to do and then return to waiting to process the next time it is pressed. I would also like to be able to see if Alt F11 is pressed so that I can have the script cleaning unregister the hotkeys and then close the program itself.
Re: Determine which Global Hot Key was Pressed; API in VBA
Take a look at the documentation. According to that documenation, hotkeys come in 3 flavors
1. Attached to a window where the ID is between 0x0000 through 0xBFFF
2. Attached to a thread (windowless)
3. Attached to a shared DLL where the ID is returned by GlobalAtom function
Since your VBA module is not a shared DLL, I think using GlobalAtom to return an ID is incorrect. Also, in the documentation it says that if you create a hotkey (option 1 above), and the same hWnd and ID are used, the hotkey will be replaced except in Vista & above.
I think your best solution may be to create the hotkey using a static ID and ensure you unregister it when its purpose is done (probably when last step is performed) and exit your message pump.
I admit that I may not fully understand your scenario.