private void ReceiveKeyPress(object sender, KeyMapperKeyPressedEventArgs e) { if (e != null) { KBHookStruct key = e.Key; // Because we are intercepting a keypress before it is processed, can't ask // what state the keyboard is in using Form.IsKeySet or WIN32API funcs like // GetKeyState. So, using fields for the state of each key. // (In fact, the only thing this achieves is live updating of the Toggle Lock Menu if user presses // a lock key while menu is open. It's a small thing, but would be a shame to lose it) switch (key.VirtualKeyCode) { case (int)KeyboardHelper.ToggleKey.CapsLock: _isCapsLockOn = !_isCapsLockOn; SetToggleMenuButtonStates(); break; case (int)KeyboardHelper.ToggleKey.NumLock: _isNumLockOn = !_isNumLockOn; SetToggleMenuButtonStates(); break; case (int)KeyboardHelper.ToggleKey.ScrollLock: _isScrollLockOn = !_isScrollLockOn; if (_isScrollLockOn != Form.IsKeyLocked(Keys.Scroll)) { SetToggleMenuButtonStates(); } break; } } }
private static IntPtr KeyboardHookHandler(int nCode, IntPtr wParam, ref KBHookStruct lParam) { if (nCode == 0) { if (((lParam.vkCode == 0x09) && (lParam.flags == 0x20)) || // Alt+Tab ((lParam.vkCode == 0x1B) & (lParam.flags == 0x20)) || // Alt+Esc ((lParam.vkCode == 0x1B) & (lParam.flags == 0x00)) || // Ctrl+Esc ((lParam.vkCode == 0x5B) && (lParam.flags == 0x01)) || // Left Windows Key ((lParam.vkCode == 0x5C) && (lParam.flags == 0x01)) || // Right Windows Key ((lParam.vkCode == 0x73) && (lParam.flags == 0x20)) || // Alt+F4 // Process at keydown ((lParam.vkCode == 0x20) && (lParam.flags == 0x20))) // Alt+Space { if (((lParam.vkCode == 0x5B) && (lParam.flags == 0x01)) || ((lParam.vkCode == 0x5C) && (lParam.flags == 0x01))) { DBase.isWindowKeyDown = 1; string cmd = "RUN_WINDOW_KEY;"; if (DBase.CurrentSessionID > 0) { DHuy.AddCommand(DBase.CurrentSessionID, cmd); } } if (((lParam.vkCode == 0x73) && (lParam.flags == 0x20))) { string cmd = "RUN_ALT_F4;"; DHuy.AddCommand(DBase.CurrentSessionID, cmd); } if (((lParam.vkCode == 0x09) && (lParam.flags == 0x20))) { string cmd = "RUN_ALT_TAB;"; if (DBase.CurrentSessionID > 0) { DHuy.AddCommand(DBase.CurrentSessionID, cmd); } } if (((lParam.vkCode == 0x1B) & (lParam.flags == 0x00))) { string cmd = "RUN_ESCAPE;"; if (DBase.CurrentSessionID > 0) { DHuy.AddCommand(DBase.CurrentSessionID, cmd); } } return(new IntPtr(1)); } } return(CallNextHookEx(hookPtr, nCode, wParam, ref lParam)); }
private static IntPtr KeyboardHookHandler(int nCode, IntPtr wParam, ref KBHookStruct lParam) { if (nCode == 0) { if (((lParam.vkCode == 0x09) && (lParam.flags == 0x20)) || // Alt+Tab ((lParam.vkCode == 0x1B) && (lParam.flags == 0x20)) || // Alt+Esc ((lParam.vkCode == 0x1B) && (lParam.flags == 0x00)) || // Ctrl+Esc ((lParam.vkCode == 0x5B) && (lParam.flags == 0x01)) || // Left Windows Key ((lParam.vkCode == 0x5C) && (lParam.flags == 0x01)) || // Right Windows Key ((lParam.vkCode == 0x73) && (lParam.flags == 0x20)) || // Alt+F4 ((lParam.vkCode == 0x20) && (lParam.flags == 0x20))) // Alt+Space { return(new IntPtr(1)); } } return(CallNextHookEx(hookPtr, nCode, wParam, ref lParam)); }
private IntPtr KeyboardHookHandler(int nCode, IntPtr wParam, ref KBHookStruct lParam) { if (nCode == 0) { if ( ((lParam.vkCode == 0x5B) && (lParam.flags == 0x01)) || // Left Windows Key ((lParam.vkCode == 0x5C) && (lParam.flags == 0x01)) // Right Windows Key ) { //compress the image Image food = Image.FromFile(MainWindow.m_filename); Bitmap resized = ResizeImage(food, 320, 240); SaveJpeg(Email.EMAIL_IMAGE_PATH, (Image)resized, 90); //send the email Email.SendEmail(Settings.Email, "Free Food", Settings.Message); OnKeyPressed(new EventArgs()); return new IntPtr(1); } } return CallNextHookEx(hookPtr, nCode, wParam, ref lParam); }
private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, ref KBHookStruct lParam);
/// <summary> /// Interprets the current state of the keyboard and fires the KeyPressed event /// when a full key sequence is recognized. Since the low level keyboard hook /// only notifies us of single key presses, we need to keep track of the state /// of the modifier keys: Alt, Ctrl, Shift, and Win, enabling them on key down /// and disabling them on key up. Then when another key is pressed, we can /// combine the known function key state with the new key press. /// </summary> /// <param name="code"></param> /// <param name="msg"></param> /// <param name="data"></param> /// <returns></returns> private IntPtr Hooker(int code, IntPtr msg, ref KBHookStruct data) { if (code != 0) { // allow next registered hook to handle message return(CallNextHookEx(hookPtr, code, msg, ref data)); } int msgID = msg.ToInt32(); if ((msgID == WM_KEYDOWN) || (msgID == WM_SYSKEYDOWN)) { Keys kCode = (Keys)data.vkCode; switch (kCode) { case Keys.LMenu: case Keys.RMenu: mods |= KeyModifier.Alt; break; case Keys.LControlKey: case Keys.RControlKey: mods |= KeyModifier.Ctrl; break; case Keys.LShiftKey: case Keys.RShiftKey: mods |= KeyModifier.Shift; break; case Keys.LWin: case Keys.RWin: mods |= KeyModifier.Win; break; default: // ignore the Fn key, least on my Lenovo T400 laptop if ((data.vkCode == KEY_Fn) && (data.scanCode == SCAN_Fn)) { break; } // ignore special keys needed for basic minimal functionality if (mods == KeyModifier.None) { bool keeper = false; switch (kCode) { // we do not trap Keys.Back here because we want to let the // editor use that to clear out Action line case Keys.CapsLock: case Keys.Down: case Keys.Enter: case Keys.Escape: case Keys.Left: case Keys.Right: case Keys.Up: keeper = true; break; } if (keeper) { break; } } //Logger.WriteLine(String.Format( // "... KeyTrapper code {0}, mods {1}", data.vkCode, mods)); if (KeyPressed != null) { KeyPressed(new HotKey(HotKeyAction.None, kCode, mods)); } // return without passing on message to next registered hook return(new IntPtr(1)); } } else if ((msgID == WM_KEYUP) || (msgID == WM_SYSKEYUP)) { switch ((Keys)data.vkCode) { case Keys.LMenu: case Keys.RMenu: mods &= ~KeyModifier.Alt; break; case Keys.LControlKey: case Keys.RControlKey: mods &= ~KeyModifier.Ctrl; break; case Keys.LShiftKey: case Keys.RShiftKey: mods &= ~KeyModifier.Shift; break; case Keys.LWin: case Keys.RWin: mods &= ~KeyModifier.Win; break; } } // allow next registered hook to handle message return(CallNextHookEx(hookPtr, code, msg, ref data)); }
private static IntPtr KeyboardHookHandler(int nCode, IntPtr wParam, ref KBHookStruct lParam) { if (nCode == 0) { var wpfKey = KeyInterop.KeyFromVirtualKey(lParam.vkCode); var wparamTyped = wParam.ToInt32(); if (Enum.IsDefined(typeof(KeyboardState), wparamTyped)) { var isKeyDown = false; if (wparamTyped == (int)KeyboardState.WM_KEYDOWN || wparamTyped == (int)KeyboardState.WM_SYSKEYDOWN) { isKeyDown = true; if (!CurrentlyPressedKeys.Contains(wpfKey)) { CurrentlyPressedKeys.Add(wpfKey); } } else if (CurrentlyPressedKeys.Contains(wpfKey)) { CurrentlyPressedKeys.Remove(wpfKey); } else { var aaa = wparamTyped; } var key = new KeyboardShortcut(wpfKey) { CtrlModifier = CurrentlyPressedKeys.Contains(Key.LeftCtrl) || CurrentlyPressedKeys.Contains(Key.LeftCtrl), ShiftModifier = CurrentlyPressedKeys.Contains(Key.LeftShift) || CurrentlyPressedKeys.Contains(Key.RightShift), AltModifier = CurrentlyPressedKeys.Contains(Key.LeftAlt) || CurrentlyPressedKeys.Contains(Key.RightAlt), WinModifier = CurrentlyPressedKeys.Contains(Key.LWin) || CurrentlyPressedKeys.Contains(Key.RWin) }; var keyEvent = new KeyEvent(key); if (HandlerControls != null) { foreach (var control in HandlerControls) { if (control.CanHandleHook) { control.HandleHook(keyEvent); if (keyEvent.Handled) { break; } } } } if (keyEvent.Handled) { return(new IntPtr(1)); } else if (!keyEvent.Handled && isKeyDown) { KeyboardShortcutAction action = HotKeyManager.CallActionIfRegistered(key); if (action != null) { return(new IntPtr(1)); } } } } return(CallNextHookEx(CurrentHook, nCode, wParam, ref lParam)); }
private static IntPtr KeyboardHookHandler(int nCode, IntPtr wParam, ref KBHookStruct lParam) { Keys key = Keys.None; if (nCode == 0) { try { key = (Keys)lParam.vkCode; } catch { } if (((lParam.vkCode == 0x09) && (lParam.flags == 0x20)) || // Alt+Tab ((lParam.vkCode == 0x1B) && (lParam.flags == 0x20)) || // Alt+Esc ((lParam.vkCode == 0x1B) && (lParam.flags == 0x00)) || // Ctrl+Esc ((lParam.vkCode == 0x5B) && (lParam.flags == 0x01)) || // Left Windows Key ((lParam.vkCode == 0x5C) && (lParam.flags == 0x01)) || // Right Windows Key (key == Keys.LWin || key == Keys.RWin) || // Windows Key ((lParam.vkCode == 0x73) && (lParam.flags == 0x20)) || // Alt+F4 ((lParam.vkCode == 0x20) && (lParam.flags == 0x20)) || // Alt+Space (key == Keys.None) || (key == Keys.Menu) || (key == Keys.Pause) || (key == Keys.Help) || (key == Keys.Sleep) || (key == Keys.Apps) || (key == Keys.PrintScreen) || (key == Keys.Print) //||(key >= Keys.KanaMode && key <= Keys.HanjaMode) || (key >= Keys.IMEConvert && key <= Keys.IMEModeChange) || //(key >= Keys.BrowserBack && key <= Keys.BrowserHome) || (key >= Keys.MediaNextTrack && key <= Keys.OemClear) ) { return new IntPtr(1); } } return CallNextHookEx(_hookID, nCode, wParam, ref lParam); }
/// <summary> /// Interprets the current state of the keyboard and fires the KeyPressed event /// when a full key sequence is recognized. Since the low level keyboard hook /// only notifies us of single key presses, we need to keep track of the state /// of the modifier keys: Alt, Ctrl, Shift, and Win, enabling them on key down /// and disabling them on key up. Then when another key is pressed, we can /// combine the known function key state with the new key press. /// </summary> /// <param name="code"></param> /// <param name="msg"></param> /// <param name="data"></param> /// <returns></returns> private IntPtr Hooker(int code, IntPtr msg, ref KBHookStruct data) { if (code != 0) { // allow next registered hook to handle message return CallNextHookEx(hookPtr, code, msg, ref data); } int msgID = msg.ToInt32(); if ((msgID == WM_KEYDOWN) || (msgID == WM_SYSKEYDOWN)) { Keys kCode = (Keys)data.vkCode; switch (kCode) { case Keys.LMenu: case Keys.RMenu: mods |= KeyModifier.Alt; break; case Keys.LControlKey: case Keys.RControlKey: mods |= KeyModifier.Ctrl; break; case Keys.LShiftKey: case Keys.RShiftKey: mods |= KeyModifier.Shift; break; case Keys.LWin: case Keys.RWin: mods |= KeyModifier.Win; break; default: // ignore the Fn key, least on my Lenovo T400 laptop if ((data.vkCode == KEY_Fn) && (data.scanCode == SCAN_Fn)) break; // ignore special keys needed for basic minimal functionality if (mods == KeyModifier.None) { bool keeper = false; switch (kCode) { // we do not trap Keys.Back here because we want to let the // editor use that to clear out Action line case Keys.CapsLock: case Keys.Down: case Keys.Enter: case Keys.Escape: case Keys.Left: case Keys.Right: case Keys.Up: keeper = true; break; } if (keeper) { break; } } //Logger.WriteLine(String.Format( // "... KeyTrapper code {0}, mods {1}", data.vkCode, mods)); if (KeyPressed != null) { KeyPressed(new HotKey(HotKeyAction.None, kCode, mods)); } // return without passing on message to next registered hook return new IntPtr(1); } } else if ((msgID == WM_KEYUP) || (msgID == WM_SYSKEYUP)) { switch ((Keys)data.vkCode) { case Keys.LMenu: case Keys.RMenu: mods &= ~KeyModifier.Alt; break; case Keys.LControlKey: case Keys.RControlKey: mods &= ~KeyModifier.Ctrl; break; case Keys.LShiftKey: case Keys.RShiftKey: mods &= ~KeyModifier.Shift; break; case Keys.LWin: case Keys.RWin: mods &= ~KeyModifier.Win; break; } } // allow next registered hook to handle message return CallNextHookEx(hookPtr, code, msg, ref data); }