예제 #1
0
        public static char ToCharacter(this Keys key)
        {
            // Gets (hopefully) the character typed by the key
            // Alternative is: http://stackoverflow.com/questions/6214326/translate-keys-to-char
            //' The problem is that the converter returns things like "Shift+X"

            byte[]     keyStates  = new byte[256];
            const byte keyPressed = 0x80;

            keyStates[(int)(key & Keys.KeyCode)] = keyPressed;
            keyStates[(int)Keys.ShiftKey]        = (byte)((key & Keys.Shift) == Keys.Shift ? keyPressed : 0);
            keyStates[(int)Keys.ControlKey]      = (byte)((key & Keys.Control) == Keys.Control ? keyPressed : 0);
            keyStates[(int)Keys.Menu]            = (byte)((key & Keys.Alt) == Keys.Alt ? keyPressed : 0);

            StringBuilder sb = new StringBuilder(10);

            if (Windows.ToUnicodeEx(key, 0, keyStates, sb, sb.Capacity, 0, InputLanguage.CurrentInputLanguage.Handle) == 1)
            {
                return(sb[0]);
            }
            return('\0');            // Could not be converted
        }