March 1, 2021
Hot Topics:

Create a Hotkey for your Application

  • By Sam Huggill
  • Send Email »
  • More Articles »

As with many operations in Visual Basic, we are going to use the SendMessage API call. This time we will implement the WM_SETHOTKEY constant. Just to remind you that the WM_ part stands for Windows Message. The SendMessage API call looks like this:

Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

  • hwnd - The window handle of the object that you are sending the message to.
  • wMsg - The message that you are sending, usually a constant.
  • wParam - Additional info about the message.
  • lParam - Additional info about the message.

We can use this message to send the WM_SETHOTKEY constant and other info that will include the key(s) needed to provoke our operation. The problem is that we can't (using this code anyway) catch when the keys are pressed. The default hotkey operation just activates the window referenced through its window handle. We can employ a little workaround here by subclassing the form to catch the windows activation. For this we need to use VB5's AddressOf operator to reference the address of our window handler. We can now track when our window receives the focus and then fire up our own procedure for doing something. I won't go through the subclassing code, because it is pretty standard.

Anyway, that should be all you need to know for this "how to". Just to say that a call to your procedure (when the hotkey has been pressed) should be placed in the WindowProc function.

Page 1 of 3

This article was originally published on November 20, 2002

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date