private IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam) { if (nCode >= 0 && (wParam == (IntPtr)WM_KEYDOWN || wParam == (IntPtr)WM_SYSKEYDOWN)) { // Cast lParam into our structure KBHookStruct keypress = (KBHookStruct)Marshal.PtrToStructure(lParam, typeof(KBHookStruct)); // Console.WriteLine("ScanCode: {0}, Extended: {1}, KeyCode: {2}, Name: {3}", // keypress.Scancode, keypress.Extended, keypress.VirtualKeyCode, AppController.GetKeyName(keypress.Scancode, keypress.Extended)); if (keypress.Scancode == 541) { // Right Alt, at least on my Dell SK-8115 keyboard // Console.WriteLine("Fixing Dell's Right Alt keyboard bug"); keypress.Scancode = 56; keypress.KeyFlags = 1; } if (keypress.VirtualKeyCode == 19) { // Pause. This doesn't capture well - it's extended value is 225 // rather than 224, so keypress.Scancode = 29; keypress.KeyFlags = 2; } // Some keyboards report Num Lock as having the extended bit set // on keypress, but that doesn't work in a mapping. if (keypress.Scancode == 69 && keypress.Extended == 224) { // The Keyboard lies. keypress.Extended = 0; } // Raise the event: if (KeyPressed != null) { KeyMapperKeyPressedEventArgs e = new KeyMapperKeyPressedEventArgs(keypress); KeyPressed(new object(), e); } if (_suppress) { // Return 1 to suppress the keypress. return((IntPtr)1); } } return(NativeMethods.CallNextHookEx(_hookID, nCode, wParam, lParam)); }
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 IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam) { if (nCode >= 0 && (wParam == (IntPtr)WM_KEYDOWN || wParam == (IntPtr)WM_SYSKEYDOWN)) { // Cast lParam into our structure KBHookStruct keypress = (KBHookStruct)Marshal.PtrToStructure(lParam, typeof(KBHookStruct)); // Console.WriteLine("ScanCode: {0}, Extended: {1}, KeyCode: {2}, Name: {3}", // keypress.Scancode, keypress.Extended, keypress.VirtualKeyCode, AppController.GetKeyName(keypress.Scancode, keypress.Extended)); if (keypress.Scancode == 541) { // Right Alt, at least on my Dell SK-8115 keyboard // Console.WriteLine("Fixing Dell's Right Alt keyboard bug"); keypress.Scancode = 56; keypress.KeyFlags = 1; } if (keypress.VirtualKeyCode == 19) { // Pause. This doesn't capture well - it's extended value is 225 // rather than 224, so keypress.Scancode = 29; keypress.KeyFlags = 2; } // Some keyboards report Num Lock as having the extended bit set // on keypress, but that doesn't work in a mapping. if (keypress.Scancode == 69 && keypress.Extended == 224) { // The Keyboard lies. keypress.Extended = 0; } // Raise the event: if (KeyPressed != null) { KeyMapperKeyPressedEventArgs e = new KeyMapperKeyPressedEventArgs(keypress); KeyPressed(new object(), e); } if (_suppress) { // Return 1 to suppress the keypress. return (IntPtr)1; } } return NativeMethods.CallNextHookEx(_hookID, nCode, wParam, lParam); }
private void OnKeyPress(object sender, KeyMapperKeyPressedEventArgs e) { int scancode = e.Key.Scancode; int extended = e.Key.Extended; if (capturingFromKey) { // Have we been sent a dud?? if (scancode == 0) { // Can't use a disabled key as From map = new KeyMapping(); } else { SetMapToBlankMapping(scancode, extended); } } else { // Can't tell from the passed key whether it's mapped or not as // if it is, we get the mapped scancode and have no way of telling // what the original key was (it's possible 2 keys could be mapped to the // same key, meaning can't just do a reverse lookup.) // So, mapping to a mapped key is de facto allowed. map = new KeyMapping(map.From, new Key(scancode, extended)); } SetupForm(); }