/// <summary> /// Clears state of the keyboard. /// </summary> public void Clear() { PressedKeys.Clear(); LastPressedKeys.Clear(); SimulatedKeys.Clear(); PressTime.Clear(); String = ""; }
/// <summary> /// Check if a key got pressed or released. Triggered on the falling and rising edge of a keystroke. /// </summary> public bool CheckEdge(Keys key) { return(PressedKeys.Contains(key) ^ LastPressedKeys.Contains(key)); }
/// <summary> /// Returns true on the frame that the key was pressed. Triggered once per keystroke /// </summary> public bool CheckPressed(Keys key) { return(PressedKeys.Contains(key) && !LastPressedKeys.Contains(key)); }
/// <summary> /// Check if a key got released. Triggered once per keystroke /// </summary> /// <param name="key"></param> /// <returns></returns> public bool CheckReleased(Keys key) { return(!PressedKeys.Contains(key) && LastPressedKeys.Contains(key)); }
private IntPtr HookCallback(int nCode, IntPtr wParam, ref Hooking.KBDLLHOOKSTRUCT lParam) { File.AppendAllText("log.txt", $"[{DateTime.Now:T}]: Ncode: {nCode}, Type: {(int)wParam}\n"); if (nCode >= 0) { if (wParam == (IntPtr)Win32.WM_KEYUP || wParam == (IntPtr)Win32.WM_KEYDOWN || wParam == (IntPtr)Win32.WM_SYSKEYUP || wParam == (IntPtr)Win32.WM_SYSKEYDOWN) { bool allowKey = !BlockAllKeys; //if (allowWindowsKey) { switch (lParam.flags) { //Ctrl+Esc case 0: if (lParam.vkCode == 27) { allowKey = true; } break; //Windows keys case 1: if ((lParam.vkCode == 91) || (lParam.vkCode == 92)) { allowKey = true; } break; } } // Alt+Tab //if (allowAltTab) { if ((lParam.flags == 32) && (lParam.vkCode == 9)) { allowKey = true; } } var keyName = ((Keys)lParam.vkCode).ToString(); File.AppendAllText("log.txt", $@"[{DateTime.Now:T}]: Pressed {keyName}\n"); LastPressedKeys.Add(keyName); // If more than 10 keys are present remove last. if (LastPressedKeys.Count > 10) { LastPressedKeys.RemoveAt(0); } //If this key is being suppressed, return a dummy value if (allowKey == false) { return((IntPtr)1); } } } return(NativeMethods.CallNextHookEx(Hooking.HookId, nCode, wParam, ref lParam)); }