Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to register a custom keyboard shortcut for a windows application

I want to create a windows utility application, which can be called anytime from within any other application using a keyboard shortcut, e.g.:

Win + T

Ctrl + T

Alt + T

Ctrl + Alt + T

Ctrl + Shift + T

What key combinations can I use and how to setup those in the windows registry?

(If the shortcut is used by an other application, it should of course not work.)

like image 785
Federico Elles Avatar asked Feb 28 '09 22:02

Federico Elles


2 Answers

An option for doing that programatically when your application start is calling this Windows API:

RegisterHotKey(IntPtr hwnd, int id, int fsModifiers, int vk);

And to unregister call this API:

UnregisterHotKey(IntPtr hwnd, int id);

Both exist in user32 APIs

http://www.pinvoke.net/search.aspx?search=RegisterHotKey&namespace=[All]

like image 74
Amr Elgarhy Avatar answered Sep 25 '22 08:09

Amr Elgarhy


If you need more advanced scenario to what the shell shortcut offer, you should start with reading Win32 Hooks and Hooks Overview.

More specifically, you need to add a WH_KEYBOARD hook using the SetWindowsHookEx function. You also need to unhook through UnhookWindowsHookEx when you are done.

There's an old article from Dino Esposito how to do Windows Hooks in .NET through some Win32 interop.

like image 34
Franci Penov Avatar answered Sep 25 '22 08:09

Franci Penov