Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mapping VirtualKey to char in UWP app, independent of layout

I'm writing a VNC client for HoloLens using C# and I'm having a tough time figuring out how to handle keyboard input. KeyUp/KeyDown give me a Windows.System.VirtualKey object, but there doesn't appear to be an API to map these VirtualKeys (along with modifiers, e.g. shift) to the characters they represent on a given layout. E.g. VirtualKey.Shift + VirtualKey.F == 'F' versus 'f' when it's simply VirtualKey.F. Or Shift + 5 to give % on a US keyboard.

In win32 apps you'd use MapVirtualKey to handle the keyboard layout for you -- how does this get handled in UWP?

like image 906
Serafina Brocious Avatar asked Jul 09 '16 00:07

Serafina Brocious


1 Answers

It is not possible to get the translated character in KeyUp/KeyDown events. But it is possible when using CoreWindow.CharacterReceived event to get the translated character.

You can register the event by the following codes:

Window.Current.CoreWindow.CharacterReceived += CoreWindow_CharacterReceived;

And you will get a KeyCode of the translated input character(e.g. for shift+5 it gets 37, while for 5 it gets 53) through the CharacterReceivedEventArgs:

private void CoreWindow_CharacterReceived(CoreWindow sender, CharacterReceivedEventArgs args)
{
    uint keyCode=args.KeyCode;
}
like image 69
Elvis Xia - MSFT Avatar answered Oct 26 '22 23:10

Elvis Xia - MSFT