KeyEventArgs.Key to char

橙三吉。 提交于 2019-12-23 07:02:05

问题


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).


回答1:


UPDATE: the comment below found the original http://stackoverflow.com/a/5826175/187650

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



回答2:


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

  • How can I prevent input controls from stealing the space character from the TextCompositionManager?
  • Why does PreviewTextInput not handle spaces?

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))



回答3:


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.



来源:https://stackoverflow.com/questions/15359336/keyeventargs-key-to-char

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!