The new vbAccelerator Site - more VB and .NET Code and Controls
Source Code
2 Common Controls Library &nbsp


NOTE: this code has been superceded by the version at the new site.



&nbsp

A HotKey Control

[Pager Control]

Download the HotKey Control with source (29kb)

Your license to the code - what you can do

This sample requires the SSubTmr.DLL component. Make sure you have loaded and registered this before trying the HotKey project.

The hotkey control is one of the controls provided as part of COMCTL32.DLL, but, being of somewhat limited utility, has never found its way into Visual Basic's controls. If you need one, however, it does the job really well.

When it has focus, you can press CTRL, ALT and SHIFT key combinations, and it will show the chosen key combination as a string in a text box. You can then use the key combination as, in this example, a hot key for the application, or you could use it as a menu accelerator.


In case you want to set your application HotKey without using the control, the code to do it is simple:

Private Const WM_SETHOTKEY = &H32
Private Declare Function SendMessageByLong Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

Public Enum echkModifierKeys
&nbsp &nbsp HOTKEYF_SHIFT = &H1
&nbsp &nbsp HOTKEYF_CONTROL = &H2
&nbsp &nbsp HOTKEYF_ALT = &H4
&nbsp &nbsp HOTKEYF_EXT = &H8
&nbsp &nbsp HOTKEYF_SHIFTCONTROL = &H3
&nbsp &nbsp HOTKEYF_ALTSHIFT = &H5
&nbsp &nbsp HOTKEYF_CONTROLALT = &H6
&nbsp &nbsp HOTKEYF_CONTROLALTSHIFT = &H7
End Enum


Public Function SetHotKey( _
&nbsp &nbsp &nbsp &nbsp ByVal hWnd As Long, _
&nbsp &nbsp &nbsp &nbsp ByVal eKeyCode As VBRUN.KeyCodeConstants, _
&nbsp &nbsp &nbsp &nbsp ByVal eModifier As echkModifierKeys _
&nbsp &nbsp ) As Boolean
Dim iR As Long
Dim lKey As Long
&nbsp &nbsp ' wParam is a word with the LoByte set to
&nbsp &nbsp ' the key code and the HiByte set to the modifier:
&nbsp &nbsp lKey = (eKeyCode And &HFF&) Or ((eModifier And &HFF&) * &H100&)
&nbsp &nbsp iR = SendMessageByLong(hWnd, WM_SETHOTKEY, lKey, 0)
&nbsp &nbsp Select Case iR
&nbsp &nbsp Case 2
&nbsp &nbsp &nbsp &nbsp Err.Raise 20001, App.EXEName & ".SetHotKey", "Hot key previously assigned"
&nbsp &nbsp Case 1
&nbsp &nbsp &nbsp &nbsp ' success
&nbsp &nbsp &nbsp &nbsp SetHotKey = True
&nbsp &nbsp Case 0
&nbsp &nbsp &nbsp &nbsp Err.Raise 20002, App.EXEName & ".SetHotKey", "Invalid window for Hot key"
&nbsp &nbsp Case -1
&nbsp &nbsp &nbsp &nbsp Err.Raise 20003, App.EXEName & ".SetHotKey", "Invalid Hot key"
&nbsp &nbsp Case Else
&nbsp &nbsp &nbsp &nbsp Err.Raise 20004, App.EXEName & ".SetHotKey", "Failed to set Hot key"
&nbsp &nbsp End Select
End Function




TopBack to top

Source Code - What We're About!Back to Source Code

&nbsp

AboutContributeSend FeedbackPrivacy

Copyright 1998-1999, Steve McMahon ( steve@vbaccelerator.com). All Rights Reserved.
Last updated: 22 November 1999