Beispiel #1
0
        private void OnKeyDown(object sender, KeyEventArgsEx e)
        {
            if (Keys.W != e.KeyCode || e.Injected)
            {
                return;
            }

            if (_isOnCooldown)
            {
                return;
            }

            e.SuppressKeyPress = true;
            e.Handled          = true;

            _isOnCooldown = true;

            Task.Run(async() =>
            {
                _inputSimulator.Keyboard
                .KeyPress(VirtualKeyCode.VK_Q)
                .KeyPress(VirtualKeyCode.VK_W);

                await Task.Delay(2000)
                .ConfigureAwait(false);

                Log("OnKeyDown callback");

                _isOnCooldown = false;
            });
        }
Beispiel #2
0
        /// <summary>
        /// Recieves the actual unsafe keyboard hook procedure
        /// </summary>
        /// <param name="code"></param>
        /// <param name="wParam"></param>
        /// <param name="lParam"></param>
        /// <returns></returns>
        private IntPtr KeyboardProc(int code, IntPtr wParam, IntPtr lParam)
        {
            KBDLLHOOKSTRUCT hookStruct = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(KBDLLHOOKSTRUCT));

            int  message  = wParam.ToInt32();
            bool injected = (hookStruct.flags & (int)KEYBDHOOKF.LLKHF_INJECTED) != 0;
            bool handled  = false;

            if (Messages.WM_KEYDOWN == message || Messages.WM_SYSKEYDOWN == message)
            {
                KeyEventArgsEx e = new KeyEventArgsEx((Keys)hookStruct.vkCode, injected);
                OnKeyDown(e);
                handled = e.Handled;
            }
            else if (Messages.WM_KEYUP == message || Messages.WM_SYSKEYUP == message)
            {
                KeyEventArgsEx e = new KeyEventArgsEx((Keys)hookStruct.vkCode, injected);
                OnKeyUp(e);
                handled = e.Handled;
            }

            if (Messages.WM_KEYDOWN == message && null != KeyPress)
            {
                byte[] keyState = new byte[256];
                byte[] buffer   = new byte[2];
                NativeMethods.GetKeyboardState(keyState);
                int conversion = NativeMethods.ToAscii(hookStruct.vkCode, hookStruct.scanCode, keyState, buffer, hookStruct.flags);

                if (conversion == 1 || conversion == 2)
                {
                    bool shift   = (NativeMethods.GetKeyState((byte)VirtualKeyCode.VK_SHIFT) & 0x80) == 0x80;
                    bool capital = NativeMethods.GetKeyState((byte)VirtualKeyCode.VK_CAPITAL) != 0;
                    char c       = (char)buffer[0];

                    if ((shift ^ capital) && char.IsLetter(c))
                    {
                        c = char.ToUpper(c);
                    }

                    KeyPressEventArgs e = new KeyPressEventArgs(c);
                    OnKeyPress(e);
                    handled |= e.Handled;
                }
            }


            return(handled ? (IntPtr)1 : NativeMethods.CallNextHookEx(_handle, code, wParam, lParam));
        }
Beispiel #3
0
 /// <summary>
 /// Raises the <see cref="KeyUp"/> event
 /// </summary>
 /// <param name="e">Event Data</param>
 protected virtual void OnKeyUp(KeyEventArgsEx e)
 {
     KeyUp?.Invoke(this, e);
 }