protected void PowerEventHandlerThread() { Thread.BeginThreadAffinity(); try { _powerEventThreadId = NativeMethods.GetCurrentThreadId(); NativeMethods.WindowClass wndclass; wndclass.style = 0; wndclass.lpfnWndProc = PowerEventThreadWndProc; wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 0; wndclass.hInstance = Process.GetCurrentProcess().Handle; wndclass.hIcon = IntPtr.Zero; wndclass.hCursor = IntPtr.Zero; wndclass.hbrBackground = IntPtr.Zero; wndclass.lpszMenuName = null; wndclass.lpszClassName = "PowerEventHandlerThreadWndClass"; NativeMethods.RegisterClass(ref wndclass); _dummyWindow = NativeMethods.CreateWindowEx(0x80, wndclass.lpszClassName, "", 0x80000000, 0, 0, 0, 0, IntPtr.Zero, IntPtr.Zero, wndclass.hInstance, IntPtr.Zero); if (_dummyWindow.Equals(IntPtr.Zero)) { SafeLogD("PowerEventHandlerThread cannot create window handle, exiting thread"); return; } // This thread needs a message loop to handle power messages from Windows. SafeLogD("PowerEventHandlerThread message loop is running"); while (true) { try { NativeMethods.Message msgApi = new NativeMethods.Message(); if (!NativeMethods.GetMessageA(ref msgApi, IntPtr.Zero, 0, 0)) // returns false on WM_QUIT { return; } NativeMethods.TranslateMessage(ref msgApi); NativeMethods.DispatchMessageA(ref msgApi); } catch (Exception ex) { SafeLogE("PowerEventHandlerThread", ex); } } } finally { Thread.EndThreadAffinity(); SafeLogD("PowerEventHandlerThread finished"); } }
private static bool SendMessage(IntPtr hWnd, NativeMethods.Message msg, char key, UIntPtr lParam) { for (int cnt = 0; cnt < 4; cnt++) { if (NativeMethods.SendMessage(hWnd, (uint)msg, (IntPtr)key, lParam) != IntPtr.Zero) { continue; } return(true); } return(false); }
private static IntPtr HookProc(int nCode, NativeMethods.Message wParam, ref NativeMethods.MSLLHOOKSTRUCT lParam) { if (nCode >= 0 && wParam != NativeMethods.Message.WM_MOUSEMOVE) { Stop(); if (wParam == NativeMethods.Message.WM_LBUTTONDOWN) { Picked?.Invoke(PickWindowTitle(lParam.pt)); return((IntPtr)1); } } return(NativeMethods.CallNextHookEx(_hHook, nCode, wParam, ref lParam)); }
private static bool SendMessage(IntPtr hWnd, NativeMethods.Message msg, char key, UIntPtr lParam) { for (var cnt = 0; cnt < 4; cnt++) { UIntPtr result; if (NativeMethods.SendMessageTimeout(hWnd, (uint)msg, (IntPtr)key, lParam, NativeMethods.SendMessageTimeoutFlags.SMTO_ABORTIFHUNG, 5000, out result) == IntPtr.Zero || result != UIntPtr.Zero) { continue; } return(true); } return(false); }
/// <summary> /// フックチェーンにインストールするフックプロシージャ /// </summary> /// <param name="nCode">フックプロシージャに渡すフックコード</param> /// <param name="msg">フックプロシージャに渡す値</param> /// <param name="msllhookstruct">フックプロシージャに渡す値</param> /// <returns>フックチェーン内の次のフックプロシージャの戻り値</returns> private static System.IntPtr HookProcedure(int nCode, NativeMethods.Message msg, ref NativeMethods.MSLLHOOKSTRUCT s) { if (nCode >= 0 && HookEvent != null) { State.button = GetButtonState(msg, ref s); State.posX = s.pt.x; State.posY = s.pt.y; State.mouseData = s.mouseData; State.flags = s.flags; State.time = s.time; State.dwExtraInfo = s.dwExtraInfo; HookEvent(ref State); if (IsCancel) { IsCancel = false; return((System.IntPtr) 1); } } return(NativeMethods.CallNextHookEx(Handle, nCode, msg, ref s)); }
private static void PostMessage(IntPtr hWnd, NativeMethods.Message msg, char key, UIntPtr lParam) { NativeMethods.PostMessage(hWnd, (uint)msg, (IntPtr)key, lParam); }
private void HidEventHandlerThread() { Thread.BeginThreadAffinity(); try { _hidThreadId = NativeMethods.GetCurrentThreadId(); NativeMethods.WindowClass wndclass; wndclass.style = 0; wndclass.lpfnWndProc = HidEventThreadWndProc; wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 0; wndclass.hInstance = Process.GetCurrentProcess().Handle; wndclass.hIcon = IntPtr.Zero; wndclass.hCursor = IntPtr.Zero; wndclass.hbrBackground = IntPtr.Zero; wndclass.lpszMenuName = null; wndclass.lpszClassName = "HidEventHandlerThreadWndClass"; NativeMethods.RegisterClass(ref wndclass); _dummyWindow = NativeMethods.CreateWindowEx(0x80, wndclass.lpszClassName, "", 0x80000000, 0, 0, 0, 0, HWND_MESSAGE, IntPtr.Zero, wndclass.hInstance, IntPtr.Zero); if (_dummyWindow.Equals(IntPtr.Zero)) { ServiceRegistration.Get <ILogger>().Debug("HidEventHandlerThread cannot create window handle, exiting thread"); return; } SharpLib.Win32.RawInputDeviceFlags flags = SharpLib.Win32.RawInputDeviceFlags.RIDEV_INPUTSINK; IntPtr handle = _dummyWindow; List <RAWINPUTDEVICE> devices = new List <RAWINPUTDEVICE>(); devices.Add(new RAWINPUTDEVICE { usUsagePage = (ushort)SharpLib.Hid.UsagePage.WindowsMediaCenterRemoteControl, usUsage = (ushort)SharpLib.Hid.UsageCollection.WindowsMediaCenter.WindowsMediaCenterRemoteControl, dwFlags = flags, hwndTarget = handle }); devices.Add(new RAWINPUTDEVICE { usUsagePage = (ushort)SharpLib.Hid.UsagePage.Consumer, usUsage = (ushort)SharpLib.Hid.UsageCollection.Consumer.ConsumerControl, dwFlags = flags, hwndTarget = handle }); devices.Add(new RAWINPUTDEVICE { usUsagePage = (ushort)SharpLib.Hid.UsagePage.GenericDesktopControls, usUsage = (ushort)SharpLib.Hid.UsageCollection.GenericDesktop.Keyboard, dwFlags = flags, hwndTarget = handle }); _hidHandler = new SharpLib.Hid.Handler(devices.ToArray(), true, -1, -1); _hidHandler.OnHidEvent += new Handler.HidEventHandler(OnHidEvent); // This thread needs a message loop to handle power messages from Windows. ServiceRegistration.Get <ILogger>().Debug("HidEventHandlerThread message loop is running"); while (true) { try { NativeMethods.Message msgApi = new NativeMethods.Message(); if (!NativeMethods.GetMessageA(ref msgApi, IntPtr.Zero, 0, 0)) // returns false on WM_QUIT { return; } NativeMethods.TranslateMessage(ref msgApi); NativeMethods.DispatchMessageA(ref msgApi); } catch (Exception ex) { ServiceRegistration.Get <ILogger>().Error("HidEventHandlerThread", ex); } } } finally { Thread.EndThreadAffinity(); ServiceRegistration.Get <ILogger>().Debug("HidEventHandlerThread finished"); } }
public static extern System.IntPtr CallNextHookEx(System.IntPtr hhk, int nCode, NativeMethods.Message msg, ref MSLLHOOKSTRUCT msllhookstruct);
/// <summary> /// 操作されたボタンを取得します。 /// </summary> /// <param name="msg">マウスに関するウィンドウメッセージ</param> /// <param name="s">低レベルのマウスの入力イベントの構造体</param> /// <returns></returns> private static ButtonType GetButtonState(NativeMethods.Message msg, ref NativeMethods.MSLLHOOKSTRUCT s) { switch (msg) { case NativeMethods.Message.WM_MOUSEMOVE: return(ButtonType.MOVE); case NativeMethods.Message.WM_LBUTTONDOWN: return(ButtonType.L_DOWN); case NativeMethods.Message.WM_LBUTTONUP: return(ButtonType.L_UP); case NativeMethods.Message.WM_LBUTTONDBLCLK: return(ButtonType.UNKNOWN); case NativeMethods.Message.WM_RBUTTONDOWN: return(ButtonType.R_DOWN); case NativeMethods.Message.WM_RBUTTONUP: return(ButtonType.R_UP); case NativeMethods.Message.WM_MBUTTONDOWN: return(ButtonType.M_DOWN); case NativeMethods.Message.WM_MBUTTONUP: return(ButtonType.M_UP); case NativeMethods.Message.WM_MOUSEWHEEL: return(((short)((s.mouseData >> 16) & 0xffff) > 0) ? ButtonType.W_UP : ButtonType.W_DOWN); case NativeMethods.Message.WM_XBUTTONDOWN: switch (s.mouseData >> 16) { case 1: return(ButtonType.X1_DOWN); case 2: return(ButtonType.X2_DOWN); default: return(ButtonType.UNKNOWN); } case NativeMethods.Message.WM_XBUTTONUP: switch (s.mouseData >> 16) { case 1: return(ButtonType.X1_UP); case 2: return(ButtonType.X2_UP); default: return(ButtonType.UNKNOWN); } default: return(ButtonType.UNKNOWN); } }
private void DispatchThreadMessages() { var msg = new NativeMethods.Message(); while (NativeMethods.GetMessage(ref msg, (IntPtr)0, 0, 0) > 0) { switch (msg.Msg) { case NativeMethods.WM_HOTKEY: if (_registeredCount > 0) { //Notify the sibling processes as only one regitration per hot key is allowed PostThreadsMessageToSibling(msg.Msg, (IntPtr)msg.WParam, (IntPtr)msg.LParam); } EvalHotKey((uint)msg.LParam); // lParam: LowerInt16=modifiers, UpperInt16=vkCode break; case WM_USER_HOTKEY_REGISTER: if (NativeMethods.RegisterHotKey(IntPtr.Zero, _registeredCount + 1, (uint)msg.WParam, (uint)msg.LParam)) _registeredCount++; break; case WM_USER_HOTKEY_UNREGISTER_ALL: while (_registeredCount > 0) NativeMethods.UnregisterHotKey(IntPtr.Zero, _registeredCount--); break; } } }