public KeyboardHook(int vkCode, Action callback) { // Ref: http://forum.cheatengine.org/viewtopic.php?t=192699&sid=d25bb4a9d48a3518bba28ec63d6510a2 this._vkCode = vkCode; this._callback = callback; using (Process curProcess = Process.GetCurrentProcess()) using (ProcessModule curModule = curProcess.MainModule) { this._hookId = WIN32_API.SetWindowsHookEx(WIN32_API.WH_KEYBOARD_LL, HookCallback, WIN32_API.GetModuleHandle(curModule.ModuleName), 0); } }
private IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam) { if (nCode >= 0 && wParam == (IntPtr)WIN32_API.WM_KEYDOWN) { int vkCode = Marshal.ReadInt32(lParam); // Console.WriteLine("Key is pressed: " + (Keys)vkCode + " - " + vkCode); // React on the defined key if (vkCode == this._vkCode) { // Ref: http://stackoverflow.com/questions/9931723/passing-a-callback-function-to-another-class _callback(); } } return(WIN32_API.CallNextHookEx(this._hookId, nCode, wParam, lParam)); }
public static void DoFlashScreen() { // Ref: // http://stackoverflow.com/questions/14385838/draw-on-screen-without-form // http://stackoverflow.com/questions/2527679/how-to-clean-up-after-myself-when-drawing-directly-to-the-screen WIN32_API.SIZE size; size.cx = WIN32_API.GetSystemMetrics(WIN32_API.SM_CXSCREEN); size.cy = WIN32_API.GetSystemMetrics(WIN32_API.SM_CYSCREEN); IntPtr hDC = WIN32_API.GetDC(WIN32_API.GetDesktopWindow()); Graphics g = Graphics.FromHdc(hDC); SolidBrush b = new SolidBrush(Color.White); g.FillRectangle(b, new Rectangle(0, 0, size.cx, size.cy)); g.Dispose(); WIN32_API.ReleaseDC(WIN32_API.GetDesktopWindow(), hDC); WIN32_API.InvalidateRect(IntPtr.Zero, IntPtr.Zero, true); }
public static Bitmap DoScreenShot() { // Ref: http://stackoverflow.com/questions/158151/how-can-i-save-a-screenshot-directly-to-a-file-in-windows WIN32_API.SIZE size; size.cx = WIN32_API.GetSystemMetrics(WIN32_API.SM_CXSCREEN); size.cy = WIN32_API.GetSystemMetrics(WIN32_API.SM_CYSCREEN); IntPtr hDC = WIN32_API.GetDC(WIN32_API.GetDesktopWindow()); IntPtr hMemDC = WIN32_API.CreateCompatibleDC(hDC); IntPtr m_HBitmap = WIN32_API.CreateCompatibleBitmap(hDC, size.cx, size.cy); if (m_HBitmap == IntPtr.Zero) { return(null); } IntPtr hOld = (IntPtr)WIN32_API.SelectObject(hMemDC, m_HBitmap); WIN32_API.BitBlt(hMemDC, 0, 0, size.cx, size.cy, hDC, 0, 0, WIN32_API.SRCCOPY); WIN32_API.SelectObject(hMemDC, hOld); WIN32_API.DeleteDC(hMemDC); WIN32_API.ReleaseDC(WIN32_API.GetDesktopWindow(), hDC); return(System.Drawing.Image.FromHbitmap(m_HBitmap)); }