Set global hotkeys using C
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Global hotkeys let a Windows application react to a key combination even when the app is not focused. In C#, the standard approach is to call the Win32 RegisterHotKey API, handle the WM_HOTKEY message, and always unregister the key when the app exits.
This Is a Windows API Feature
Global hotkeys are not a pure .NET feature. They come from the Windows API, which means the implementation is platform-specific and message-driven.
The two key functions are:
- '
RegisterHotKeyto reserve the key combination' - '
UnregisterHotKeyto release it later'
In a WinForms app, the simplest place to handle the hotkey is WndProc.
A Working WinForms Example
This registers Ctrl+Shift+K globally for the lifetime of the form.
How It Works
When registration succeeds, Windows reserves that combination for your window and sends WM_HOTKEY to it whenever the user presses the key combo. Your code does not need a low-level keyboard hook for this pattern.
That is important because RegisterHotKey is much simpler and safer than intercepting all keyboard input globally when you only need a few shortcuts.
Choosing Modifiers and Keys
You typically combine one or more modifier flags such as MOD_CONTROL, MOD_ALT, MOD_SHIFT, or MOD_WIN with a virtual key code.
Be careful with key choices. Some combinations are already used by Windows or by other applications. If the call returns false, it may mean the hotkey is already taken.
You can register multiple hotkeys by using different numeric IDs:
Then dispatch based on the ID in WndProc.
Cleanup Is Mandatory
Global hotkeys are a shared operating-system resource. If you register one and never unregister it, you can block that combination for the rest of the session or until the window is destroyed.
That is why cleanup belongs in OnFormClosed, Dispose, or an equivalent shutdown path. Registering is only half of the feature; unregistering is part of the contract.
Common Pitfalls
The biggest mistake is forgetting that this is Windows-specific code. A C# application using RegisterHotKey will not be portable across platforms without an alternative implementation.
Another common issue is choosing a shortcut that is already reserved by the system or another app. When registration fails, check for conflicts before assuming the code is wrong.
It is also easy to forget cleanup. If you register a hotkey, always call UnregisterHotKey when the app closes.
Summary
- In C#, global hotkeys on Windows are typically implemented with
RegisterHotKey. - Handle the
WM_HOTKEYmessage to react when the key combination is pressed. - Use unique IDs if you register more than one hotkey.
- Always call
UnregisterHotKeyduring shutdown. - Prefer this API over low-level keyboard hooks when you only need a few global shortcuts.
Related reading
- Set System.Drawing.Color values
- Set timeout for webClient.DownloadFile
- Set Visual Studio Code to be global Git editor on OSX
- Setting an object to null vs Dispose
- Setting Objects to Null/Nothing after use in .NET
- Setting tab order in WPF
- Setting WPF image source in code
- Settings variable values in a Moq Callback call

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.