{"id":688,"date":"2017-01-18T14:10:49","date_gmt":"2017-01-18T14:10:49","guid":{"rendered":"https:\/\/ibex.tech\/windows-iot\/?p=441"},"modified":"2017-01-18T14:10:49","modified_gmt":"2017-01-18T14:10:49","slug":"detecting-key-presses-on-a-page","status":"publish","type":"post","link":"https:\/\/ibex.tech\/csharp\/uwp-programming-in-c\/keypresses\/detecting-key-presses-on-a-page","title":{"rendered":"Detecting Key Presses On A Page"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">CharacterReceived &#8211; Get keypresses after they&#8217;ve been turned into the resulting characters<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>using Windows.UI.Core;<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Add to the Page_Loading<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>\t\/\/----- REGISTER FOR THE CHARACTER RECEIVED EVENT SO WE GET ALL KEYPRESSES -----\n\tWindow.Current.CoreWindow.CharacterReceived += CoreWindow_CharacterReceived;<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Add to the Page_Unloaded<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>\tWindow.Current.CoreWindow.CharacterReceived -= CoreWindow_CharacterReceived;<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Method that will be called<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>\t\/\/*********************************************************\n\t\/\/*********************************************************\n\t\/\/********** KEYPRESS CHARACTER RECEIVED ON PAGE **********\n\t\/\/*********************************************************\n\t\/\/*********************************************************\n\tprivate void CoreWindow_CharacterReceived(CoreWindow sender, CharacterReceivedEventArgs args)\n\t{\n\t\ttry\n\t\t{\n\t\t\t\/\/Flag that keypress has been handled (to stop it repeating forever)\n\t\t\targs.Handled = true;\n\n\t\t\t\/\/args.KeyCode = the the raw key code as typed after modifiers are applied.\n\t\t\t\/\/\tSo if you hold CTRL+A you'll get a value of 1 (which is CTRL+A in ascii)\n\t\t\t\/\/\tSHIFT+Z will give you 90 'Z'\n\t\t\t\/\/Note you'll get repeate calls for repeating characters\n\n\t\t\t\/\/Get the character entered\n\t\t\tstring CharacterEntered = Convert.ToString(Convert.ToChar(args.KeyCode));\n\t\t\tif ((args.KeyCode &lt; ' ') || (args.KeyCode &gt; '~'))\t\t\/\/Exit if its a non displayed character\n\t\t\t\treturn;\n\n\t\t\tif (args.KeyCode == 27)\t\/\/27=&#091;ESC]\n\t\t\t{\n\t\t\t\t\/\/ESCAPE PRESSED\n\t\t\t\t\n\t\t\t}\n\n\t\t\t\/\/If you need to see if a particular key is also pressed\n\t\t\tvar SpecialKey = Window.Current.CoreWindow.GetKeyState(Windows.System.VirtualKey.LeftWindows);\n\t\t\tif (SpecialKey.HasFlag(Windows.UI.Core.CoreVirtualKeyStates.Down))\n\t\t\t{\n\t\t\t\tif (args.KeyCode == 'a')\n\t\t\t\t{\n\t\t\t\t\t\/\/&#091;WINDOWS]+'a' PRESSED\n\t\t\t\t\t\n\t\t\t\t}\n\t\t\t}\n\n\t\t}\n\t\tcatch (Exception ex)\n\t\t{\n\t\t\tSystem.Diagnostics.Debug.WriteLine(\"Exception: \" + ex.Message);\n\t\t}\n\t}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">KeyDown &#8211; Get specific keys pressed before conversions into local characters etc<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>using Windows.UI.Core;<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Add to the Page_Loading<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>\t\/\/----- REGISTER FOR THE KEY DOWN EVENT SO WE GET ALL KEYPRESSES -----\n\tWindow.Current.CoreWindow.KeyDown += CoreWindow_KeyDown;<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Add to the Page_Unloaded<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>\tWindow.Current.CoreWindow.KeyDown -= CoreWindow_KeyDown;<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Method that will be called<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>\t\/\/***********************************************\n\t\/\/***********************************************\n\t\/\/********** KEYPRESS RECEIVED ON PAGE **********\n\t\/\/***********************************************\n\t\/\/***********************************************\n\tprivate void CoreWindow_KeyDown(CoreWindow sender, KeyEventArgs args)\n\t{\n\t\ttry\n\t\t{\n\t\t\t\/\/Flag that keypress has been handled (to stop it repeating forever)\n\t\t\targs.Handled = true;\n\n\t\t\t\/\/args.KeyCode = the the raw key code as typed after modifiers are applied.\n\t\t\t\/\/\tSo if you hold CTRL+A you'll get a value of 1 (which is CTRL+A in ascii)\n\t\t\t\/\/\tSHIFT+Z will give you 90 'Z'\n\t\t\t\/\/Note you'll get repeate calls for repeating characters\n\n\t\t\tstring Key = args.VirtualKey.ToString();\n\t\t\tif (args.VirtualKey == Windows.System.VirtualKey.Escape)\n\t\t\t{\n\t\t\t\t\/\/ESCAPE PRESSED\n\n\t\t\t}\n\n\t\t\t\/\/If you need to see if a particular key is also pressed\n\t\t\tvar SpecialKey = Window.Current.CoreWindow.GetKeyState(Windows.System.VirtualKey.LeftWindows);\n\t\t\tif (SpecialKey.HasFlag(Windows.UI.Core.CoreVirtualKeyStates.Down))\n\t\t\t{\n\t\t\t\tif (Key == \"V\")\n\t\t\t\t{\n\t\t\t\t\t\/\/&#091;WINDOWS]+'a' PRESSED\n\n\t\t\t\t}\n\t\t\t}\n\n\t\t}\n\t\tcatch (Exception ex)\n\t\t{\n\t\t\tSystem.Diagnostics.Debug.WriteLine(\"Exception: \" + ex.Message);\n\t\t}\n\t}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>CharacterReceived &#8211; Get keypresses after they&#8217;ve been turned into the resulting characters Add to the Page_Loading Add to the Page_Unloaded Method that will be called KeyDown &#8211; Get specific keys pressed before conversions into local characters etc Add to the Page_Loading Add to the Page_Unloaded Method that will be called<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[88],"tags":[],"class_list":["post-688","post","type-post","status-publish","format-standard","hentry","category-keypresses"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/688","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/comments?post=688"}],"version-history":[{"count":0,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/688\/revisions"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/media?parent=688"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/categories?post=688"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/tags?post=688"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}