public static void KeyUp(object sender, KeyEventArgs e) { if (PressedKeys.ContainsKey(e.KeyCode)) { PressedKeys[e.KeyCode] = false; } }
public static void PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) { if (PressedKeys.ContainsKey(e.KeyCode)) { e.IsInputKey = true; } }
public static void KeyDown(object sender, KeyEventArgs e) { if (PressedKeys.ContainsKey(e.KeyCode)) { PressedKeys[e.KeyCode] = true; OnKeyPressed(e.KeyCode); } }
private void OnKeyPressed(object sender, GlobalKeyboardHookEventArgs e) { // https://msdn.microsoft.com/en-us/library/windows/desktop/dd375731(v=vs.85).aspx // Remote Play not found if (CurrentProcess == null) { return; } // Check for active window var activeWindow = GetForegroundWindow(); if (activeWindow != IntPtr.Zero) { if (activeWindow != CurrentProcess.MainWindowHandle) { return; } } int vk = e.KeyboardData.VirtualCode; Keys key = (Keys)vk; //System.Diagnostics.Debug.WriteLine("KEY: " + vk); if (e.KeyboardState == GlobalKeyboardHook.KeyboardState.KeyDown) { //System.Diagnostics.Debug.WriteLine("KEY DOWN"); if (!PressedKeys.ContainsKey(key)) { PressedKeys.Add(key, true); } e.Handled = true; } else if (e.KeyboardState == GlobalKeyboardHook.KeyboardState.KeyUp) { //System.Diagnostics.Debug.WriteLine("KEY UP"); if (PressedKeys.ContainsKey(key)) { PressedKeys.Remove(key); } e.Handled = true; } }
public void OnKeyPressed(object sender, GlobalKeyboardHookEventArgs e) { // https://msdn.microsoft.com/en-us/library/windows/desktop/dd375731(v=vs.85).aspx if (!CheckFocusedWindow()) { return; } int vk = e.KeyboardData.VirtualCode; Keys key = (Keys)vk; // Key down if (e.KeyboardState == GlobalKeyboardHook.KeyboardState.KeyDown) { if (!PressedKeys.ContainsKey(key)) { PressedKeys.Add(key, true); ExecuteActionsByKey(PressedKeys.Keys.ToList()); } e.Handled = true; } // Key up else if (e.KeyboardState == GlobalKeyboardHook.KeyboardState.KeyUp) { if (PressedKeys.ContainsKey(key)) { PressedKeys.Remove(key); ExecuteActionsByKey(PressedKeys.Keys.ToList()); } e.Handled = true; } // Reset state if (!IsKeyDown()) { CurrentState = null; } }