Invalid WPF Code for KeyDown ASCII

I need to get the numeric values ​​of keyboard keys based on an ASCII table . Since WPF doesn't have a built-in solution, I tried a few hacks:

1. This only gives the upper case value.

        char res ;
        Char.TryParse(e.Key.ToString(),out res);
        Debug.WriteLine((int)res);

      

2 This one, even though listed in many places as a possible solution, gives a completely wrong number.

        Debug.WriteLine(Convert.ToInt16(e.Key).ToString());

      

So how do I get both upper and lower case letters of ASCII codes from input in WPF?

Update

After getting multiple answers here, I want to emphasize my question. I absolutely need to get ASCII codes from an input for both lower and upper case. No hard coded char comparison or anything like that. I am looking for a more general way.

+3


source to share


4 answers


Based on one of the answers mentioned here , I suggest you use the event TextInput

(or PreviewTextInput

) to get the email. It should give correct values ​​for upper and lower case letters.

To test it, you can use code similar to this one:



private void Window_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
    Console.WriteLine(String.Format("letter: {0} (charcode: {1})", e.TextComposition.Text, (int)Convert.ToChar(e.TextComposition.Text)));
}

      

Let me know if it helps or if you have any problems using this solution.

+3


source


Have a look at KeyEventUtility.GetCharFromKey (e.Key)

UPDATE: sorry it was some kind of library code



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

      

+4


source


This is not how keyboards work. Every PC in the world has the same keyboard. Pressing a key generates a virtual key value, the same value on every keyboard in the world. The e.Key value that you get in the KeyDown event.

This is where it stops for keys like function keys, cursor keys, Shift key, etc. If the key is not the enter key that creates the character. Like the letter A. Internally, the KeyDown event is handled by Windows and translated to a character. The actual character created depends on the keyboard layout. Users from different countries have different keyboard layouts, preferring the glyphs that are most commonly used in their language. Or, for that matter, personal preference of the user, the programmer often likes Dvorak's layout.

The character created also depends on the state of the keyboard. Like the Shift key, hold it down to create an A, not down to create a. This can be quite confusing on non-english keyboards, other layouts also have dead keys that, when pressed, change the character created by the next key. Languages ​​that have a lot of diacritics use them.

The implication is clear, translating the virtual key that you get from the KeyDown event is a very dangerous business. Therefore, you should not try to do this. And you don't have to use the TextInput event.

+2


source


if (e.Key == Key.A )

      

Look at the page

0


source







All Articles