{"id":3124,"date":"2010-08-16T08:42:54","date_gmt":"2010-08-16T08:42:54","guid":{"rendered":"https:\/\/test.simple-talk.com\/uncategorized\/handling-hot-keys-in-a-windows-application\/"},"modified":"2016-07-28T10:50:07","modified_gmt":"2016-07-28T10:50:07","slug":"handling-hot-keys-in-a-windows-application","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/blogs\/handling-hot-keys-in-a-windows-application\/","title":{"rendered":"Handling Hot Keys in a Windows Application"},"content":{"rendered":"<p>I&#8217;m mostly a web developer, but there are occasions when I find myself developing WinForm or WPF applications.&#160; On those occasions, I have often needed to handle key combinations that include the ALT, SHIFT, or CONTROL (CTRL) keys.&#160; I also distinctly remember having to use the Win32 API to make that happen.&#160; I think you may need the Win32 API if you want to handle a hot key regardless of which application has focus, but if you are only worried about the hot key when your application has focus, then there is a much simpler way that I found lingering out there on a message board.&#160; All you have to do is override the ProcessCmdKey method in the System.Windows.Forms.Form class.&#160; Then you can use pure .NET code to figure out which key combination is pressed and handle it accordingly:<\/p>\n<p><code>protected override bool ProcessCmdKey(ref Message msg, Keys keyData)      <br \/>{       <br \/>&#160;&#160;&#160; switch (keyData)       <br \/>&#160;&#160;&#160; {       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; case Keys.Alt | Keys.C:&#160; <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; \/\/Code for ALT+C Action      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; break; <\/code><\/p>\n<p><code>&#160;&#160;&#160;&#160;&#160;&#160;&#160; case Keys.Ctrl | Keys.U:     <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; \/\/Code for CONTROL+U Action      <\/p>\n<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; case Keys.Alt | Keys.B      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; \/\/Code for ALT+B Action<\/code><code>     <br \/>&#160;&#160;&#160; }       <br \/>&#160;&#160;&#160; return base.ProcessCmdKey(ref msg, keyData);       <br \/>} <\/code><\/p>\n<p>If you dig through the Keys enumeration you&#8217;ll find there is a ControlKey and an AltKey enumeration item.&#160; These are for the actual keys themselves &#8211; they are not key modifiers.&#160; So make sure you use Keys.Ctrl and Keys.Alt in the combination or else you will not get the desired results.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m mostly a web developer, but there are occasions when I find myself developing WinForm or WPF applications.&#160; On those occasions, I have often needed to handle key combinations that include the ALT, SHIFT, or CONTROL (CTRL) keys.&#160; I also distinctly remember having to use the Win32 API to make that happen.&#160; I think you&#8230;&hellip;<\/p>\n","protected":false},"author":46738,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[],"coauthors":[],"class_list":["post-3124","post","type-post","status-publish","format-standard","hentry","category-blogs"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/3124","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/46738"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=3124"}],"version-history":[{"count":2,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/3124\/revisions"}],"predecessor-version":[{"id":41925,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/3124\/revisions\/41925"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=3124"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=3124"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=3124"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=3124"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}