Today is Monday, 22nd July 2024

Hotkey Methods

Author: DarkKnightH20
Language: Visual Basic 6.0

Two different hotkey 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. So it really doesn’t matter if your only computer experience is sending emails, you should be able to master this. Also, make note that this method takes up a little more resources than other methods, 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 to a Module

Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
This tells the program to obtain the state of the specified key (i.e. pressed, not pressed).

Add This Code to Timer1

Dim DarkKnightIsHot As Integer
DarkKnightIsHot = GetAsyncKeyState(123)
If DarkKnightIsHot = -32767 Then: Shell "calc"

THE END! Very short code, huh?

GetAsyncKeyState is set to be looking for key “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. 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?


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
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_HOTKEY = &H312

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 hotkey.
—————&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
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.

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 — 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.

112 = F1
113 = F2
114 = F3
115 = F4
116 = F5
117 = F6
118 = F7
119 = F8
120 = F9
121 = F10
122 = F11
123 = F12

For utilizing the RegisterHotKey method, here are some more HK Values–

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 (i.e. shift+F, ctrl+F12), here are two more–


Leave a Reply


Affiliate Articles:

Amazon Deals