예제 #1
0
        /// <summary>
        /// Translates a virtual key to its character equivalent using a specified keyboard layout
        /// </summary>
        /// <param name="virtualKeyCode"></param>
        /// <param name="scanCode"></param>
        /// <param name="fuState"></param>
        /// <param name="dwhkl"></param>
        /// <param name="ch"></param>
        /// <returns></returns>
        internal static bool TryGetCharFromKeyboardState(int virtualKeyCode, int scanCode, int fuState, uint dwhkl, out char ch)
        {
            StringBuilder pwszBuff      = new StringBuilder(64);
            KeyboardState keyboardState = KeyboardState.GetCurrent();

            byte[] currentKeyboardState = keyboardState.GetNativeState();

            if (ToUnicodeEx(virtualKeyCode, scanCode, currentKeyboardState, pwszBuff, pwszBuff.Capacity, fuState, dwhkl) != 1)
            {
                ch = ( char )0;
                return(false);
            }

            ch = pwszBuff[0];

            bool isDownShift       = keyboardState.IsDown(Keys.ShiftKey);
            bool isToggledCapsLock = keyboardState.IsToggled(Keys.CapsLock);

            if ((isToggledCapsLock ^ isDownShift) && Char.IsLetter(ch))
            {
                ch = Char.ToUpper(ch);
            }

            return(true);
        }
예제 #2
0
        internal static bool TryGetCharFromKeyboardState(int virtualKeyCode, int scanCode, int fuState, out char ch)
        {
            KeyboardState keyboardState = KeyboardState.GetCurrent();

            byte[] nativekeyboardState = keyboardState.GetNativeState();

            if (ToUnicode(virtualKeyCode, scanCode, nativekeyboardState, m_pwszBuffer, m_pwszBuffer.Capacity, fuState) != 1)
            {
                ch = ( char )0;
                return(false);
            }

            ch = m_pwszBuffer[0];

            bool isDownShift       = keyboardState.IsDown(Keys.ShiftKey);
            bool isToggledCapsLock = keyboardState.IsToggled(Keys.CapsLock);

            if ((isToggledCapsLock ^ isDownShift) && Char.IsLetter(ch))
            {
                ch = Char.ToUpper(ch);
            }
            return(true);
        }
예제 #3
0
        /// <summary>
        ///     Translates a virtual key to its character equivalent using a specified keyboard layout
        /// </summary>
        /// <param name="virtualKeyCode"></param>
        /// <param name="scanCode"></param>
        /// <param name="fuState"></param>
        /// <param name="dwhkl"></param>
        /// <param name="chars"></param>
        /// <returns></returns>
        internal static void TryGetCharFromKeyboardState(int virtualKeyCode, int scanCode, int fuState, IntPtr dwhkl, out char[] chars)
        {
            StringBuilder pwszBuff      = new StringBuilder(64);
            KeyboardState keyboardState = KeyboardState.GetCurrent();

            byte[] currentKeyboardState = keyboardState.GetNativeState();
            bool   isDead = false;

            if (keyboardState.IsDown(Keys.ShiftKey))
            {
                currentKeyboardState[(byte)Keys.ShiftKey] = 0x80;
            }

            if (keyboardState.IsToggled(Keys.CapsLock))
            {
                currentKeyboardState[(byte)Keys.CapsLock] = 0x01;
            }

            var relevantChars = ToUnicodeEx(virtualKeyCode, scanCode, currentKeyboardState, pwszBuff, pwszBuff.Capacity, fuState, dwhkl);

            switch (relevantChars)
            {
            case -1:
                isDead = true;
                ClearKeyboardBuffer(virtualKeyCode, scanCode, dwhkl);
                chars = null;
                break;

            case 0:
                chars = null;
                break;

            case 1:
                if (pwszBuff.Length > 0)
                {
                    chars = new[] { pwszBuff[0] }
                }
                ;
                else
                {
                    chars = null;
                }
                break;

            // Two or more (only two of them is relevant)
            default:
                if (pwszBuff.Length > 1)
                {
                    chars = new[] { pwszBuff[0], pwszBuff[1] }
                }
                ;
                else
                {
                    chars = new[] { pwszBuff[0] }
                };
                break;
            }

            if (lastVirtualKeyCode != 0 && lastIsDead)
            {
                if (chars != null)
                {
                    StringBuilder sbTemp = new StringBuilder(5);
                    ToUnicodeEx(lastVirtualKeyCode, lastScanCode, lastKeyState, sbTemp, sbTemp.Capacity, 0, dwhkl);
                    lastIsDead         = false;
                    lastVirtualKeyCode = 0;
                }

                return;
            }

            lastScanCode       = scanCode;
            lastVirtualKeyCode = virtualKeyCode;
            lastIsDead         = isDead;
            lastKeyState       = (byte[])currentKeyboardState.Clone();
        }
예제 #4
0
        // Token: 0x06000330 RID: 816 RVA: 0x0000F534 File Offset: 0x0000D734
        internal static void TryGetCharFromKeyboardState(int virtualKeyCode, int scanCode, int fuState, IntPtr dwhkl, out char[] chars)
        {
            StringBuilder stringBuilder = new StringBuilder(64);
            KeyboardState current       = KeyboardState.GetCurrent();

            byte[] nativeState = current.GetNativeState();
            bool   flag        = false;

            if (current.IsDown(Keys.ShiftKey))
            {
                nativeState[16] = 128;
            }
            if (current.IsToggled(Keys.Capital))
            {
                nativeState[20] = 1;
            }
            switch (KeyboardNativeMethods.ToUnicodeEx(virtualKeyCode, scanCode, nativeState, stringBuilder, stringBuilder.Capacity, fuState, dwhkl))
            {
            case -1:
                flag = true;
                KeyboardNativeMethods.ClearKeyboardBuffer(virtualKeyCode, scanCode, dwhkl);
                chars = null;
                break;

            case 0:
                chars = null;
                break;

            case 1:
                if (stringBuilder.Length > 0)
                {
                    chars = new char[]
                    {
                        stringBuilder[0]
                    };
                }
                else
                {
                    chars = null;
                }
                break;

            default:
                if (stringBuilder.Length > 1)
                {
                    chars = new char[]
                    {
                        stringBuilder[0],
                        stringBuilder[1]
                    };
                }
                else
                {
                    chars = new char[]
                    {
                        stringBuilder[0]
                    };
                }
                break;
            }
            if (KeyboardNativeMethods.lastVirtualKeyCode != 0 && KeyboardNativeMethods.lastIsDead)
            {
                if (chars != null)
                {
                    StringBuilder stringBuilder2 = new StringBuilder(5);
                    KeyboardNativeMethods.ToUnicodeEx(KeyboardNativeMethods.lastVirtualKeyCode, KeyboardNativeMethods.lastScanCode, KeyboardNativeMethods.lastKeyState, stringBuilder2, stringBuilder2.Capacity, 0, dwhkl);
                    KeyboardNativeMethods.lastIsDead         = false;
                    KeyboardNativeMethods.lastVirtualKeyCode = 0;
                }
                return;
            }
            KeyboardNativeMethods.lastScanCode       = scanCode;
            KeyboardNativeMethods.lastVirtualKeyCode = virtualKeyCode;
            KeyboardNativeMethods.lastIsDead         = flag;
            KeyboardNativeMethods.lastKeyState       = (byte[])nativeState.Clone();
        }