You are on page 1of 2

Tip 190: Adding Hot Key Access to Your Visual Basic Application

December 5, 1995
Abstract
This article explains how to add a hot key that allows the user of a running app
lication to quickly switch to your Microsoft Visual Basic application.
Providing Task Switching by Adding a Hot Key
Under the Microsoft Windows 95 operating system, you can easily switch between run
ning applications by clicking on an application's icon on the taskbar or by pres
sing the ALT+TAB key combination. When developing a Microsoft Visual Basic applic
ation, you may want to provide a quick method for the user to switch to the appl
ication. This can be done by adding a hot key to your application.
Whenever the user presses the specific hot key that you have assigned to your Vi
sual Basic application, your application receives the focus and is maximized. Th
e user does not have to use ALT+TAB or the Windows 95 taskbar to activate your a
pplication.
Suppose that whenever the user presses the PAUSE key, you want to activate your
Visual Basic application. Therefore, you need to monitor the computer system so
that the application is activated only when this keystroke is detected on the ke
yboard. This can be accomplished by using the Windows application programming in
terface (API) SendMessage function.
The SendMessage function can be used to send a specific message to a window. To
use this function, add the following Declare statement to the General Declaratio
ns section of your form:
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA"
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long,
lParam As Long) As Long
The SendMessage function takes the following four arguments:
hWnd A long value containing the window's handle. The message is sent to this wi
ndow.
wMsg A long value containing the message you want to send to hWnd.
wParam A long value containing additional message-dependent information.
lParam A long value containing additional message-dependent information.
In the example program below, you want to set PAUSE as the hot key to your appli
cation. Because you want the application to be activated when PAUSE is pressed,
you need to send a WM_SETHOTKEY message to the application's windowin this case,
Form1. A WM_SETHOTKEY message is used to assign a specific key as an application
's hot key. Therefore, we need to use the SendMessage function to send a WM_HOTK
EY message to the application. This message is placed at the top of the thread's
message queue, which in turn allows the PAUSE key to gain immediate attention w
hen it is detected.
Note that a window can have only one hot key associated with it at any one time.
In addition, child windows cannot have hot keys associated with them. Finally,
if you assign a hot key to a window that already has a hot key assigned to it, t
he new hot key replaces the original one.
Example Program
This program below shows how to add hot key access to your Visual Basic applicat
ions.
Create a new project in Visual Basic. Form1 is created by default.
Add the following Constant and Declare statements to the General Declarations se
ction of Form1 (note that the Declare statement must be typed as a single line o
f code):
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA"
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long,
lParam As Long) As Long
Const WM_SETHOTKEY = &H32
Const VK_PAUSE = &H13
Add the following code to the Click event for Form1:
Private Sub Form_Click()
Dim I As Long
I = SendMessage(Me.hwnd, WM_SETHOTKEY, VK_PAUSE, 0)
MsgBox "The Pause key was pressed"
End Sub
Run the example program by pressing F5. Each time you click the mouse on the for
m, the "The Pause key was pressed" message box is displayed. Now, minimize the e
xample program. Control returns to the Visual Basic design environment. Press PA
USE on the keyboard. The example program is immediately maximized and receives t
he focus.

You might also like