Best WinAppDriver code snippet using WinAppDriverUIRecorder.MouseKeyboardHook.KeyboardHookProc
MouseKeyboardHook.cs
Source:MouseKeyboardHook.cs
...255 class MouseKeyboardHook256 {257 //Declare MouseHookProcedure as a HookProc type.258 static HookProc MouseHookProcedure;259 static HookProc KeyboardHookProcedure;260 //Declare the hook handle as an int.261 static uint s_hHookMouse = 0;262 static uint s_hHookKeyboard = 0;263 public static bool s_bPauseMouseKeyboard = false;264 // WinUser.h265 public const int WM_MOUSEFIRST = 0x0200;266 public const int WM_MOUSEMOVE = 0x0200;267 public const int WM_LBUTTONDOWN = 0x0201;268 public const int WM_LBUTTONUP = 0x0202;269 public const int WM_LBUTTONDBLCLK = 0x0203;270 public const int WM_RBUTTONDOWN = 0x0204;271 public const int WM_RBUTTONUP = 0x0205;272 public const int WM_RBUTTONDBLCLK = 0x0206;273 public const int WM_MBUTTONDOWN = 0x0207;274 public const int WM_MBUTTONUP = 0x0208;275 public const int WM_MBUTTONDBLCLK = 0x0209;276 public const int WM_MOUSEWHEEL = 0x020A;277 public const int WM_XBUTTONDOWN = 0x020B;278 public const int WM_XBUTTONUP = 0x020C;279 public const int WM_XBUTTONDBLCLK = 0x020D;280 //Declare the mouse hook constant.281 public const uint WH_JOURNALRECORD = 0;282 public const uint WH_JOURNALPLAYBACK = 1;283 public const uint WH_KEYBOARD = 2;284 public const uint WH_GETMESSAGE = 3;285 public const uint WH_CALLWNDPROC = 4;286 public const uint WH_CBT = 5;287 public const uint WH_SYSMSGFILTER = 6;288 public const uint WH_MOUSE = 7;289 public const uint WH_HARDWARE = 8;290 public const uint WH_DEBUG = 9;291 public const uint WH_SHELL = 10;292 public const uint WH_FOREGROUNDIDLE = 11;293 public const uint WH_CALLWNDPROCRET = 12;294 public const uint WH_KEYBOARD_LL = 13;295 public const uint WH_MOUSE_LL = 14;296 [StructLayout(LayoutKind.Sequential)]297 public class POINT298 {299 public int X;300 public int Y;301 }302 [StructLayout(LayoutKind.Sequential)]303 public class MSLLHOOKSTRUCT304 {305 public POINT pt;306 public uint mouseData;307 public uint flags;308 public uint time;309 public uint dwExtraInfo;310 }311 [StructLayout(LayoutKind.Sequential)]312 public class KBDLLHOOKSTRUCT313 {314 public uint vkCode;315 public uint scanCode;316 public uint flags;317 public uint time;318 public uint dwExtraInfo;319 }320 private static void StartMouseHook()321 {322 StopMouseHook();323 MouseHookProcedure = new HookProc(MouseHookProc);324 s_hHookMouse = (uint)NativeMethods.SetWindowsHookExNative(MouseHookProcedure, (uint)WH_MOUSE_LL, NativeMethods.GetCurrentThreadId());325 }326 private static void StopMouseHook()327 {328 if (s_hHookMouse != 0)329 {330 NativeMethods.UninitializeHook(s_hHookMouse);331 s_hHookMouse = 0;332 }333 MouseHookProcedure = null;334 }335 private static void StartKeyboardHook()336 {337 StopKeyboardHook();338 KeyboardHookProcedure = new HookProc(KeyboardHookProc);339 s_hHookKeyboard = (uint)NativeMethods.SetWindowsHookExNative(KeyboardHookProcedure, (uint)WH_KEYBOARD_LL, 0);340 }341 private static void StopKeyboardHook()342 {343 if (s_hHookKeyboard != 0)344 {345 NativeMethods.UninitializeHook(s_hHookKeyboard);346 s_hHookKeyboard = 0;347 }348 KeyboardHookProcedure = null;349 }350 public static bool StartHook()351 {352 s_bPauseMouseKeyboard = false;353 MouseKeyboardHook.StartMouseHook();354 MouseKeyboardHook.StartKeyboardHook();355 return (s_hHookKeyboard != 0 && s_hHookMouse != 0);356 }357 public static void StopHook()358 {359 MouseKeyboardHook.StopMouseHook();360 MouseKeyboardHook.StopKeyboardHook();361 }362 public static IntPtr KeyboardHookProc(int nCode, IntPtr wParam, IntPtr lParam)363 {364 if (nCode < 0 || MouseKeyboardEventHandler.s_threadRecorder == null || MainWindow.s_mainWin.IsRecording == false)365 {366 return NativeMethods.CallNextHookEx(IntPtr.Zero, nCode, wParam, lParam);367 }368 KeyboardEvents kEvent = (KeyboardEvents)wParam.ToInt32();369 KBDLLHOOKSTRUCT kbd = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(KBDLLHOOKSTRUCT));370 int scanCode = (int)kbd.scanCode;371 VirtualKeys vk = (VirtualKeys)kbd.vkCode;372 if (vk == VirtualKeys.VK_PAUSE && kEvent == KeyboardEvents.KeyDown)373 {374 s_bPauseMouseKeyboard = !s_bPauseMouseKeyboard;375 if (s_bPauseMouseKeyboard == true)376 NativeMethods.PostMessage(MainWindow.s_windowHandle, (int)MainWindow.UiThreadTask.PauseRecording, 0, 0);...
KeyboardHookProc
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using System.Windows.Forms;7using WinAppDriverUIRecorder;8{9 {10 static void Main(string[] args)11 {12 MouseKeyboardHook mHook = new MouseKeyboardHook();13 mHook.KeyDown += new KeyEventHandler(mHook_KeyDown);14 mHook.Start();15 Console.ReadLine();16 }17 static void mHook_KeyDown(object sender, KeyEventArgs e)18 {19 Console.WriteLine(e.KeyValue);20 }21 }22}23using System;24using System.Collections.Generic;25using System.Linq;26using System.Text;27using System.Threading.Tasks;28using System.Windows.Forms;29using WinAppDriverUIRecorder;30{31 {32 static void Main(string[] args)33 {34 MouseKeyboardHook mHook = new MouseKeyboardHook();35 mHook.MouseDown += new MouseEventHandler(mHook_MouseDown);36 mHook.Start();37 Console.ReadLine();38 }39 static void mHook_MouseDown(object sender, MouseEventArgs e)40 {41 Console.WriteLine(e.Button.ToString());42 }43 }44}45using System;46using System.Collections.Generic;47using System.Linq;48using System.Text;49using System.Threading.Tasks;50using System.Windows.Forms;51using WinAppDriverUIRecorder;52{53 {54 static void Main(string[] args)55 {56 MouseKeyboardHook mHook = new MouseKeyboardHook();57 mHook.MouseUp += new MouseEventHandler(mHook_MouseUp);58 mHook.Start();59 Console.ReadLine();60 }61 static void mHook_MouseUp(object sender, MouseEventArgs e)62 {63 Console.WriteLine(e.Button.ToString());64 }65 }66}67using System;68using System.Collections.Generic;69using System.Linq;70using System.Text;71using System.Threading.Tasks;72using System.Windows.Forms;73using WinAppDriverUIRecorder;74{75 {76 static void Main(string[] args)77 {
KeyboardHookProc
Using AI Code Generation
1{2 {3 [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]4 public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);5 [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]6 public static extern short GetKeyState(int nVirtKey);7 [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]8 public static extern short GetAsyncKeyState(int vKey);9 [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]10 public static extern int GetKeyboardState(byte[] pbKeyState);11 [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]12 public static extern int ToAscii(int uVirtKey, int uScanCode, byte[] lpbKeyState, byte[] lpwTransKey, int fuState);13 [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]14 public static extern short GetKeyState(int nVirtKey);15 [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]16 public static extern int GetKeyboardLayout(int dwLayout);17 [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]18 public static extern bool GetKeyboardLayoutName(StringBuilder pwszKLID);19 [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]20 public static extern int MapVirtualKey(int uCode, int uMapType);21 [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]22 public static extern int GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId);23 [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]24 public static extern int GetForegroundWindow();25 [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]26 public static extern int AttachThreadInput(int idAttach, int idAttachTo, int fAttach);27 [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]28 public static extern int GetWindowText(int hWnd, StringBuilder text, int count);
KeyboardHookProc
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using System.Runtime.InteropServices;7using System.Diagnostics;8using System.Windows.Forms;9using System.Drawing;10using System.IO;11using System.Xml;12{13 {14 public static bool isRecording = false;15 public static string windowTitle;16 public static string windowHandle;17 public static string windowDimensions;18 public static string fileLocation;19 public static string[] mouseClicks = new string[3];20 public static string[] mouseLocation = new string[2];21 public static string[] keyboardKeys = new string[2];22 public static string[] windowDetails = new string[3];23 public static bool isLeftClicked = false;24 public static bool isRightClicked = false;25 public static bool isMiddleClicked = false;26 public static bool isKeyClicked = false;27 public static int x;28 public static int y;29 public static string key;30 public static string title;31 public static string handle;32 public static string dimensions;33 public static string xmlFileName;34 public static string xmlFilePath;35 public static string xmlFileDirectory;36 public static string xmlFileExtension = ".xml";37 public static string xmlFileElementWindow = "window";38 public static string xmlFileElementTitle = "title";39 public static string xmlFileElementHandle = "handle";40 public static string xmlFileElementDimensions = "dimensions";
KeyboardHookProc
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using System.Windows.Forms;7using System.Runtime.InteropServices;8using System.IO;9{10 {11 public static MouseKeyboardHook mHook = new MouseKeyboardHook();12 private const int WH_KEYBOARD_LL = 13;13 private const int WH_MOUSE_LL = 14;14 private static LowLevelKeyboardProc _proc = HookCallback;15 private static LowLevelMouseProc _procMouse = MouseHookProc;16 private static IntPtr _hookID = IntPtr.Zero;17 private static IntPtr _hookIDMouse = IntPtr.Zero;18 private static StreamWriter sw;19 private static StreamWriter swMouse;20 private static string path = "C:\\Users\\Public\\Documents\\";21 private static string pathMouse = "C:\\Users\\Public\\Documents\\";22 private static string fileName = "KeyBoardEvents.txt";23 private static string fileNameMouse = "MouseEvents.txt";24 private static string fullPath = path + fileName;25 private static string fullPathMouse = pathMouse + fileNameMouse;26 public static void Start()27 {28 {29 if (!File.Exists(fullPath))30 {31 sw = File.CreateText(fullPath);32 sw.Close();33 }34 if (!File.Exists(fullPathMouse))35 {36 swMouse = File.CreateText(fullPathMouse);37 swMouse.Close();38 }39 _hookID = SetHook(_proc);40 _hookIDMouse = SetHookMouse(_procMouse);41 }42 catch (Exception ex)43 {44 MessageBox.Show(ex.Message);45 }46 }47 private static IntPtr SetHook(LowLevelKeyboardProc proc)48 {49 using (ProcessCur
KeyboardHookProc
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.IO;4using System.Linq;5using System.Text;6using System.Threading.Tasks;7using System.Runtime.InteropServices;8using System.Windows.Forms;9using System.Diagnostics;10{11 {12 private const int WH_KEYBOARD_LL = 13;13 private const int WM_KEYDOWN = 0x0100;14 private const int WM_KEYUP = 0x0101;15 private const int WM_SYSKEYDOWN = 0x0104;16 private const int WM_SYSKEYUP = 0x0105;17 [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]18 private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);19 [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]20 [return: MarshalAs(UnmanagedType.Bool)]21 private static extern bool UnhookWindowsHookEx(IntPtr hhk);22 [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]23 private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);24 [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]25 private static extern IntPtr GetModuleHandle(string lpModuleName);26 private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);27 private LowLevelKeyboardProc _proc = HookCallback;28 private IntPtr _hookID = IntPtr.Zero;29 private string _fileName;30 private StreamWriter _writer;
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!