private void OnUnicodeChar() { if (KeyUnicode != null) { string s = string.Empty; int integerCode; foreach (VirtualKeyCode vk in numpadDigitsKeys) { s += (vk - VirtualKeyCode.NUMPAD0); } if (Int32.TryParse(s, out integerCode)) { Console.WriteLine("UNICODE: code= " + integerCode); KEYBDINPUT down = new KEYBDINPUT { wVk = 0, wScan = (ushort)integerCode, dwFlags = KEYEVENTF_UNICODE, time = 0, dwExtraInfo = IntPtr.Zero }; KEYBDINPUT up = new KEYBDINPUT { wVk = 0, wScan = (ushort)integerCode, dwFlags = (KEYEVENTF_KEYUP | KEYEVENTF_UNICODE), time = 0, dwExtraInfo = IntPtr.Zero }; if ((integerCode & 0xFF00) == 0xE000) { up.dwFlags |= KEYEVENTF_EXTENDEDKEY; down.dwFlags |= KEYEVENTF_EXTENDEDKEY; } INPUT[] inputs = new INPUT[] { new INPUT { dwType = (Int32)InputType.Keyboard, ki = down }, new INPUT { dwType = (Int32)InputType.Keyboard, ki = up } }; KeyUnicode(this, inputs); } } }
private IntPtr HookCallback(int nCode, IntPtr wParam, ref KEYBDINPUT lParam) { if (nCode >= 0) { VirtualKeyCode code = (VirtualKeyCode)lParam.wVk; INPUT input = new INPUT { dwType = (Int32)InputType.Keyboard, ki = lParam }; int message = (int)wParam; if (message == WM_KEYDOWN || message == WM_SYSKEYDOWN) { if (code == VirtualKeyCode.MENU || code == VirtualKeyCode.LMENU || code == VirtualKeyCode.RMENU) { menuDown = true; } else if (code >= VirtualKeyCode.NUMPAD0 && code <= VirtualKeyCode.NUMPAD9 && menuDown) { if (code != downKeys[downKeys.Count - 1]) { numpadDigitsKeys.Add(code); } } else { numpadDigitsKeys.Clear(); menuDown = false; } OnKeyDown(input); if (!downKeys.Contains(code)) { downKeys.Add(code); } } else if (message == WM_KEYUP || message == WM_SYSKEYUP) { OnKeyUp(input); if (downKeys.Count > 1) { OnKeyShortcut(); } if (downKeys.Contains(code)) { downKeys.Remove(code); } if (code == VirtualKeyCode.MENU || code == VirtualKeyCode.LMENU || code == VirtualKeyCode.RMENU) { OnUnicodeChar(); numpadDigitsKeys.Clear(); menuDown = false; } } } int altDown = Win32.GetKeyState((int)VirtualKeyCode.MENU); bool isAltDown = altDown == -127 || altDown == -128; if (isAltDown && lParam.wVk == 9) { Console.WriteLine("alt tab"); return(new IntPtr(-1)); } int ctrlDown = Win32.GetKeyState((int)VirtualKeyCode.LCONTROL); bool isCtrlDown = ctrlDown == -127 || ctrlDown == -128; if (isCtrlDown && isAltDown && lParam.wVk == (int)VirtualKeyCode.DELETE) { Console.WriteLine("ctrl alt canc"); return(new IntPtr(-1)); } if (isAltDown && lParam.wVk == (int)VirtualKeyCode.F4) { Console.WriteLine("alt F4"); return(new IntPtr(-1)); } return(CallNextHookEx(hookPtr, nCode, wParam, ref lParam)); }
public static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, ref KEYBDINPUT lParam);