Esempio n. 1
0
 internal static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, int wParam, ref KBDLLHOOKSTRUCT lParam);
            public DeadKeyInfo(KBDLLHOOKSTRUCT info, byte[] keyState)
            {
                KeyCode = (Keys)info.KeyCode;
                ScanCode = info.ScanCode;

                KeyboardState = keyState;
            }
        private static string ToUnicode(KBDLLHOOKSTRUCT info)
        {
            string result = null;

            var keyState = new byte[256];
            var buffer = new StringBuilder(128);

            NativeMethods.GetKeyboardState(keyState);
            bool isAltGr = IsKeyPressed(NativeMethods.VK_RMENU) && IsKeyPressed(NativeMethods.VK_LCONTROL);
            if (isAltGr) keyState[NativeMethods.VK_LCONTROL] = keyState[NativeMethods.VK_LALT] = 0x80;

            var layout = GetForegroundKeyboardLayout();
            var count = ToUnicode((Keys)info.KeyCode, info.ScanCode, keyState, buffer, layout);

            if (count > 0)
            {
                result = buffer.ToString(0, count);

                if (_lastDeadKey != null)
                {
                    ToUnicode(_lastDeadKey.KeyCode,
                              _lastDeadKey.ScanCode,
                              _lastDeadKey.KeyboardState,
                              buffer,
                              layout);

                    _lastDeadKey = null;
                }
            }
            else if (count < 0)
            {
                _lastDeadKey = new DeadKeyInfo(info, keyState);

                while (count < 0)
                {
                    count = ToUnicode(Keys.Decimal, buffer, layout);
                }
            }

            return result;
        }
        private IntPtr keyboardHandler(int nCode, int wParam, ref KBDLLHOOKSTRUCT kbdStruct)
        {
            if (nCode >= 0)
            {
                Keys virtualKeyCode = (Keys)kbdStruct.KeyCode;
                Keys keyData = BuildKeyData(virtualKeyCode);
                var keyEventArgs = new KeyEventArgs(keyData);

                if (wParam == NativeMethods.WM_KEYDOWN || wParam == NativeMethods.WM_SYSKEYDOWN)
                {
                    RaiseKeyDownEvent(keyEventArgs);

                    string buffer = ToUnicode(kbdStruct);
                    if (!string.IsNullOrEmpty(buffer))
                    {
                        foreach (var rawKey in buffer)
                        {
                            var key = _keyConverter.ConvertToString(rawKey)[0];
                            RaiseKeyPressEvent(key);
                        }
                    }
                }
                else if (wParam == NativeMethods.WM_KEYUP || wParam == NativeMethods.WM_SYSKEYUP)
                {
                    Debug.Print("Release({0})", (int)virtualKeyCode);
                    RaiseKeyUpEvent(keyEventArgs);
                }
            }

            return NativeMethods.CallNextHookEx(this._previousKeyboardHandler, nCode, wParam, ref kbdStruct);
        }
Esempio n. 5
0
 internal static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, int wParam, ref KBDLLHOOKSTRUCT lParam);