Best WinAppDriver code snippet using WinAppDriverUIRecorder.MSLLHOOKSTRUCT
MouseKeyboardHook.cs
Source:MouseKeyboardHook.cs
...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);377 else378 NativeMethods.PostMessage(MainWindow.s_windowHandle, (int)MainWindow.UiThreadTask.Active, 0, 0);379 }380 381 if(s_bPauseMouseKeyboard == false)382 {383 MouseKeyboardEventHandler.RecordKey(kEvent, vk, scanCode);384 }385 return NativeMethods.CallNextHookEx(IntPtr.Zero, nCode, wParam, lParam);386 }387 public static IntPtr MouseHookProc(int nCode, IntPtr wParam, IntPtr lParam)388 {389 if (nCode < 0 || MouseKeyboardEventHandler.s_threadRecorder == null || s_bPauseMouseKeyboard == true)390 {391 return NativeMethods.CallNextHookEx(IntPtr.Zero, nCode, wParam, lParam);392 }393 MSLLHOOKSTRUCT mhs = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT));394 int left = mhs.pt.X;395 int top = mhs.pt.Y;396 //skip if cursor is on this app397 IntPtr hwnd = NativeMethods.WindowFromPoint(left, top);398 if (hwnd == MainWindow.s_windowHandle)399 {400 MouseKeyboardEventHandler.ResetRecordTimer();401 return NativeMethods.CallNextHookEx(IntPtr.Zero, nCode, wParam, lParam);402 }403 switch (wParam.ToInt32())404 {405 case WM_MOUSEMOVE:406 MouseKeyboardEventHandler.MouseMove(left, top);407 break;...
MSLLHOOKSTRUCT
Using AI Code Generation
1using WinAppDriverUIRecorder;2{3 {4 public POINT pt;5 public uint mouseData;6 public uint flags;7 public uint time;8 public UIntPtr dwExtraInfo;9 }10}11using WinAppDriverUIRecorder;12{13 {14 [DllImport("user32.dll")]15 public static extern short GetAsyncKeyState(int vKey);16 }17}18using WinAppDriverUIRecorder;19{20 {21 [DllImport("user32.dll")]22 public static extern bool GetCursorPos(out POINT lpPoint);23 }24}25using WinAppDriverUIRecorder;26{27 {28 public int x;29 public int y;30 }31}32using WinAppDriverUIRecorder;33{34 {35 [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]36 public static extern IntPtr SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);37 [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, SetLastError = true)]38 [return: MarshalAs(UnmanagedType.Bool)]39 public static extern bool UnhookWindowsHookEx(IntPtr hhk);40 [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]41 public static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);42 [DllImport("kernel32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]43 public static extern IntPtr GetModuleHandle(string lpModuleName);44 [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]45 public static extern int ToUnicode(uint wVirtKey, uint wScan
MSLLHOOKSTRUCT
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.Windows.Forms;8using WinAppDriverUIRecorder;9using System.Drawing;10{11 {12 [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]13 public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);14 [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]15 public static extern bool UnhookWindowsHookEx(int idHook);16 [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]17 public static extern int CallNextHookEx(int idHook, int nCode, IntPtr wParam, IntPtr lParam);18 [DllImport("kernel32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]19 public static extern IntPtr GetModuleHandle(string lpModuleName);20 public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);21 public const int WH_MOUSE_LL = 14;22 public const int WM_LBUTTONDOWN = 0x0201;23 public const int WM_LBUTTONUP = 0x0202;24 public const int WM_MOUSEMOVE = 0x0200;25 public const int WM_MOUSEWHEEL = 0x020A;26 public const int WM_RBUTTONDOWN = 0x0204;27 public const int WM_RBUTTONUP = 0x0205;28 public const int WM_MBUTTONDOWN = 0x0207;29 public const int WM_MBUTTONUP = 0x0208;30 public const int WM_KEYDOWN = 0x0100;31 public const int WM_KEYUP = 0x0101;32 public const int WM_SYSKEYDOWN = 0x0104;33 public const int WM_SYSKEYUP = 0x0105;34 private static HookProc _mouseDelegate;35 private static int _mouseHookHandle;36 public static void Main(string[] args)37 {38 _mouseDelegate = MouseHookProc;39 using (var curProcess = System.Diagnostics.Process.GetCurrentProcess())40 using (var curModule = curProcess.MainModule)41 {42 _mouseHookHandle = SetWindowsHookEx(WH_MOUSE_LL, _mouseDelegate, GetModuleHandle(curModule.ModuleName
MSLLHOOKSTRUCT
Using AI Code Generation
1using System;2using System.Windows.Forms;3using WinAppDriverUIRecorder;4{5 {6 static void Main(string[] args)7 {8 MouseHook.Start();9 MouseHook.MouseAction += MouseHook_MouseAction;10 Console.ReadLine();11 }12 private static void MouseHook_MouseAction(object sender, MouseHookEventArgs e)13 {14 Console.WriteLine("Mouse Action: " + e.Action);15 Console.WriteLine("Mouse Position: " + e.Position);16 Console.WriteLine("Mouse Data: " + e.Data);17 Console.WriteLine("Mouse Flags: " + e.Flags);18 Console.WriteLine("Mouse Time: " + e.Time);19 Console.WriteLine("Mouse ExtraInfo: " + e.ExtraInfo);20 }21 }22}23using System;24using System.Windows.Forms;25using WinAppDriverUIRecorder;26{27 {28 static void Main(string[] args)29 {30 KeyboardHook.Start();31 KeyboardHook.KeyboardAction += KeyboardHook_KeyboardAction;32 Console.ReadLine();33 }34 private static void KeyboardHook_KeyboardAction(object sender, KeyboardHookEventArgs e)35 {36 Console.WriteLine("Keyboard Action: " + e.Action);37 Console.WriteLine("Keyboard Virtual Key: " + e.VirtualKeyCode);38 Console.WriteLine("Keyboard Scan Code: " + e.ScanCode);39 Console.WriteLine("Keyboard Flags: " + e.Flags);40 Console.WriteLine("Keyboard Time: " + e.Time);41 Console.WriteLine("Keyboard ExtraInfo: " + e.ExtraInfo);42 }43 }44}45using System;46using System.Windows.Forms;47using WinAppDriverUIRecorder;48{49 {50 static void Main(string[] args)51 {52 MouseHook.Start();53 KeyboardHook.Start();54 MouseHook.MouseAction += MouseHook_MouseAction;55 KeyboardHook.KeyboardAction += KeyboardHook_KeyboardAction;56 Console.ReadLine();57 }58 private static void MouseHook_MouseAction(object sender, MouseHookEventArgs e)59 {60 Console.WriteLine("Mouse Action: " + e.Action);61 Console.WriteLine("Mouse Position: " + e.Position);62 Console.WriteLine("Mouse Data: " + e.Data);
MSLLHOOKSTRUCT
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.Windows.Forms;8{9 {10 public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);11 public delegate void MouseHookCallback(int x, int y);12 private MouseHookCallback callback;13 private HookProc hookProc;14 private int hookHandle;15 public MouseHook(MouseHookCallback callback)16 {17 this.callback = callback;18 hookProc = new HookProc(HookCallback);19 }20 public void Start()21 {22 hookHandle = SetWindowsHookEx(WH_MOUSE_LL, hookProc, IntPtr.Zero, 0);23 }24 public void Stop()25 {26 UnhookWindowsHookEx(hookHandle);27 }28 private int HookCallback(int nCode, IntPtr wParam, IntPtr lParam)29 {30 if (nCode >= 0)31 {32 MSLLHOOKSTRUCT hookStruct = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT));33 callback(hookStruct.pt.x, hookStruct.pt.y);34 }35 return CallNextHookEx(hookHandle, nCode, wParam, lParam);36 }37 private const int WH_MOUSE_LL = 14;38 private const int WM_LBUTTONDOWN = 0x0201;39 private const int WM_LBUTTONUP = 0x0202;40 private const int WM_RBUTTONDOWN = 0x0204;41 private const int WM_RBUTTONUP = 0x0205;42 private const int WM_MOUSEMOVE = 0x0200;43 private const int WM_MOUSEWHEEL = 0x020A;44 private const int WM_MOUSEHWHEEL = 0x020E;45 [StructLayout(LayoutKind.Sequential)]46 {47 public int x;48 public int y;49 }50 [StructLayout(LayoutKind.Sequential)]51 {52 public POINT pt;53 public uint mouseData;54 public uint flags;55 public uint time;56 public UIntPtr dwExtraInfo;57 }58 [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]59 private static extern int SetWindowsHookEx(int idHook, HookProc
MSLLHOOKSTRUCT
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.Windows.Forms;8using System.Diagnostics;9using System.IO;10using WinAppDriverUIRecorder;11{12 {13 private static LowLevelMouseProc _proc = HookCallback;14 private static IntPtr _hookID = IntPtr.Zero;15 public static string path = @"C:\Users\Public\Documents\mouseHook.txt";16 public static string path2 = @"C:\Users\Public\Documents\mouseHook2.txt";17 public static string path3 = @"C:\Users\Public\Documents\mouseHook3.txt";18 public static string path4 = @"C:\Users\Public\Documents\mouseHook4.txt";19 public static void Main()20 {21 _hookID = SetHook(_proc);22 Application.Run();23 UnhookWindowsHookEx(_hookID);24 }25 private static IntPtr SetHook(LowLevelMouseProc proc)26 {27 using (Process curProcess = Process.GetCurrentProcess())28 using (ProcessModule curModule = curProcess.MainModule)29 {30 return SetWindowsHookEx(WH_MOUSE_LL, proc,31 GetModuleHandle(curModule.ModuleName), 0);32 }33 }34 private delegate IntPtr LowLevelMouseProc(35 int nCode, IntPtr wParam, IntPtr lParam);36 private static IntPtr HookCallback(37 {38 if (nCode >= 0 &&39 MouseMessages.WM_LBUTTONDOWN == (MouseMessages)wParam)40 {41 MSLLHOOKSTRUCT hookStruct = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT));42 using (StreamWriter sw = File.AppendText(path))43 {44 sw.WriteLine("mouse down");45 }46 using (StreamWriter sw = File.AppendText(path2))47 {48 sw.WriteLine(hookStruct.pt.x + " " + hookStruct.pt.y
MSLLHOOKSTRUCT
Using AI Code Generation
1public static void Main(string[] args)2{3 var recorder = new WinAppDriverUIRecorder();4 recorder.Start();5 Console.WriteLine("Press any key to stop recording");6 Console.ReadKey();7 recorder.Stop();8 var events = recorder.Events;9 foreach (var evt in events)10 {11 Console.WriteLine(evt);12 }13}14public static void Main(string[] args)15{16 var recorder = new WinAppDriverUIRecorder();17 recorder.Start();18 Console.WriteLine("Press any key to stop recording");19 Console.ReadKey();20 recorder.Stop();21 var events = recorder.Events;22 foreach (var evt in events)23 {24 Console.WriteLine(evt);25 }26}27public static void Main(string[] args)28{29 var recorder = new WinAppDriverUIRecorder();30 recorder.Start();31 Console.WriteLine("Press any key to stop recording");32 Console.ReadKey();33 recorder.Stop();34 var events = recorder.Events;35 foreach (var evt in events)36 {37 Console.WriteLine(evt);38 }39}40public static void Main(string[] args)41{42 var recorder = new WinAppDriverUIRecorder();43 recorder.Start();44 Console.WriteLine("Press any key to stop recording");45 Console.ReadKey();46 recorder.Stop();47 var events = recorder.Events;48 foreach (var evt in events)49 {50 Console.WriteLine(evt);51 }52}53public static void Main(string[] args)54{55 var recorder = new WinAppDriverUIRecorder();56 recorder.Start();57 Console.WriteLine("Press any key to stop recording");58 Console.ReadKey();59 recorder.Stop();60 var events = recorder.Events;61 foreach (var evt in events)62 {63 Console.WriteLine(evt);64 }65}
MSLLHOOKSTRUCT
Using AI Code Generation
1using System;2using System.Windows.Forms;3using System.Runtime.InteropServices;4using System.Drawing;5using System.Threading;6using System.Diagnostics;7using System.Collections.Generic;8using System.Linq;9using System.Text;10using System.Threading.Tasks;11using System.Windows.Automation;12using System.Windows.Automation.Text;13using System.Windows.Automation.Provider;14using System.Windows;15using System.Windows.Media;16using System.IO;17using System.Xml.Linq;18using System.Xml;19using System.Text.RegularExpressions;20using System.Reflection;21using System.Threading;22{23 {24 [DllImport("user32.dll")]25 public static extern int SetForegroundWindow(IntPtr point);26 [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]27 public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);28 [DllImport("user32.dll")]29 public static extern bool GetCursorPos(out Point lpPoint);30 [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]31 public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);32 [DllImport("user32.dll")]33 public static extern short GetKeyState(int nVirtKey);34 [DllImport("user32.dll")]35 public static extern short GetAsyncKeyState(int vKey);36 [DllImport("user32.dll")]37 public static extern IntPtr GetForegroundWindow();38 [DllImport("user32.dll")]39 public static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);40 [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]41 public static extern IntPtr GetDesktopWindow();42 [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]43 public static extern IntPtr GetParent(IntPtr hWnd);44 [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]45 public static extern IntPtr GetWindow(IntPtr hWnd, int uCmd);46 [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]47 public static extern IntPtr GetWindowThreadProcessId(IntPtr handle, out int processId);
MSLLHOOKSTRUCT
Using AI Code Generation
1using System;2using System.Runtime.InteropServices;3using WinAppDriverUIRecorder;4{5 {6 [DllImport("user32.dll")]7 private static extern IntPtr SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hMod, uint dwThreadId);8 [DllImport("user32.dll")]9 [return: MarshalAs(UnmanagedType.Bool)]10 private static extern bool UnhookWindowsHookEx(IntPtr hhk);11 [DllImport("user32.dll")]12 private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);13 [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]14 private static extern IntPtr GetModuleHandle(string lpModuleName);15 private delegate IntPtr HookProc(int nCode, IntPtr wParam, IntPtr lParam);16 private const int WH_MOUSE_LL = 14;17 private static HookProc s_MouseDelegate;18 private static IntPtr s_MouseHook = IntPtr.Zero;19 private const int WM_LBUTTONDOWN = 0x0201;20 private const int WM_RBUTTONDOWN = 0x0204;21 private const int WM_MBUTTONDOWN = 0x0207;22 private const int WM_MOUSEMOVE = 0x0200;23 static void Main(string[] args)24 {25 s_MouseDelegate = MouseHookProc;26 s_MouseHook = SetHook(s_MouseDelegate);27 Console.WriteLine("Press any key to exit.");28 Console.ReadKey();29 UnhookWindowsHookEx(s_MouseHook);30 }31 private static IntPtr SetHook(HookProc proc)32 {33 using (var curProcess = System.Diagnostics.Process.GetCurrentProcess())34 using (var curModule = curProcess.MainModule)35 {36 return SetWindowsHookEx(WH_MOUSE_LL, proc, GetModuleHandle(curModule.ModuleName), 0);37 }38 }39 private static IntPtr MouseHookProc(int nCode, IntPtr wParam, IntPtr lParam)40 {41 if (nCode >= 0
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!!