KeyEventArgs.Key to char
Asked Answered
L

3

15

Is there any way to convert WPF's KeyEventArgs.Key to Char?

I tried to use KeyInterop:

var x = (Char)KeyInterop.VirtualKeyFromKey(e.Key);

For numbers and letters it works fine, but for other characters it doesn't. E.g. for OemComma it returns '1/4' instead of ','.

I need it to prevent user of inputting into TextBox any characters except numbers and separators (i.e. commas or periods).

Lollygag answered 12/3, 2013 at 10:58 Comment(3)
#5826320Hartz
possible duplicate of How to convert a character in to equivalent System.Windows.Input.Key Enum value?Humfried
Try KeyConverter.ConvertToString insteadFarinaceous
V
8

UPDATE: the comment below found the original https://mcmap.net/q/425273/-how-to-capture-the-39-39-character-on-different-locale-keyboards-in-wpf-c

Found this somewhere that I've forgotten. Still using it, not sure what holes it has, but it's worked as far as I know.

public static class KeyEventUtility
{
    // ReSharper disable InconsistentNaming
    public enum MapType : uint
    {
        MAPVK_VK_TO_VSC = 0x0,
        MAPVK_VSC_TO_VK = 0x1,
        MAPVK_VK_TO_CHAR = 0x2,
        MAPVK_VSC_TO_VK_EX = 0x3,
    }
    // ReSharper restore InconsistentNaming

    [DllImport( "user32.dll" )]
    public static extern int ToUnicode(
        uint wVirtKey,
        uint wScanCode,
        byte[] lpKeyState,
        [Out, MarshalAs( UnmanagedType.LPWStr, SizeParamIndex = 4 )] 
        StringBuilder pwszBuff,
        int cchBuff,
        uint wFlags );

    [DllImport( "user32.dll" )]
    public static extern bool GetKeyboardState( byte[] lpKeyState );

    [DllImport( "user32.dll" )]
    public static extern uint MapVirtualKey( uint uCode, MapType uMapType );

    public static char GetCharFromKey( Key key )
    {
        char ch = ' ';

        int virtualKey = KeyInterop.VirtualKeyFromKey( key );
        var keyboardState = new byte[256];
        GetKeyboardState( keyboardState );

        uint scanCode = MapVirtualKey( (uint)virtualKey, MapType.MAPVK_VK_TO_VSC );
        var stringBuilder = new StringBuilder( 2 );

        int result = ToUnicode( (uint)virtualKey, scanCode, keyboardState, stringBuilder, stringBuilder.Capacity, 0 );
        switch ( result )
        {
        case -1:
            break;
        case 0:
            break;
        case 1:
            {
                ch = stringBuilder[0];
                break;
            }
        default:
            {
                ch = stringBuilder[0];
                break;
            }
        }
        return ch;
    }
}
Vernice answered 1/8, 2014 at 1:40 Comment(1)
for bugs in this code, see the comments here: https://mcmap.net/q/425273/-how-to-capture-the-39-39-character-on-different-locale-keyboards-in-wpf-cFlagwaving
F
6

My first suggestion would be using PreviewTextInput and using the TextCompositionEventArgs. However this 'eats' the space-characters. For this 'space'-issue see:

another solution would be using the KeyConverter in a KeyUp event:

  KeyConverter kc = new KeyConverter();
  var str = kc.ConvertToString(e.Key);

For WPF: https://msdn.microsoft.com/en-us/library/system.windows.input.keyconverter(v=vs.110).aspx For Winforms: https://msdn.microsoft.com/en-us/library/system.windows.forms.keysconverter.aspx

Note that KeyConverter will give you the string "Space" instead of " ". (same for "Esc" when pressing the escape key.

Another WPF solution is using this in the KeyUp event:

((char)KeyInterop.VirtualKeyFromKey(e.Key))
Flagwaving answered 15/11, 2016 at 10:57 Comment(1)
That KeyInterop.VirtualKeyFromKey does not help either, when pressing numpad 1 (NumPad1) it will get char 'a' (byte: 97).Basketwork
C
2

Why such approach? There are different ways of preventing users from performing such input. For example, define an event-handler for the TextBox.TextChanged-event and when the input is not a decimal or a separator, revoke the input.

private void textChangedEventHandler(object sender, TextChangedEventArgs args){
//args contains a property `Changes` where you can see what happened
//check for invalid characters and revoke them
}

See TextChangedEventArgs and the event.

Corroborate answered 12/3, 2013 at 11:6 Comment(1)
I solved that problem with PreviewTextInput. But thanks anyway.Lollygag

© 2022 - 2024 — McMap. All rights reserved.