Repository navigation
SysWin Gui HotKey
Home › All Categories › sys/win › sys/win/gui › HotKey
Reads and sets the key combination in a hot key control, and the combinations it refuses.
A hot key control is an edit-like box that shows the key combination the user presses in it,
such as Ctrl+Shift+F5, so a program can let the user choose a shortcut. It only records the
combination; making it do something is up to the program, for instance with RegisterHotKey
or WM_SETHOTKEY. Create one with CWindow.AddControl( "HotKey", ... ). Every function here
takes the control's handle first and is written with the namespace:
HotKey.GetHotKey( hHotKey ).
A combination is a virtual-key code (VK_F5, asc( "A" ) …) and modifier flags:
HOTKEYF_SHIFT, HOTKEYF_CONTROL, HOTKEYF_ALT and HOTKEYF_EXT (an extended key). These are
not the MOD_* flags RegisterHotKey takes: Shift and Alt are swapped.
#import "sys/win"
#import "sys/win/gui"
using win, guiA box for choosing the key that brings the window to the front, from anywhere. Set hands
the combination to Windows with WM_SETHOTKEY; plain letters and Shift+letter are refused and
become Ctrl+Alt+letter.
#import "sys/win"
#import "sys/win/gui"
using win, gui
const IDC_HOTKEY = 101
const IDC_SET = 102
declare function WndProc( byval hwnd as HWND, byval uMsg as UINT, byval wParam as WPARAM, byval lParam as LPARAM ) as LRESULT
function main() as long
SetProcessDpiAwareness( PROCESS_SYSTEM_DPI_AWARE )
dim pWindow as CWindow = "HotKeyDemo"
dim hWin as HWND = pWindow.Create( NULL, "Choose an activation key", @WndProc )
pWindow.SetClientSize( 320, 90 )
pWindow.Center()
dim hHotKey as HWND = pWindow.AddControl( "HotKey", hWin, IDC_HOTKEY, "", 20, 30, 180, 24 )
pWindow.AddControl( "Button", hWin, IDC_SET, "&Set", 215, 28, 80, 28 )
HotKey.SetRules( hHotKey, HKCOMB_NONE or HKCOMB_S, HOTKEYF_CONTROL or HOTKEYF_ALT )
return pWindow.DoEvents( SW_NORMAL )
end function
function WndProc( byval hwnd as HWND, byval uMsg as UINT, byval wParam as WPARAM, byval lParam as LPARAM ) as LRESULT
select case uMsg
case WM_COMMAND
if CBCTL( wParam, lParam ) = IDC_SET andalso CBCTLMSG( wParam, lParam ) = BN_CLICKED then
dim wKey as DWORD = HotKey.GetHotKey( GetDlgItem( hwnd, IDC_HOTKEY ) )
if SendMessageW( hwnd, WM_SETHOTKEY, LOWORD( wKey ), 0 ) = 1 then
SetWindowText( hwnd, "Activation key set" )
else
SetWindowText( hwnd, "That key cannot be used" )
end if
end if
return 0
case WM_DESTROY
PostQuitMessage( 0 )
return 0
end select
return DefWindowProcW( hwnd, uMsg, wParam, lParam )
end function
end main()| Function | Does |
|---|---|
| GetHotKey | Returns the key and modifiers in the control. |
| SetHotKey | Puts a key in the control; the modifiers are not passed on. |
| SetRules | Sets the combinations the control refuses and what it uses instead. |
function GetHotKey( byval hHotKey as HWND ) as DWORDReturns the combination in the control: the virtual-key code in the low byte and the
HOTKEYF_* modifier flags in the next byte, so LOBYTE( LOWORD( r ) ) is the key and
HIBYTE( LOWORD( r ) ) the modifiers. That low word is also what WM_SETHOTKEY takes. Ctrl+F5
is &h274.
Returns the combination, or 0 when the control is empty.
Example
dim r as DWORD = HotKey.GetHotKey( hCtrl )
print "key &h"; hex( LOBYTE( LOWORD( r ) ) ); ", ctrl:"; ( HIBYTE( LOWORD( r ) ) and HOTKEYF_CONTROL ) <> 0Microsoft Learn: HKM_GETHOTKEY
sub SetHotKey( byval hHotKey as HWND, byval vkCode as short, byval fMods as short )Puts the key vkCode in the control.
| Parameter | Meaning |
|---|---|
vkCode |
The virtual-key code. |
fMods |
HOTKEYF_* flags; not passed on, as above. |
Example
HotKey.SetHotKey( hCtrl, VK_F5, 0 ) ' F5
SendMessageW( hCtrl, HKM_SETHOTKEY, MAKEWORD( VK_F5, HOTKEYF_CONTROL ), 0 ) ' Ctrl+F5Microsoft Learn: HKM_SETHOTKEY
sub SetRules( byval hHotKey as HWND, byval fwCombInv as WORD, byval fwModInv as WORD )Says which modifier combinations the control refuses, and what it puts in their place. When the
user presses a refused combination, the control uses the same key with the fwModInv modifiers
instead.
| Parameter | Meaning |
|---|---|
fwCombInv |
The refused combinations, any of HKCOMB_NONE (no modifier), HKCOMB_S, HKCOMB_C, HKCOMB_A, HKCOMB_SC, HKCOMB_SA, HKCOMB_CA, HKCOMB_SCA. |
fwModInv |
The HOTKEYF_* flags used instead. |
Example
HotKey.SetRules( hCtrl, HKCOMB_NONE or HKCOMB_S, HOTKEYF_CONTROL or HOTKEYF_ALT )Microsoft Learn: HKM_SETRULES
- sys/win/gui
-
CWindow for
AddControl( "HotKey", ... ) - Menu for keyboard shortcuts on menu items
- Edit
tiko-lang · Home · Feature Index · Keyword Index · License
FreeBASIC manual pages reproduced under the GNU FDL 1.3.
- Generics
- Interfaces
- For Each
- Iterators
- Tuples
- Array Results
- Defer
- Lambdas
- Block Strings
- Partial Types
- String Slices
- Embed
- Typed Variadics
- C Header Import
- Foreign Library Linking
- UString
- Fixed-Width Types
- Nullptr
- String Library
- Format
- Array
- Map
- Set
- LinkedList
- OrderedMap
- OrderedSet
- Sorting
- Optional
- Result
- Smart Pointers
- Date and Time (Chrono)
- Filesystem
- File Streams
- Config (INI and TOML)
- Codec
- Hash
- UUID
- OS
- Process
- Net
- HTTP and TLS
- Math Library
- Finance
- Logging
- Terminal TUI Library
- PsControls
- tkControls
- sys/win
- Goroutines and Channels
- LLVM Backend
- tiko build
- TIKO_COMPILER
- Variable Declarations
- Standard Data Types
- User Defined Types
- Procedures
- Operators
- Control Flow Statements
- Converting Data Types
- Array Functions
- String Functions
- Mathematical Functions
- Bit Manipulation
- Date and Time Functions
- File I/O Functions
- Console Functions
- User Input (Runtime Library)
- User Input Functions (Graphics Library)
- Screen Functions
- 2D Drawing Functions
- Graphics Library Reference
- Graphics Keyword List
- Memory Functions
- Threading Support Functions
- Error Handling Functions
- Operating System Functions
- Preprocessor
- Intrinsic Defines
- Miscellaneous Keywords
- Modularizing