Serafina Brocious
Serafina Brocious

Reputation: 30609

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?

Upvotes: 6

Views: 2377

Answers (1)

Elvis Xia - MSFT
Elvis Xia - MSFT

Reputation: 10831

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;
}

Upvotes: 11

Related Questions