protected override void WndProc(ref Message m) { switch ((EnumWinMsgs)m.Msg) { case EnumWinMsgs.WM_DRAWCLIPBOARD: GetClipboardData(); DllUser32.SendMessage(_clipboardViewerNext, m.Msg, m.WParam, m.LParam); break; case EnumWinMsgs.WM_CHANGECBCHAIN: if (m.WParam == _clipboardViewerNext) { _clipboardViewerNext = m.LParam; } else { DllUser32.SendMessage(_clipboardViewerNext, m.Msg, m.WParam, m.LParam); } break; default: base.WndProc(ref m); break; } }
internal static IntPtr SetHook(LowLevelKeyboardProc proc) { using (var curProcess = Process.GetCurrentProcess()) using (var curModule = curProcess.MainModule) { return(DllUser32.SetWindowsHookEx(WH_KEYBOARD_LL, proc, DllUser32.GetModuleHandle(curModule.ModuleName), 0)); } }
private static int GetWindowLong(IntPtr hWnd, int nIndex) { if (IntPtr.Size == 4) { return(DllUser32.GetWindowLong(hWnd, nIndex)); } return(DllUser32.GetWindowLongPtr(hWnd, nIndex)); }
internal ShellEventHook(IntPtr hWnd) { var cp = new CreateParams(); CreateHandle(cp); DllUser32.SetTaskmanWindow(hWnd); if (DllUser32.RegisterShellHookWindow(Handle)) { _wmShellHook = DllUser32.RegisterWindowMessage("SHELLHOOK"); } }
private static IntPtr SetHook(LowLevelMouseProc mouseProc) { var hook = DllUser32.SetWindowsHookEx(WH_MOUSE_LL, mouseProc, DllUser32.GetModuleHandle("user32"), 0); if (hook == IntPtr.Zero) { throw new Win32Exception(); } return(hook); }
internal static IntPtr GetActiveWindowHandle() { try { return((IntPtr)DllUser32.GetForegroundWindow()); } catch (Exception) { // ~ } return(IntPtr.Zero); }
private static void ClearKeyboardBuffer(uint vk, uint sc, IntPtr hkl) { var sb = new StringBuilder(10); int rc; do { var lpKeyStateNull = new byte[255]; rc = DllUser32.ToUnicodeEx(vk, sc, lpKeyStateNull, sb, sb.Capacity, 0, hkl); } while (rc < 0); }
IntPtr SetHook(EnumWindowEvent @event) { IntPtr hookId = DllUser32.SetWinEventHook( hookMin: @event, hookMax: @event, moduleHandle: IntPtr.Zero, callback: _eventProc, processId: 0, threadId: 0, flags: EnumHookFlags.OUT_OF_CONTEXT); if (hookId == IntPtr.Zero) { throw new Win32Exception(); } return(hookId); }
internal static string GetWindowText(IntPtr hWnd) { try { int length = DllUser32.GetWindowTextLength(hWnd); var sb = new StringBuilder(length + 1); DllUser32.GetWindowText(hWnd, sb, sb.Capacity); return(sb.ToString()); } catch (Exception) { return("err-get-TxtWin"); } }
private IntPtr HookCallBack(int nCode, IntPtr wParam, IntPtr lParam) { StructMSLLHOOKSRTUCT hookStruct; if (nCode < 0) { return(DllUser32.CallNextHookEx(_hookId, nCode, wParam, lParam)); } hookStruct = (StructMSLLHOOKSRTUCT)Marshal.PtrToStructure(lParam, typeof(StructMSLLHOOKSRTUCT)); MouseAction(null, new RawMouseEventArgs { MouseMessage = (EnumWinMsgs)wParam, Point = hookStruct.pt, MouseInfo = hookStruct.mouseData }); return(DllUser32.CallNextHookEx(_hookId, nCode, wParam, lParam)); }
void EventRemove(ref EventHandler <WindowEventArgs> handler, EventHandler <WindowEventArgs> user, EnumWindowEvent @event) { lock (_hooksDictionary) { if (handler != null && handler - user == null) { if (!DllUser32.UnhookWinEvent(_hooksDictionary[@event])) { throw new Win32Exception(); } bool existed = _hooksDictionary.Remove(@event); Debug.Assert(existed); } handler -= user; } }
internal static string GetAppPath(IntPtr hWnd) { if (hWnd == IntPtr.Zero) { return("empty handle"); } try { uint pid; DllUser32.GetWindowThreadProcessId(hWnd, out pid); var proc = Process.GetProcessById((int)pid); return(proc.MainModule.FileName); } catch (Exception ex) { return("empty handle"); } }
private IntPtr LowLevelKeyboardProc(int nCode, UIntPtr wParam, IntPtr lParam) { if (nCode >= 0) { if (wParam.ToUInt32() == (int)Enums.EnumKeyEvent.WM_KEYDOWN || wParam.ToUInt32() == (int)Enums.EnumKeyEvent.WM_KEYUP || wParam.ToUInt32() == (int)Enums.EnumKeyEvent.WM_SYSKEYDOWN || wParam.ToUInt32() == (int)Enums.EnumKeyEvent.WM_SYSKEYUP) { string chars = InterceptKeys.VkCodeToString((uint)Marshal.ReadInt32(lParam), wParam.ToUInt32() == (int)Enums.EnumKeyEvent.WM_KEYDOWN || wParam.ToUInt32() == (int)Enums.EnumKeyEvent.WM_SYSKEYDOWN); _hookedKeyboardCallbackAsync.BeginInvoke((Enums.EnumKeyEvent)wParam.ToUInt32(), Marshal.ReadInt32(lParam), chars, null, null); } } return(DllUser32.CallNextHookEx(_hookId, nCode, wParam, lParam)); }
private static bool IsAppWindow(IntPtr hWnd) { if ((GetWindowLong(hWnd, (int)EnumGWLIndex.GWL_STYLE) & (int)EnumWindowsStyle.WS_SYSMENU) == 0) { return(false); } if (DllUser32.IsWindowVisible(hWnd)) { if ((GetWindowLong(hWnd, (int)EnumGWLIndex.GWL_EXSTYLE) & (int)EnumWindowsStyleEx.WS_EX_TOOLWINDOW) != 0) { return(false); } var hwndOwner = DllUser32.GetWindow(hWnd, (int)EnumGetWindowConstants.GW_OWNER); return((GetWindowLong(hwndOwner, (int)EnumGWLIndex.GWL_STYLE) & ((int)EnumWindowsStyle.WS_VISIBLE | (int)EnumWindowsStyle.WS_CLIPCHILDREN)) != ((int)EnumWindowsStyle.WS_VISIBLE | (int)EnumWindowsStyle.WS_CLIPCHILDREN) || (GetWindowLong(hwndOwner, (int)EnumGWLIndex.GWL_EXSTYLE) & (int)EnumWindowsStyleEx.WS_EX_TOOLWINDOW) != 0); } return(false); }
public void Dispose() { DllUser32.UnhookWindowsHookEx(_hookId); }
internal void EnumWindows() { DllUser32.EnumWindows(EnumWindowsProc, IntPtr.Zero); }
internal void UnregisterClipboardViewer() { DllUser32.ChangeClipboardChain(Handle, _clipboardViewerNext); }
internal void RegisterClipboardViewer() { _clipboardViewerNext = DllUser32.SetClipboardViewer(Handle); }
// Note: Sometimes single VKCode represents multiple chars, thus string. // E.g. typing "^1" (notice that when pressing 1 the both characters appear, // because of this behavior, "^" is called dead key) /// <summary> /// Convert VKCode to Unicode. /// <remarks>isKeyDown is required for because of keyboard state inconsistencies!</remarks> /// </summary> /// <param name="vkCode">VKCode</param> /// <param name="isKeyDown">Is the key down event?</param> /// <returns>String representing single unicode character.</returns> internal static string VkCodeToString(uint vkCode, bool isKeyDown) { // ToUnicodeEx needs StringBuilder, it populates that during execution. var sbString = new StringBuilder(5); var bKeyState = new byte[255]; bool bKeyStateStatus; bool isDead = false; // Gets the current windows window handle, threadID, processID IntPtr currentHWnd = (IntPtr)DllUser32.GetForegroundWindow();//may be bag uint currentWindowThreadId = DllUser32.GetWindowThreadProcessId(currentHWnd, out _); // This programs Thread ID uint thisProgramThreadId = DllUser32.GetCurrentThreadId(); // Attach to active thread so we can get that keyboard state if (DllUser32.AttachThreadInput(thisProgramThreadId, currentWindowThreadId, true)) { // Current state of the modifiers in keyboard bKeyStateStatus = DllUser32.GetKeyboardState(bKeyState); // Detach DllUser32.AttachThreadInput(thisProgramThreadId, currentWindowThreadId, false); } else { // Could not attach, perhaps it is this process? bKeyStateStatus = DllUser32.GetKeyboardState(bKeyState); } // On failure we return empty string. if (!bKeyStateStatus) { return(""); } // Gets the layout of keyboard var hkl = DllUser32.GetKeyboardLayout(currentWindowThreadId); // Maps the virtual keycode uint lScanCode = DllUser32.MapVirtualKeyEx(vkCode, 0, hkl); // Keyboard state goes inconsistent if this is not in place. In other words, we need to call above commands in UP events also. if (!isKeyDown) { return(""); } // Converts the VKCode to unicode int relevantKeyCountInBuffer = DllUser32.ToUnicodeEx(vkCode, lScanCode, bKeyState, sbString, sbString.Capacity, 0, hkl); string ret = string.Empty; switch (relevantKeyCountInBuffer) { // Dead keys (^,`...) case -1: isDead = true; // We must clear the buffer because ToUnicodeEx messed it up, see below. ClearKeyboardBuffer(vkCode, lScanCode, hkl); break; case 0: break; // Single character in buffer case 1: ret = sbString[0].ToString(); break; // Two or more (only two of them is relevant) default: ret = sbString.ToString().Substring(0, 2); break; } // We inject the last dead key back, since ToUnicodeEx removed it. // More about this peculiar behavior see e.g: // http://www.experts-exchange.com/Programming/System/Windows__Programming/Q_23453780.html // http://blogs.msdn.com/michkap/archive/2005/01/19/355870.aspx // http://blogs.msdn.com/michkap/archive/2007/10/27/5717859.aspx if (_lastVkCode != 0 && _lastIsDead) { var sbTemp = new StringBuilder(5); DllUser32.ToUnicodeEx(_lastVkCode, _lastScanCode, _lastKeyState, sbTemp, sbTemp.Capacity, 0, hkl); _lastVkCode = 0; return(ret); } // Save these _lastScanCode = lScanCode; _lastVkCode = vkCode; _lastIsDead = isDead; _lastKeyState = (byte[])bKeyState.Clone(); return(ret); }
internal void Stop() { DllUser32.UnhookWindowsHookEx(_hookId); }
internal void DeRegister() { DllUser32.RegisterShellHook(Handle, 0); }