locked
How To Detect @ key In Wpf RRS feed

  • Question

  • How I Detect Whether @ or 2 is pressed by user as

    both have value of D2 in   Key(class)  key.d2

    Saturday, March 8, 2014 4:05 PM

Answers

  • The following GetCharFromKey help method should solve your issue:

    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,
            }
    
            [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);
                byte[] keyboardState = new byte[256];
                GetKeyboardState(keyboardState);
    
                uint scanCode = MapVirtualKey((uint)virtualKey, MapType.MAPVK_VK_TO_VSC);
                StringBuilder 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;
            }
    
            private void TextBox_KeyDown(object sender, KeyEventArgs e)
            {
                if (e.Key == Key.D2 && GetCharFromKey(e.Key) == '@')
                {
                    //@ was pressed...
                }
            }

    Please refer to the following links for more information:

    http://huddledmasses.org/how-to-get-the-character-and-virtualkey-from-a-wpf-keydown-event/
    http://stackoverflow.com/questions/5825820/how-to-capture-the-character-on-different-locale-keyboards-in-wpf-c


    Sunday, March 9, 2014 12:01 PM

All replies

  • For @ the Shift/Caps Lock would be pressed.   Use the KeyBoard.IsKeyDown method to check.

    if Keyboard.IsKeyDown(Key.LeftShift ) etc.


    Lloyd Sheen

    Saturday, March 8, 2014 4:27 PM
  • The following GetCharFromKey help method should solve your issue:

    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,
            }
    
            [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);
                byte[] keyboardState = new byte[256];
                GetKeyboardState(keyboardState);
    
                uint scanCode = MapVirtualKey((uint)virtualKey, MapType.MAPVK_VK_TO_VSC);
                StringBuilder 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;
            }
    
            private void TextBox_KeyDown(object sender, KeyEventArgs e)
            {
                if (e.Key == Key.D2 && GetCharFromKey(e.Key) == '@')
                {
                    //@ was pressed...
                }
            }

    Please refer to the following links for more information:

    http://huddledmasses.org/how-to-get-the-character-and-virtualkey-from-a-wpf-keydown-event/
    http://stackoverflow.com/questions/5825820/how-to-capture-the-character-on-different-locale-keyboards-in-wpf-c


    Sunday, March 9, 2014 12:01 PM