- Joined
- Jul 21, 2006
- Messages
- 1,638
- Reaction score
- 341
In this thread, I shall be posting two different hot key methods. One using the RegisterHotKey function and one using the GetAsyncKeyState function -- Both of which are popular. Other examples can be found on pscode and Google
GetAsyncKeyState Method
Info: This method is very simple and easy. It takes a very tiny amount of code, which is ideal for anyone. However, it has a downside--it requires a timer set at an extremely low integer in order to maximize results. In other words -- it takes up resources, though...not much.
Code Reqs: This requires a timer called "timer1" -- which is set to 1ms (or anything that you want it set for. Don't make it too high of a number, otherwise the hotkey is less likely to work...I recommend setting it to 1 though if your new to this method -- just to make sure it works). Also, you obviously need to make a form...Keep it as "Form1" -- you know, just to make things more complicated
Add This Code To A Module
Add This Code To Timer1
THE END! Very short code, huh?
[y]NOTE: I'd like to remind people to always set their variables to their proper identities. Why? It saves memory resources. An unset variable is actually NOT unset. It turns into Long -- which is lengthy, though not harmful to do...It only takes like ONE second! Just do it...Do it for me...Do it for your country...I don't care, just do it. Besides, all the cool kids are doing it. You don't wanna be unpopular...do you? [/i]
So...what does the code mean? Well, the variable known as "DarkKnightIsHot" is the integer container for the numeric responses from GetAsyncKeyState.
GetAsyncKeyState is set to be looking for character "123" to be pressed (our hotkey) -- which, if you don't know, is the F12 button.
When the response is "0" -- nothing is pressed. If you decide to use a lot of hotkeys through this method, then I suggest keeping a "if DarkKnightIsHot = 0 then: exit sub" near the top of the code to help not waste resources (it won't waste very many bytes, but whatever [be neat]). When -32767 gets triggered, it means that the key was pressed, thus following the command I set forth --
Shell "calc" '(this just loads a calculator via the shell command...it's nothing fancy...just put it in as an example)
Simple, yeah?
Good.
------------
RegisterHotKey Method
Note: I did not write all of the code in this example. I did, however, modify it to make it easier for those who are learning. Also, I will be commenting on this code to help explain what it is that this code does.
Info: The RegisterHotKey method does NOT use a timer, unlike the GetAsyncKeyState method. This means that much less resources are used AND the hotkey is more dependable Down side? More lines of code. Cure for it? Keep a module handy so that you don't have to rewrite the code every time you use it.
Code Reqs: This code requires a Module (name it "Module1"! Lawl!) and a form (really, name this "Form1"). Also, while we're at it, make another Module ("Module2").
Add This Code To A Module
I will explain this module before even getting to the rest of the code because it is a bit lengthy.
Option Explicit -- Whenever a variable is NOT set, a red flag goes up and stops the code. This is the function of Option Explicit. It is very useful and I encourage its use.
OldWindowProc -- Stores, as said, the old window data.
SetWindowLong -- Alters a window's attributes.
CallWindowProc -- Used a lot for subclassing. Gives information to a specific window.
GWL_WNDPROC -- Sets address.
RegisterHotKey -- Tells computer what key we would like to use. For those who are more experienced in Visual Basic, I'm sure you noticed the "Modifiers" part in this function. You can use this piece if you'd like multiple keys to be pressed in order to trigger the hotkey's bidding. Before I modified this example, alt+F10 were used for triggering.
UnregisterHotKey -- Does the obvious - It makes the hotkey "no worky anymorez".
MOD_ALT -- This is purely a container. It makes users not have to type "&H1" for use of ALT as, in this case, a modifier (though it can be used as a hotkey).
---------------&H1 = ALT.
VK_F10 -- Same situation as the above, but with users not having to type "&H79" in order to use F10 as a hot key.
---------------&H79 = F10
HOTKEY_ID -- An ID to keep track of specified hotkeys (duh)
NewWindowProc -- Looks for responses. The WM_HOTKEY response AND the WM_NCDESTROY response.
WM_HOTKEY -- Triggers when hotkey is pressed (&H312).
WM_NCDESTROY -- Triggers when hotkey is unloaded (&H82).
The "If Msg = WM_NCDESTROY" if-then statement simply checks whether or not the hotkey is being unloaded. If it is, then it reverts settings back to normal and the hotkey is unregistered.
The "If Msg = WM_HOTKEY" if-then statement just triggers the hotkey event, which can be anything you specify. In this case, it will be loading a calculator (which you will see later in this example).
"NewWindowProc = CallWind..." -- This I like to call the "carry on, nothing to see here" part of the code. It makes sure everything goes well.
Form1's Code
Option Explicit -- I explained this one already.
The entire Form_Load sub-- The registering of the hotkey is something that needs to be done (usually) on load -- otherwise it is a rather crappy hotkey. However, this code can be placed elsewhere, though I don't recommend it (unless you're some reason wanting these hotkeys only to be enabled if specified, in which the user would have to trigger the event that enables 'em).
I mentioned earlier that a modifier can be used with "RegisterHotKey". I decided to set this modifier as a "0" -- making is absolutely unneeded. If you want the requirement of the user having to press alt+F10 again, change 0 into either "MOD_ALT" or "&H1".
You should have noticed that the program checks if RegisterHotKey is equal to 0. If it is, that means there was an issue either with registering the keys. This usually happens if you change the hotkey information incorrectly. Example -- RegisterHotKey(hWnd, HOTKEY_ID, VK_F10, VK_F10) -- The modifier and VKey can NOT be the same.
Module2's Code
For reasons of simplicity, I decided to put the hotkey within its own tiny module. This is not a must -- it can in fact go in your form's code by changing If Msg = WM_HOTKEY Then Hotkey into If Msg = WM_HOTKEY Then Form1.Hotkey or elsewhere for that matter.
It's an immensely tiny sub, really -- and there's not much to say about it. When the hotkey(s) is/are pressed, this sub is initiated. It opens a calculator via the shell command.
Here's some support for the methods--
VB Helper: HowTo: Install and deinstall hotkeys -- The RegisterHotKey example came from here. It was modified by me, as stated earlier, just to make it more user-friendly and easier to explain.
If you're using the GetAsyncKeyState method, here are a list of F# Key (F1-F12) values that I put together in an attempt to help utilize this method. Note: I only added the F1-F12 keys because the rest are easy to find.
If you choose to use modifiers, here are two more--
Hope it helps.
GetAsyncKeyState Method
Info: This method is very simple and easy. It takes a very tiny amount of code, which is ideal for anyone. However, it has a downside--it requires a timer set at an extremely low integer in order to maximize results. In other words -- it takes up resources, though...not much.
Code Reqs: This requires a timer called "timer1" -- which is set to 1ms (or anything that you want it set for. Don't make it too high of a number, otherwise the hotkey is less likely to work...I recommend setting it to 1 though if your new to this method -- just to make sure it works). Also, you obviously need to make a form...Keep it as "Form1" -- you know, just to make things more complicated
Add This Code To A Module
Code:
Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Add This Code To Timer1
Code:
Dim DarkKnightIsHot As Integer
DarkKnightIsHot = GetAsyncKeyState(123)
If DarkKnightIsHot = -32767 Then: Shell "calc"
THE END! Very short code, huh?
[y]NOTE: I'd like to remind people to always set their variables to their proper identities. Why? It saves memory resources. An unset variable is actually NOT unset. It turns into Long -- which is lengthy, though not harmful to do...It only takes like ONE second! Just do it...Do it for me...Do it for your country...I don't care, just do it. Besides, all the cool kids are doing it. You don't wanna be unpopular...do you? [/i]
So...what does the code mean? Well, the variable known as "DarkKnightIsHot" is the integer container for the numeric responses from GetAsyncKeyState.
GetAsyncKeyState is set to be looking for character "123" to be pressed (our hotkey) -- which, if you don't know, is the F12 button.
When the response is "0" -- nothing is pressed. If you decide to use a lot of hotkeys through this method, then I suggest keeping a "if DarkKnightIsHot = 0 then: exit sub" near the top of the code to help not waste resources (it won't waste very many bytes, but whatever [be neat]). When -32767 gets triggered, it means that the key was pressed, thus following the command I set forth --
Shell "calc" '(this just loads a calculator via the shell command...it's nothing fancy...just put it in as an example)
Simple, yeah?
Good.
------------
RegisterHotKey Method
Note: I did not write all of the code in this example. I did, however, modify it to make it easier for those who are learning. Also, I will be commenting on this code to help explain what it is that this code does.
Info: The RegisterHotKey method does NOT use a timer, unlike the GetAsyncKeyState method. This means that much less resources are used AND the hotkey is more dependable Down side? More lines of code. Cure for it? Keep a module handy so that you don't have to rewrite the code every time you use it.
Code Reqs: This code requires a Module (name it "Module1"! Lawl!) and a form (really, name this "Form1"). Also, while we're at it, make another Module ("Module2").
Add This Code To A Module
Code:
Option Explicit
Public OldWindowProc As Long
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Const GWL_WNDPROC = (-4)
Public Declare Function RegisterHotKey Lib "user32" (ByVal hWnd As Long, ByVal id As Long, ByVal fsModifiers As Long, ByVal vk As Long) As Long
Public Declare Function UnregisterHotKey Lib "user32" (ByVal hWnd As Long, ByVal id As Long) As Long
Public Const MOD_ALT = &H1
Public Const VK_F10 = &H79
Public Const HOTKEY_ID = 1
Public Function NewWindowProc(ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Const WM_NCDESTROY = &H82
Const WM_HOTKEY = &H312
If Msg = WM_NCDESTROY Then
SetWindowLong hWnd, GWL_WNDPROC, OldWindowProc
UnregisterHotKey hWnd, HOTKEY_ID
End If
If Msg = WM_HOTKEY Then Hotkey
NewWindowProc = CallWindowProc(OldWindowProc, hWnd, Msg, wParam, lParam)
End Function
I will explain this module before even getting to the rest of the code because it is a bit lengthy.
Option Explicit -- Whenever a variable is NOT set, a red flag goes up and stops the code. This is the function of Option Explicit. It is very useful and I encourage its use.
OldWindowProc -- Stores, as said, the old window data.
SetWindowLong -- Alters a window's attributes.
CallWindowProc -- Used a lot for subclassing. Gives information to a specific window.
GWL_WNDPROC -- Sets address.
RegisterHotKey -- Tells computer what key we would like to use. For those who are more experienced in Visual Basic, I'm sure you noticed the "Modifiers" part in this function. You can use this piece if you'd like multiple keys to be pressed in order to trigger the hotkey's bidding. Before I modified this example, alt+F10 were used for triggering.
UnregisterHotKey -- Does the obvious - It makes the hotkey "no worky anymorez".
MOD_ALT -- This is purely a container. It makes users not have to type "&H1" for use of ALT as, in this case, a modifier (though it can be used as a hotkey).
---------------&H1 = ALT.
VK_F10 -- Same situation as the above, but with users not having to type "&H79" in order to use F10 as a hot key.
---------------&H79 = F10
HOTKEY_ID -- An ID to keep track of specified hotkeys (duh)
NewWindowProc -- Looks for responses. The WM_HOTKEY response AND the WM_NCDESTROY response.
WM_HOTKEY -- Triggers when hotkey is pressed (&H312).
WM_NCDESTROY -- Triggers when hotkey is unloaded (&H82).
The "If Msg = WM_NCDESTROY" if-then statement simply checks whether or not the hotkey is being unloaded. If it is, then it reverts settings back to normal and the hotkey is unregistered.
The "If Msg = WM_HOTKEY" if-then statement just triggers the hotkey event, which can be anything you specify. In this case, it will be loading a calculator (which you will see later in this example).
"NewWindowProc = CallWind..." -- This I like to call the "carry on, nothing to see here" part of the code. It makes sure everything goes well.
Form1's Code
Code:
Option Explicit
Private Sub Form_Load()
If RegisterHotKey(hWnd, HOTKEY_ID, 0, VK_F10) = 0 Then
MsgBox "Error registering hotkey."
Unload Me
End If
OldWindowProc = SetWindowLong(hWnd, GWL_WNDPROC, AddressOf NewWindowProc)
End Sub
Option Explicit -- I explained this one already.
The entire Form_Load sub-- The registering of the hotkey is something that needs to be done (usually) on load -- otherwise it is a rather crappy hotkey. However, this code can be placed elsewhere, though I don't recommend it (unless you're some reason wanting these hotkeys only to be enabled if specified, in which the user would have to trigger the event that enables 'em).
I mentioned earlier that a modifier can be used with "RegisterHotKey". I decided to set this modifier as a "0" -- making is absolutely unneeded. If you want the requirement of the user having to press alt+F10 again, change 0 into either "MOD_ALT" or "&H1".
You should have noticed that the program checks if RegisterHotKey is equal to 0. If it is, that means there was an issue either with registering the keys. This usually happens if you change the hotkey information incorrectly. Example -- RegisterHotKey(hWnd, HOTKEY_ID, VK_F10, VK_F10) -- The modifier and VKey can NOT be the same.
Module2's Code
Code:
Public Sub Hotkey()
Shell "calc"
End Sub
For reasons of simplicity, I decided to put the hotkey within its own tiny module. This is not a must -- it can in fact go in your form's code by changing If Msg = WM_HOTKEY Then Hotkey into If Msg = WM_HOTKEY Then Form1.Hotkey or elsewhere for that matter.
It's an immensely tiny sub, really -- and there's not much to say about it. When the hotkey(s) is/are pressed, this sub is initiated. It opens a calculator via the shell command.
Here's some support for the methods--
VB Helper: HowTo: Install and deinstall hotkeys -- The RegisterHotKey example came from here. It was modified by me, as stated earlier, just to make it more user-friendly and easier to explain.
If you're using the GetAsyncKeyState method, here are a list of F# Key (F1-F12) values that I put together in an attempt to help utilize this method. Note: I only added the F1-F12 keys because the rest are easy to find.
For utilizing the RegisterHotKey method, here are some more HK Values--112 = F1
113 = F2
114 = F3
115 = F4
116 = F5
117 = F6
118 = F7
119 = F8
120 = F9
121 = F10
122 = F11
123 = F12
VK_NUMPAD0 = &H60
VK_NUMPAD1 = &H61
VK_NUMPAD2 = &H62
VK_NUMPAD3 = &H63
VK_NUMPAD4 = &H64
VK_NUMPAD5 = &H65
VK_NUMPAD6 = &H66
VK_NUMPAD7 = &H67
VK_NUMPAD8 = &H68
VK_NUMPAD9 = &H69
VK_MULTIPLY = &H6A
VK_ADD = &H6B
VK_SEPARATOR = &H6C
VK_SUBTRACT = &H6D
VK_DECIMAL = &H6E
VK_DIVIDE = &H6F
VK_F1 = &H70
VK_F2 = &H71
VK_F3 = &H72
VK_F4 = &H73
VK_F5 = &H74
VK_F6 = &H75
VK_F7 = &H76
VK_F8 = &H77
VK_F9 = &H78
VK_F10 = &H79
VK_F11 = &H7A
VK_F12 = &H7B
If you choose to use modifiers, here are two more--
MOD_CONTROL = &H2
MOD_SHIFT = &H4
Hope it helps.

