How to register a global hotkey for your application in C#
If you are looking for a way to set a global hotkey for your C# application that can be used without your form having focus I have created a decently commentated example for you below that you can use.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | using System; using System.Windows.Forms; namespace GlobalHotkeyExampleForm { public partial class ExampleForm : Form { [System.Runtime.InteropServices.DllImport("user32.dll")] private static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vk); [System.Runtime.InteropServices.DllImport("user32.dll")] private static extern bool UnregisterHotKey(IntPtr hWnd, int id); enum KeyModifier { None = 0, Alt = 1, Control = 2, Shift = 4, WinKey = 8 } public ExampleForm() { InitializeComponent(); int id = 0; // The id of the hotkey. RegisterHotKey(this.Handle, id, (int)KeyModifier.Shift, Keys.A.GetHashCode()); // Register Shift + A as global hotkey. } protected override void WndProc(ref Message m) { base.WndProc(ref m); if (m.Msg == 0x0312) { /* Note that the three lines below are not needed if you only want to register one hotkey. * The below lines are useful in case you want to register multiple keys, which you can use a switch with the id as argument, or if you want to know which key/modifier was pressed for some particular reason. */ Keys key = (Keys)(((int)m.LParam >> 16) & 0xFFFF); // The key of the hotkey that was pressed. KeyModifier modifier = (KeyModifier)((int)m.LParam & 0xFFFF); // The modifier of the hotkey that was pressed. int id = m.WParam.ToInt32(); // The id of the hotkey that was pressed. MessageBox.Show("Hotkey has been pressed!"); // do something } } private void ExampleForm_FormClosing(object sender, FormClosingEventArgs e) { UnregisterHotKey(this.Handle, 0); // Unregister hotkey with id 0 before closing the form. You might want to call this more than once with different id values if you are planning to register more than one hotkey. } } } |
How to register hotkey combination like ctrl + A + P
Thanks my friend , it’s working perfectly and directly what i wanted !!!
Hello, I’m a novice, I tried your code and I have a question.
How can I grab “CTRL + A + A”?
I’ll be happy to waiti for your help!
Hi, Coolmine, this program raise an exception for me saying that, unable to find an entry point named RegisterHotkey in dll user32.dll what should I do ?
hey nice code but i have a problem . What if i want to register 2 modifiers and a hot key ? example (alt+ shift + a) .
Any Solution please ?
Note that the key modifier numbers are binary. Alt is 1 (0001) and shift is 4 (0100) so alt shift will be 5 (0101).
Thank you for the post, it helped to make my hot keys work.
This also did not work for me in Windows 10.
I’ve tested the above code on Windows 10 v1709 and it works just fine. If you are having issues I suggest sharing your project so people can take a look.
Not working when the application is not activated (out of focus). how to solve the issue?
for this issue you must create hook class to sniff all pressed keys in whole windows.
I have one issue:
Thanks for this superb post, I tried this it works with windows server 2008 but when I run that program in windows 7 it’s getting in indefinite loop.
Please help !!!
Hello, found interesting and trying but..is this also able to apply to WPF ?
I’m writing a WPF application..It’s great but hope also available to WPF !
It should work just fine in WPF but you might require some additional code. Take a look at http://stackoverflow.com/questions/624367/how-to-handle-wndproc-messages-in-wpf
This does not seem to work in Windows 10 .. IS 0x0312 still the valid message code for hotkey?
Yes, it’s still valid.
I’ve tried it myself just to make sure since I’m running Windows 10 as well. Seems to be working as expected.
Really useful! Thanks a lot!
Hello this is somewhat of off topic but I was wanting to know if blogs use WYSIWYG editors or if you have to manually code with HTML. I’m starting a blog soon but have no coding experience so I wanted to get advice from someone with experience. Any help would be enormously appreciated!
Thanks, helped me to register a hotkey for an Excel 2013 Application level add-in (since there is no built event to do this as far as I know so have to listen to the messages). Only needed to change hWnd to:
RegisterHotKey(new IntPtr(Globals.ThisAddIn.Application.Hwnd), …
Word of advice though for Excel developers, the handle is different for every workbook you open (not the same as Excel 2010), so on your workbook activate event you need to load that workbooks handle.
That is some extremely useful information. Thank you for sharing.
I’m new to C# coding. How do I create this app? I created an empty project in my VS 2013 C# IDE but there is no form to paste your code into. I don’t know what kind of form to create.
I created a new project, WinForm, but that code looks so different from your code I don’t know what kind of structure you are using…WinForm? WPF?
I need this capability in my C# WinForm but I have no idea how to get your code into my code to make it work.
Had to personally thank you for this article, it saved me soo much trouble :) thanks!
Nice post, I use a different method to check the hotkey if you register multiple.
In your above code I would check the hotkey ID when the message is received like this:
Hey there! I just wish to give you a huge thumbs up for the excellent information you have right here on this post. I am returning to your blog for more soon.
Hello would you mind letting me know which hosting company you’re using? I’ve loaded your blog in 3 different browsers and I must say this blog loads a lot faster then most. Can you suggest a good hosting provider at a fair price? Thank you, I appreciate it!
Good day I am so happy I found your web site, I really found you by accident, while I was searching on Yahoo for something else, Nonetheless I am here now and would just like to say kudos for a marvelous post and a all round thrilling blog (I also love the theme/design), I don’t have time to read through it all at the moment but I have book-marked it and also added your RSS feeds, so when I have time I will be back to read more, Please do keep up the awesome jo.
I’ve been surfing online more than 2 hours today, yet I never found any interesting article like yours. It’s pretty worth enough for me. In my view, if all website owners and bloggers made good content as you did, the web will be a lot more useful than ever before.
I’m really enjoying the design and layout of your blog. It’s a very easy on the eyes which makes it much more enjoyable for me to come here and visit more often. Did you hire out a designer to create your theme? Excellent work!
I’m not that much of a internet reader to be honest but your sites really nice, keep it up! I’ll go ahead and bookmark your website to come back later on. Cheers
Hello! I want to say that this article is amazing, great written and come with almost all important infos. I’d like to see extra posts like this .
Very nice post. I just stumbled upon your weblog and wanted to say that I have truly enjoyed browsing your blog posts.
After all I’ll be subscribing to your rss feed and I hope you write again soon!