protected bool OnKeyDownEvent(Win32Api.KBDLLHOOKSTRUCT kb) { KeyHookEventArgs e = new(kb); KeyDownEvent?.Invoke(this, e); return(e.Cancel); }
private IntPtr HookProc(int code, IntPtr wParam, IntPtr lParam) { var keyboardInput = Marshal.PtrToStructure <User32.KEYBDINPUT>(lParam); var pressedKey = (Keys)keyboardInput.wVk; if (code >= 0 && HookedKey == pressedKey) { var keyEvent = (int)wParam; Logger.Information("Key {pressedKey} was found", pressedKey); var keyEventArgs = new KeyEventArgs(pressedKey); // key down if (keyEvent == Constants.WM_KEYDOWN || keyEvent == Constants.WM_SYSKEYDOWN) { KeyDownEvent?.Invoke(this, keyEventArgs); } // key up else if (keyEvent == Constants.WM_KEYUP || keyEvent == Constants.WM_SYSKEYUP) { KeyUpEvent?.Invoke(this, keyEventArgs); } if (keyEventArgs.Handled) { return(new IntPtr(1)); } } return(User32.CallNextHookEx(_hook, code, wParam, lParam)); }
protected override IntPtr CustomHookProc(IntPtr wParam, IntPtr lParam) { KBDLLHOOKSTRUCT kbd = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(KBDLLHOOKSTRUCT)); bool injected = (kbd.flags & (uint)LLKHF.INJECTED) != 0; switch ((WM)wParam) { case WM.KEYDOWN: case WM.SYSKEYDOWN: if (GetAsyncKeyState((int)kbd.vkCode) >= 0 && KeyDownEvent?.Invoke(this, kbd.vkCode, injected) == true) { return((IntPtr)(-1)); } if (KeyPressEvent?.Invoke(this, kbd.vkCode, injected) == true) { return((IntPtr)(-1)); } break; case WM.KEYUP: case WM.SYSKEYUP: if (KeyUpEvent?.Invoke(this, kbd.vkCode, injected) == true) { return((IntPtr)(-1)); } break; } return(IntPtr.Zero); }
/// <summary> /// 获取键盘消息 /// </summary> /// <param name="nCode"></param> /// <param name="wParam"></param> /// <param name="lParam"></param> /// <returns></returns> private int KeyboardHookProc(int nCode, int wParam, IntPtr lParam) { // 如果该消息被丢弃(nCode<0 if (nCode < 0) { return(Win32Helper.CallNextHookEx(_hKeyboardHook, nCode, wParam, lParam)); } var keyboardHookStruct = (Win32Helper.KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(Win32Helper.KeyboardHookStruct)); var key = KeyInterop.KeyFromVirtualKey(keyboardHookStruct.vkCode); switch (wParam) { case Win32Helper.WM_KEYDOWN: case Win32Helper.WM_SYSKEYDOWN: //WM_KEYDOWN和WM_SYSKEYDOWN消息,将会引发OnKeyDownEvent事件 // 此处触发键盘按下事件 _onKeyDown?.Invoke(key); break; case Win32Helper.WM_KEYUP: case Win32Helper.WM_SYSKEYUP: //WM_KEYUP和WM_SYSKEYUP消息,将引发OnKeyUpEvent事件 // 此处触发键盘抬起事件 _onKeyUp?.Invoke(key); break; } return(Win32Helper.CallNextHookEx(_hKeyboardHook, nCode, wParam, lParam)); }
/// <summary> /// 键盘输入(扫码枪扫描)钩子 /// </summary> /// <param name="nCode"></param> /// <param name="wParam"></param> /// <param name="lParam"></param> /// <returns></returns> private int KeyboardHookProc(int nCode, int wParam, IntPtr lParam) { //如果该消息被丢弃(nCode<0)或者没有事件绑定处理程序则不会触发事件 if (nCode >= 0 && (KeyDownEvent != null || KeyUpEvent != null || KeyPressEvent != null)) { var keyDataFromHook = (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct)); var keyData = (Keys)keyDataFromHook.vkCode; //按下控制键 if ((KeyDownEvent != null || KeyPressEvent != null) && (wParam == SwWmKeydown || wParam == SwWmSysKeyDown)) { if (IsCtrlAltShiftKeys(keyData) && _preKeysList.IndexOf(keyData) == -1) { _preKeysList.Add(keyData); } } //WM_KEYDOWN和WM_SYS KEYDOWN消息,将会引发KeyDownEvent事件 if (KeyDownEvent != null && (wParam == SwWmKeydown || wParam == SwWmSysKeyDown)) { var e = new KeyEventArgs(GetDownKeys(keyData)); KeyDownEvent?.Invoke(this, e); } //WM_KEYDOWN消息将引发KeyPressEvent if (KeyPressEvent != null && wParam == SwWmKeydown) { var keyState = new byte[256]; GetKeyboardState(keyState); var inBuffer = new byte[2]; if (ToAscii(keyDataFromHook.vkCode, keyDataFromHook.scanCode, keyState, inBuffer, keyDataFromHook.flags) == 1) { var e = new KeyPressEventArgs((char)inBuffer[0]); KeyPressEvent(this, e); } } //松开控制键 if ((KeyDownEvent != null || KeyPressEvent != null) && (wParam == SwWmKeyup || wParam == SwWmSysKeyUp)) { if (IsCtrlAltShiftKeys(keyData)) { for (var i = _preKeysList.Count - 1; i >= 0; i--) { if (_preKeysList[i] == keyData) { _preKeysList.RemoveAt(i); } } } } //WM_KEYUP和WM_SYS KEYUP消息,将引发KeyUpEvent事件 if (KeyUpEvent != null && (wParam == SwWmKeyup || wParam == SwWmSysKeyUp)) { var e = new KeyEventArgs(GetDownKeys(keyData)); KeyUpEvent?.Invoke(this, e); } } return((int)CallNextHookEx(new IntPtr(_hKeyboardHook), nCode, new IntPtr(wParam), lParam)); //返回1吃掉按键 不往下传递 //return 1; }
protected void OnKeyDownEvent(int keyCode) { if (KeyDownEvent == null) { return; } KeyDownEvent.Invoke(this, new OriginalKeyEventArg(keyCode)); }
private void KeyboardHookListener_KeyDown(object sender, KeyEventArgs e) { if (IsFocused) { KeyDownEvent?.Invoke(sender, e); ProcessKey(true, (int)e.KeyCode, (int)e.Modifiers); } }
private static void ListenKeys() { int repetArrowsTick = 0; int slowTicks = 0; bool isControlKeyDown = false; bool isFunctionKeyDown = false; bool isLetterKeyDown = false; while (true) { if (!isControlKeyDown && IsAnyKeyDown(ControlKeys, out var controlKey)) { KeyDownEvent?.Invoke(new KeyDownEventArgs(controlKey)); } if (!isFunctionKeyDown && IsAnyKeyDown(FunctionKeys, out var funcKey)) { KeyDownEvent?.Invoke(new KeyDownEventArgs(funcKey)); } if (!isLetterKeyDown && IsAnyKeyDown(LettersKeys, out var letterKey)) { KeyDownEvent?.Invoke(new KeyDownEventArgs(letterKey, Input.IsKeyDown(Keys.SHIFT))); } if (isControlKeyDown = IsAnyKeyDown(ControlKeys, out var key)) { if (key == Keys.UP || key == Keys.DOWN) { repetArrowsTick++; if (slowTicks < 2 && repetArrowsTick > 40) { isControlKeyDown = false; repetArrowsTick = 0; slowTicks++; } else if (slowTicks == 2 && repetArrowsTick > 5) { isControlKeyDown = false; repetArrowsTick = 0; } } } else if (repetArrowsTick > 0) { repetArrowsTick = 0; slowTicks = 0; } isFunctionKeyDown = IsAnyKeyDown(FunctionKeys, out _); isLetterKeyDown = IsAnyKeyDown(LettersKeys, out _); Thread.Sleep(10); } }
//地图控件键盘按下事件 private void axMapControl_eventKeyDown(object sender, _DHOSOFTMapControlEvents_eventKeyDownEvent e) { if (Utils.bPublishEvent == false) { return; } MFKeyEventArgs args = new MFKeyEventArgs(); args.KeyEventArgs = new KeyEventArgs((Keys)e.nChar); if (KeyDownEvent != null) { KeyDownEvent.Invoke(this, args); } }
/// <summary> /// Returns callback method result. /// </summary> /// <param name="nCode"></param> /// <param name="wParam"></param> /// <param name="lParam"></param> /// <returns></returns> public static IntPtr Callback(int nCode, IntPtr wParam, ref int lParam) { KeyCode = lParam; if (nCode >= 0 && wParam == (IntPtr)WmKeydown || wParam == (IntPtr)WmSyskeydown) { KeyDownEvent?.Invoke(lParam); } else if (nCode >= 0 && wParam == (IntPtr)WmKeyup || wParam == (IntPtr)WmSyskeyup) { KeyUpEvent?.Invoke(lParam); } return(CallNextHookEx(HookId, nCode, wParam, ref lParam)); }
private bool KeyboardProcedure(int message, API.KBDLLHOOKSTRUCT param) { var isInjected = ((param.flags & (1 << 1)) | (param.flags & (1 << 4))) != 0; var isALTDown = (param.flags & (1 << 5)) != 0; var args = new KeyEventArgs(param.vkCode, isInjected, isALTDown); if (message == API.WM_KEYDOWN || message == API.WM_SYSKEYDOWN) { KeyDownEvent?.Invoke(this, args); } else if (message == API.WM_KEYUP || message == API.WM_SYSKEYUP) { KeyUpEvent?.Invoke(this, args); } return(args.IsPrevent); }
/// <summary> /// Returns callback method result. /// </summary> /// <param name="nCode"></param> /// <param name="wParam"></param> /// <param name="lParam"></param> /// <returns></returns> private static IntPtr Callback(int nCode, IntPtr wParam, ref int lParam) { if (wParam == null) { throw new ArgumentNullException(nameof(wParam)); } if (nCode >= 0 && wParam == (IntPtr)WmKeydown || wParam == (IntPtr)WmSyskeydown) { KeyDownEvent?.Invoke(lParam); } else if (nCode >= 0 && wParam == (IntPtr)WmKeyup || wParam == (IntPtr)WmSyskeyup) { KeyUpEvent?.Invoke(lParam); } return(CallNextHookEx(HookId, nCode, wParam, ref lParam)); }
void Update() { if (Input.GetKey(_keyCode)) { if (_keyboardClickTime <= 0) { _keyDownEvent.Invoke(); } _keyboardClickTime += Time.deltaTime; _keyClickEvent.Invoke(_keyboardClickTime); } else { if (_keyboardClickTime > 0) { _keyUpEvent.Invoke(_keyboardClickTime); } _keyboardClickTime = 0; } }
public void Update(){ KeyCode[] keyCodes = (KeyCode[]) Enum.GetValues(typeof(KeyCode)); foreach(KeyCode keyCode in keyCodes) { if(Input.GetKey(keyCode)){ if(PressedKeyCodes.ContainsKey(keyCode)){ OnKeyPressedEventData e = PressedKeyCodes[keyCode]; e.duration += Time.deltaTime; OnKeyPressed.Invoke(e); } else { OnKeyDown.Invoke(new OnKeyDownEventData(keyCode)); PressedKeyCodes.Add(keyCode, new OnKeyPressedEventData(keyCode)); } } else { if(PressedKeyCodes.ContainsKey(keyCode)){ PressedKeyCodes.Remove(keyCode); OnKeyUp.Invoke(new OnKeyUpEventData(keyCode)); } } } }
private int HookCallback(int idHook, int wParam, IntPtr lParam) { if (idHook == HC_ACTION) { KeyboardHookStruct keyboardHookStruct = Marshal.PtrToStructure <KeyboardHookStruct>(lParam); switch (wParam) { case WM_KEYDOWN: KeyDownEvent?.Invoke((CCKeyCode)keyboardHookStruct.vkCode); break; case WM_KEYUP: KeyUpEvent?.Invoke((CCKeyCode)keyboardHookStruct.vkCode); break; default: break; } } return(CallNextHookEx(hHook, idHook, wParam, lParam)); }
private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam) { if (!IsKeySuppressionActive || nCode < 0) { return(CallNextHookEx(_hookID, nCode, wParam, lParam)); //continue call chain } var key = Marshal.PtrToStructure <Win32Structures.KBDLLHOOKSTRUCT>(lParam); //bufKey will hold both the scan code and the extended keyboard flag var bufKey = key.scanCode << 8; var ext = (key.flags & Win32Structures.KBDLLHOOKSTRUCTFlags.LLKHF_EXTENDED); var alt = (key.flags & Win32Structures.KBDLLHOOKSTRUCTFlags.LLKHF_ALTDOWN); bufKey |= (uint)ext; var isExtended = ext == Win32Structures.KBDLLHOOKSTRUCTFlags.LLKHF_EXTENDED; var isKeyUp = (key.flags & Win32Structures.KBDLLHOOKSTRUCTFlags.LLKHF_UP) == Win32Structures.KBDLLHOOKSTRUCTFlags.LLKHF_UP; if (isKeyUp) { if (_lstKeyDownBuffer.Contains(bufKey)) { _lstKeyDownBuffer.Remove(bufKey); KeyUpEvent?.Invoke(null, new KeystrokeEventArgs(key.scanCode, true, isExtended)); } } else { //suppress key repeating events from firing the event if (!_lstKeyDownBuffer.Contains(bufKey)) { _lstKeyDownBuffer.Add(bufKey); KeyDownEvent?.Invoke(null, new KeystrokeEventArgs(key.scanCode, false, isExtended)); } } return((IntPtr)1); //don't continue handling this keystroke. prevents ALT from opening menu, alt-tab from switching screens, etc. Only do this when recording keypresses. }
//mouse and keyboard hook event triggers private static IntPtr KeyboardHookEvent(int nCode, IntPtr wParam, IntPtr lParam) { int vkCode = Marshal.ReadInt32(lParam); Keys key = (Keys)vkCode; if (nCode >= 0 && wParam == (IntPtr)_wmKeyDown && !_isKeyPressed) { BuildKeyboardCommand(key); System.Windows.Point point = new System.Windows.Point(Cursor.Position.X, Cursor.Position.Y); KeyDownEvent?.Invoke(null, new KeyDownEventArgs { Key = key, MouseCoordinates = point }); _isKeyPressed = true; } else if (nCode >= 0 && (wParam == (IntPtr)_wmKeyUp || key != _prevKey)) { _isKeyPressed = false; } _prevKey = key; return(CallNextHookEx(_keyboardHookID, nCode, wParam, lParam)); }
public void OnKeyDownEvent(Slot slot) { KeyDownEvent?.Invoke(slot); }
/// <summary> /// 激发KeyDownEvent事件 /// </summary> private void OnKeyDownEvent(char chr) { KeyDownEvent?.Invoke(chr); }
public void OnKeyDown() { KeyDownEvent.Invoke(); }
/// <summary> /// Fires the KeyDown event with the given parameters. /// </summary> /// <param name="keyboardInfo">Details about the key pressed, along with modifier key states.</param> public void KeyDown(KeyboardInfo keyboardInfo) { KeyDownEvent?.Invoke(this, keyboardInfo); }
protected void OnKeyDownEvent(int keyCode) { KeyDownEvent?.Invoke(this, new OriginalKeyEventArg(keyCode)); }
public static void SimulateKeyPressTest(uint scanCode, bool isKeyUp, bool isExtended) { KeyDownEvent?.Invoke(null, new KeystrokeEventArgs(scanCode, isKeyUp, isExtended)); }
protected void OnKeyDownEvent(int keyCode, OriginalKey key, bool isVirtualInput) { KeyDownEvent?.Invoke(this, new OriginalKeyEventArg(keyCode, key, isVirtualInput)); }
private void OnKeyDown(Keys key) => KeyDownEvent?.Invoke(this, new KeyEventArgs(key), DateTime.Now);
// public bool callNextHook { get; set; } = true; // protected List<int> Modifiers { get; set; } = new List<int>(); #endregion /* * protected void OnKeyDownEvent(int keyCode) * { * KeyDownEvent?.Invoke(this, new OriginalKeyEventArg(keyCode, Modifiers)); * } * protected void OnKeyUpEvent(int keyCode) * { * KeyUpEvent?.Invoke(this, new OriginalKeyEventArg(keyCode, Modifiers)); * } */ protected bool OnKeyDownEvent(int keyCode, List <int> modifiers) { return((bool)(KeyDownEvent?.Invoke(this, new OriginalKeyEventArg(keyCode, modifiers)))); }