CharacterReceived – Get keypresses after they’ve been turned into the resulting characters
using Windows.UI.Core;
Add to the Page_Loading
//----- REGISTER FOR THE CHARACTER RECEIVED EVENT SO WE GET ALL KEYPRESSES -----
Window.Current.CoreWindow.CharacterReceived += CoreWindow_CharacterReceived;
Add to the Page_Unloaded
Window.Current.CoreWindow.CharacterReceived -= CoreWindow_CharacterReceived;
Method that will be called
//*********************************************************
//*********************************************************
//********** KEYPRESS CHARACTER RECEIVED ON PAGE **********
//*********************************************************
//*********************************************************
private void CoreWindow_CharacterReceived(CoreWindow sender, CharacterReceivedEventArgs args)
{
try
{
//Flag that keypress has been handled (to stop it repeating forever)
args.Handled = true;
//args.KeyCode = the the raw key code as typed after modifiers are applied.
// So if you hold CTRL+A you'll get a value of 1 (which is CTRL+A in ascii)
// SHIFT+Z will give you 90 'Z'
//Note you'll get repeate calls for repeating characters
//Get the character entered
string CharacterEntered = Convert.ToString(Convert.ToChar(args.KeyCode));
if ((args.KeyCode < ' ') || (args.KeyCode > '~')) //Exit if its a non displayed character
return;
if (args.KeyCode == 27) //27=[ESC]
{
//ESCAPE PRESSED
}
//If you need to see if a particular key is also pressed
var SpecialKey = Window.Current.CoreWindow.GetKeyState(Windows.System.VirtualKey.LeftWindows);
if (SpecialKey.HasFlag(Windows.UI.Core.CoreVirtualKeyStates.Down))
{
if (args.KeyCode == 'a')
{
//[WINDOWS]+'a' PRESSED
}
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("Exception: " + ex.Message);
}
}
KeyDown – Get specific keys pressed before conversions into local characters etc
using Windows.UI.Core;
Add to the Page_Loading
//----- REGISTER FOR THE KEY DOWN EVENT SO WE GET ALL KEYPRESSES -----
Window.Current.CoreWindow.KeyDown += CoreWindow_KeyDown;
Add to the Page_Unloaded
Window.Current.CoreWindow.KeyDown -= CoreWindow_KeyDown;
Method that will be called
//***********************************************
//***********************************************
//********** KEYPRESS RECEIVED ON PAGE **********
//***********************************************
//***********************************************
private void CoreWindow_KeyDown(CoreWindow sender, KeyEventArgs args)
{
try
{
//Flag that keypress has been handled (to stop it repeating forever)
args.Handled = true;
//args.KeyCode = the the raw key code as typed after modifiers are applied.
// So if you hold CTRL+A you'll get a value of 1 (which is CTRL+A in ascii)
// SHIFT+Z will give you 90 'Z'
//Note you'll get repeate calls for repeating characters
string Key = args.VirtualKey.ToString();
if (args.VirtualKey == Windows.System.VirtualKey.Escape)
{
//ESCAPE PRESSED
}
//If you need to see if a particular key is also pressed
var SpecialKey = Window.Current.CoreWindow.GetKeyState(Windows.System.VirtualKey.LeftWindows);
if (SpecialKey.HasFlag(Windows.UI.Core.CoreVirtualKeyStates.Down))
{
if (Key == "V")
{
//[WINDOWS]+'a' PRESSED
}
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("Exception: " + ex.Message);
}
}
Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.