private void InitializeHookThread() { lock (_lockObject) { _hookProc = HookProcedure; var methodPtr = Marshal.GetFunctionPointerForDelegate(_hookProc); _hookHandler = User32.SetWindowsHookEx((int)WindowsHookType, methodPtr, MainModuleHandle, 0); _hookThreadId = Kernel32.GetCurrentThreadId(); } if (_hookHandler == IntPtr.Zero) { return; } var msg = new Message(); while (User32.GetMessage(ref msg, IntPtr.Zero, 0, 0) != 0) { if (msg.Msg == (uint)WindowsMessage.Quit) { break; } } User32.UnhookWindowsHookEx(_hookHandler); }
static HotkeyManager() { hkCnt = 0; hHook = IntPtr.Zero; hotkeys = new Dictionary <int, DynamicHotkey>(); hookProc = new User32.HookProc(EvtDispatcher); }
private void Dispose(bool disposing) { if (disposing) { KeyboardHookAction -= LowLevelKeyboardAction; MouseHookAction -= LowLevelMouseAction; WindowHookAction -= WindowChangedAction; if (KeyboardHookHandle != IntPtr.Zero && !User32.UnhookWindowsHookEx(KeyboardHookHandle)) { throw new Win32Exception(Marshal.GetLastWin32Error()); } if (MouseHookHandle != IntPtr.Zero && !User32.UnhookWindowsHookEx(MouseHookHandle)) { throw new Win32Exception(Marshal.GetLastWin32Error()); } if (WindowHookHandle != IntPtr.Zero && !User32.UnhookWinEvent(WindowHookHandle)) { throw new Win32Exception(Marshal.GetLastWin32Error()); } if (User32Handle != IntPtr.Zero && !Kernel32.FreeLibrary(User32Handle)) { throw new Win32Exception(Marshal.GetLastWin32Error()); } } }
private static void SubscribeInner(KeyboardListener instance) { if (Subscribed.Contains(instance)) { return; } Subscribed.Add(instance); if (_hookHandle == 0) { // ReSharper disable once RedundantDelegateCreation _hookProc = new User32.HookProc(KeyboardHookProc); // var id = Kernel32.LoadLibrary("User32"); var id = Kernel32.GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName); var result = User32.SetWindowsHookEx(User32.WH_KEYBOARD_LL, _hookProc, id, 0); _hookHandle = result; AcToolsLogging.Write("Subscribed: " + result); if (result == 0) { throw new Win32Exception(Marshal.GetLastWin32Error()); } } }
private void InitializeHookThread() { lock (lockObject) { hookThreadId = Kernel32.GetCurrentThreadId(); hookProc = new User32.HookProc(HookProcedure); IntPtr methodPtr = Marshal.GetFunctionPointerForDelegate(hookProc); hookHandle = User32.SetWindowsHookEx((int)WindowsHookType, methodPtr, MainModuleHandle, 0); } Message msg = new Message(); while (User32.GetMessage(ref msg, IntPtr.Zero, 0, 0) != 0) { if (msg.Msg == (uint)WindowsMessage.WM_QUIT) { break; } } User32.UnhookWindowsHookEx(hookHandle); }
private KeyboardTrigger(KeyAction onKeyAction) { _onKeyAction = onKeyAction; var module = Process.GetCurrentProcess().MainModule ?? throw new StateException("MainModule of process is null"); _hModule = Kernel32.GetModuleHandle(module.ModuleName); _hookProc = KeyboardHookProc; }
private void EnableHook() { var hInstance = Kernel32.LoadLibrary("User32"); _hookProc = HookProc; _hook = User32.SetWindowsHookEx(User32.HookType.WH_KEYBOARD_LL, _hookProc, hInstance, 0); }
private static IntPtr SetHook(User32.HookProc proc) { using (Process curProcess = Process.GetCurrentProcess()) using (ProcessModule curModule = curProcess.MainModule) { return(User32.SetWindowsHookEx(WH_KEYBOARD_LL, proc, Kernel32.GetModuleHandle(curModule.ModuleName), 0)); } }
public MouseHook() { callbackDelegate = new User32.HookProc(ReceiveCallback); IntPtr hModule; using (var p = Process.GetCurrentProcess()) using (var m = p.MainModule) hModule = Kernel32.GetModuleHandle(m.ModuleName); hook = User32.SetWindowsHookEx(HookType.WH_MOUSE_LL, callbackDelegate, hModule, 0); }
public MouseHook(MouseHookListener listener) { _listener = listener; _hookProc = HookCallback; // if an instance-level delegate is not used, windows will silently remove the hook... using (Process currentProcess = Process.GetCurrentProcess()) using (ProcessModule currentModule = currentProcess.MainModule) { _hook = User32.SetWindowsHookEx(WH_MOUSE_LL, _hookProc, Kernel32.GetModuleHandle(currentModule.ModuleName), 0); } }
private IntPtr SetHook(int hookNum, User32.HookProc proc) { using var curProcess = Process.GetCurrentProcess(); using var curModule = curProcess.MainModule; if (_thisModHandle == IntPtr.Zero) { _thisModHandle = Kernel32.GetModuleHandle(curModule.ModuleName); } return(User32.SetWindowsHookEx(hookNum, proc, _thisModHandle, 0)); }
private static void UnsubscribeInner(KeyboardListener instance) { if (Subscribed.Remove(instance) && Subscribed.Count == 0 && _hookHandle != 0) { var result = User32.UnhookWindowsHookEx(_hookHandle); _hookHandle = 0; _hookProc = null; if (result == 0) { throw new Win32Exception(Marshal.GetLastWin32Error()); } } }
/// <summary> /// Places a low-level mouse hook on the desktop session. /// </summary> /// <remarks> /// The thread that registers this hook must pump window messages! /// /// SafeHHOOK will automatically call UnhookWindowsHookEx on dispose! /// This is possible because UnhookWindowsHook can be called from any thread. /// </remarks> /// <param name="hookFunc">The function to invoke</param> /// <param name="runner">The (message pumping) thread controller to register the hook on</param> /// <returns>Task that completes with a safe handle for the allocated resources.</returns> /// <seealso cref="MouseHook"/> public static Task <User32.SafeHHOOK> HookLowLevelMouse(User32.HookProc hookFunc, Dispatcher runner) { if (hookFunc is null) { throw new ArgumentNullException(nameof(hookFunc)); } if (runner is null) { throw new ArgumentNullException(nameof(runner)); } User32.SafeHHOOK Unsafe() { var hookResult = User32.SetWindowsHookEx(User32.HookType.WH_MOUSE_LL, hookFunc, default, 0);
private void Window_Loaded(object sender, RoutedEventArgs e) { _locked = false; _hookProc = new User32.HookProc(Window_HookProc); _hookPtr = User32.SetWindowsHookEx(13 /*WH_KEYBOARD_LL*/, _hookProc, User32.GetModuleHandle(null), 0); _random = new Random(); _speech = new SpeechSynthesizer(); Left = SystemParameters.WorkArea.Right - Width; Top = SystemParameters.WorkArea.Bottom - Height; //Lock(); }
public void init() { mapVCtlKey.Clear(); mapVCtlKey[162] = (byte)Key.LeftCtrl << 24; mapVCtlKey[163] = (byte)Key.RightCtrl << 24; mapVCtlKey[160] = (byte)Key.LeftShift << 16; mapVCtlKey[161] = (byte)Key.LeftShift << 16; mapVCtlKey[164] = (byte)Key.LeftAlt << 8; mapVCtlKey[165] = (byte)Key.LeftAlt << 8; hKeyHookProc = new User32.HookProc(keyHookProc); IntPtr hModel = Kernel32.GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName); hKeyboardHook = User32.SetWindowsHookEx(HookType.WH_KEYBOARD_LL, hKeyHookProc, hModel, 0); }
private static void SubscribeInner(KeyboardListener instance) { if (Subscribed.Contains(instance)) return; Subscribed.Add(instance); if (_hookHandle == 0) { // ReSharper disable once RedundantDelegateCreation _hookProc = new User32.HookProc(KeyboardHookProc); // var id = Kernel32.LoadLibrary("User32"); var id = Kernel32.GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName); var result = User32.SetWindowsHookEx(User32.WH_KEYBOARD_LL, _hookProc, id, 0); _hookHandle = result; AcToolsLogging.Write("Subscribed: " + result); if (result == 0) throw new Win32Exception(Marshal.GetLastWin32Error()); } }
public GlobalHook() { KeyboardHookAction = LowLevelKeyboardAction; MouseHookAction = LowLevelMouseAction; WindowHookAction = WindowChangedAction; KeyboardHookHandle = IntPtr.Zero; MouseHookHandle = IntPtr.Zero; WindowHookHandle = IntPtr.Zero; // load User32 if ((User32Handle = Kernel32.LoadLibrary("User32")) == IntPtr.Zero) { throw new Win32Exception(Marshal.GetLastWin32Error()); } KeyBinding.Load(); }
private static void SubscribeInner(KeyboardListener instance) { if (Subscribed.Contains(instance)) { return; } Subscribed.Add(instance); if (_hookHandle == 0) { // ReSharper disable once RedundantDelegateCreation // Have to create that delegate to avoid it being sweeped by GC, I guess. _hookProc = new User32.HookProc(KeyboardHookProc); _hookHandle = User32.SetWindowsHookEx(User32.WH_KEYBOARD_LL, _hookProc, Kernel32.GetModuleHandle(Process.GetCurrentProcess().MainModule?.ModuleName), 0); if (_hookHandle == 0) { throw new Win32Exception(Marshal.GetLastWin32Error()); } } }
public void Run() { // DPI Awareness API is not available on older OS's, but they work in // physical pixels anyway, so we just ignore if the call fails. try { SHCore.SetProcessDpiAwareness(PROCESS_DPI_AWARENESS.Process_Per_Monitor_DPI_Aware); } catch (DllNotFoundException) { Debug.WriteLine("No SHCore.DLL. No problem."); } // Make sure we catch CTRL-C hard-exit of program. _ctrlCHandler = new Kernel32.ConsoleEventDelegate(ConsoleEventCallback); Kernel32.SetConsoleCtrlHandler(_ctrlCHandler, true); Debug.WriteLine(_screenSet = new ScreenSet()); // Get notified of any screen configuration changes. SystemEvents.DisplaySettingsChanged += Event_DisplaySettingsChanged; //User32.ShowWindow(Kernel32.GetConsoleWindow(), User32.SW_HIDE); //--Keep a reference to the delegate, so it does not get garbage collected. _mouseHookDelegate = LLMouseHookCallback; _llMouseHookhand = SetHook(User32.WH_MOUSE_LL, _mouseHookDelegate); Debug.WriteLine(""); // This is the one that runs "forever" while the application is alive, and handles // events, etc. This application is ABSOLUTELY ENTIRELY driven by the LLMouseHook // and DisplaySettingsChanged events. System.Windows.Forms.Application.Run(); Debug.WriteLine("Exiting!!!"); UnsetHook(ref _llMouseHookhand); }
public KeyboardHook(VBE vbe) { _vbe = vbe; _callback = HookCallback; }
public MouseHook(VBE vbe) { _vbe = vbe; _callback = HookCallback; }
private static void UnsubscribeInner(KeyboardListener instance) { if (Subscribed.Remove(instance) && Subscribed.Count == 0 && _hookHandle != 0) { var result = User32.UnhookWindowsHookEx(_hookHandle); _hookHandle = 0; _hookProc = null; AcToolsLogging.Write("Unsubscribed: " + result); if (result == 0) throw new Win32Exception(Marshal.GetLastWin32Error()); } }
private KeyListener() { dispatcher = Dispatcher.CurrentDispatcher; staticHookCallback = new User32.HookProc(HookCallback); Process curProcess = Process.GetCurrentProcess(); ProcessModule curModule = curProcess.MainModule; hookPointer = User32.SetWindowsHookEx(User32.WH_KEYBOARD_LL, staticHookCallback, Kernel32.GetModuleHandle(curModule.ModuleName), 0); }
/// <summary> /// Constructor /// </summary> /// <para> /// Initialises the members and registers the hook. /// </para> public HookHandler() { mModifierSemaphore = new Semaphore(1, 1); mModifierStates = new ModifierStates[(uint)ModifierIndexes.Count]; for (uint i = 0; i < (uint)ModifierIndexes.Count; i++) mModifierStates[i] = ModifierStates.None; mKeyCombinationsMutex = new Mutex(); mKeyCombinations = new int[ShortcutData.getKeyCodeCount() * 256]; reset(); mHookCallbackHandle = new User32.HookProc(keyboardLowLevelHookCallback); mHookHandle = User32.SetWindowsHookEx(User32.HookType.WH_KEYBOARD_LL, mHookCallbackHandle, IntPtr.Zero, 0); if (mHookHandle == IntPtr.Zero) { log.ErrorFormat("SetWindowsHookEx error. Code: {0}", User32.GetLastError()); throw new ApplicationException("Installing hook failed. See previous message for the code"); } }
public ZalgoService() { InitializeComponent(); _llKeyboardProc = LLKeyboardProc; }