private void HookEx_Events(object sender, WindowsHookExEventArgs e) { if (KeyChange == null) { return; } KeyboardLLHookStruct @struct = (KeyboardLLHookStruct)Marshal.PtrToStructure((IntPtr)e.lParam, typeof(KeyboardLLHookStruct)); PressType pressType = PressType.None; VirtualKeys vk = (VirtualKeys)@struct.VirtualKeyCode; KeyboardState state = KeyboardState.FromCurrentState(); char?inputChar = null; if (e.wParam.ToInt64() == WM.KEYDOWN || e.wParam.ToInt64() == WM.SYSKEYDOWN) { pressType = PressType.KeyDown; } else if (e.wParam.ToInt64() == WM.KEYUP || e.wParam.ToInt64() == WM.SYSKEYUP) { pressType = PressType.KeyUp; } // Get Press Char char?PressKey = null; byte[] inBuffer = new byte[2]; if (User32.ToAscii(@struct.VirtualKeyCode, @struct.ScanCode, state.Bytes, inBuffer, @struct.Flags) == 1) { char ch = (char)inBuffer[0]; if (!char.IsControl(ch)) { PressKey = ch; if ((state.CapsLockToggled ^ state.ShiftPressed) && char.IsLetter(ch)) { PressKey = char.ToUpper(ch); } inputChar = PressKey; } } var args = new KeyChangeEventArgs() { Handled = false, PressType = pressType, Key = vk, KeyboardState = state, InputChar = inputChar, }; foreach (var action in KeyChange.GetInvocationList().Reverse()) { action.DynamicInvoke(this, args); if (args.Handled) { e.Handled = true; return; } } }
private int ProcMsg(int nCode, int wParam, IntPtr lParam) { if (Events != null) { WindowsHookExEventArgs e = new WindowsHookExEventArgs(HookType, nCode, wParam, lParam); foreach (var eve in Events.GetInvocationList().Reverse()) { eve.DynamicInvoke(this, e); if (e.Handled) { return(-1); } } } return(User32.CallNextHookEx(HookId, nCode, wParam, lParam)); }
private void HookEx_Events(object sender, WindowsHookExEventArgs e) { if (MouseChange == null) { return; } MouseLLHookStruct @struct = (MouseLLHookStruct)Marshal.PtrToStructure((IntPtr)e.lParam, typeof(MouseLLHookStruct)); PressType pressType = PressType.None; VirtualKeys vk = VirtualKeys.None; int click = 0; switch (e.wParam.ToInt64()) { // left case WM.LBUTTONDOWN: pressType = PressType.KeyDown; vk = VirtualKeys.LeftButton; click = 1; break; case WM.LBUTTONUP: pressType = PressType.KeyUp; vk = VirtualKeys.LeftButton; click = 1; break; case WM.LBUTTONDBLCLK: pressType = PressType.LeftButtonDoubleClick; break; // right case WM.RBUTTONDOWN: pressType = PressType.KeyDown; vk = VirtualKeys.RightButton; click = 1; break; case WM.RBUTTONUP: pressType = PressType.KeyUp; vk = VirtualKeys.RightButton; click = 1; break; case WM.RBUTTONDBLCLK: pressType = PressType.RightButtonDoubleClick; break; // middle case WM.MBUTTONDOWN: pressType = PressType.KeyDown; vk = VirtualKeys.MiddleButton; click = 1; break; case WM.MBUTTONUP: pressType = PressType.KeyUp; vk = VirtualKeys.MiddleButton; click = 1; break; case WM.MBUTTONDBLCLK: pressType = PressType.MiddleButtonDoubleClick; break; // wheel case WM.MOUSEWHEEL: //If the message is WM_MOUSEWHEEL, the high-order word of MouseData member is the wheel delta. //One wheel click is defined as WHEEL_DELTA, which is 120. //(value >> 16) & 0xffff; retrieves the high-order word from the given 32-bit value var mouseDelta = (short)((@struct.MouseData >> 16) & 0xffff); if (mouseDelta > 0) { pressType = PressType.WheelUp; } else { pressType = PressType.WheelDown; } break; //TODO: X BUTTONS (I havent them so was unable to test) //If the message is WM_XBUTTONDOWN, WM_XBUTTONUP, WM_XBUTTONDBLCLK, WM_NCXBUTTONDOWN, WM_NCXBUTTONUP, //or WM_NCXBUTTONDBLCLK, the high-order word specifies which X button was pressed or released, //and the low-order word is reserved. This value can be one or more of the following values. //Otherwise, MouseData is not used. } var arg = new MouseChangeEventArgs() { Handled = false, PressType = pressType, Key = vk, MouseClick = click, MousePosition = @struct.Point, }; if (LastMousePosX != @struct.Point.X || LastMousePosY != @struct.Point.Y) { LastMousePosX = @struct.Point.X; LastMousePosY = @struct.Point.Y; arg.MouseMoved = true; } foreach (var dele in MouseChange.GetInvocationList().Reverse()) { dele.DynamicInvoke(this, arg); if (arg.Handled) { e.Handled = true; return; } } }