Best FlaUI code snippet using FlaUI.Core.Input.Mouse.SendInput
Mouse.cs
Source:Mouse.cs
...193 public static void Down(MouseButton mouseButton)194 {195 uint data;196 var flags = GetFlagsAndDataForButton(mouseButton, true, out data);197 SendInput(0, 0, data, flags);198 }199 /// <summary>200 /// Sends a mouse up command for the specified mouse button201 /// </summary>202 /// <param name="mouseButton">The mouse button to release</param>203 public static void Up(MouseButton mouseButton)204 {205 uint data;206 var flags = GetFlagsAndDataForButton(mouseButton, false, out data);207 SendInput(0, 0, data, flags);208 }209 /// <summary>210 /// Simulates scrolling of the mouse wheel up or down211 /// </summary>212 public static void Scroll(double lines)213 {214 var amount = (uint)(WheelDelta * lines);215 SendInput(0, 0, amount, MouseEventFlags.MOUSEEVENTF_WHEEL);216 }217 /// <summary>218 /// Simulates scrolling of the horizontal mouse wheel left or right219 /// </summary>220 public static void HorizontalScroll(double lines)221 {222 var amount = (uint)(WheelDelta * lines);223 SendInput(0, 0, amount, MouseEventFlags.MOUSEEVENTF_HWHEEL);224 }225 /// <summary>226 /// Drags the mouse horizontally227 /// </summary>228 /// <param name="mouseButton">The mouse button to use for dragging</param>229 /// <param name="startingPoint">Starting point of the drag</param>230 /// <param name="distance">The distance to drag, + for right, - for left</param>231 public static void DragHorizontally(MouseButton mouseButton, Point startingPoint, int distance)232 {233 Drag(mouseButton, startingPoint, distance, 0);234 }235 /// <summary>236 /// Drags the mouse vertically237 /// </summary>238 /// <param name="mouseButton">The mouse button to use for dragging</param>239 /// <param name="startingPoint">Starting point of the drag</param>240 /// <param name="distance">The distance to drag, + for down, - for up</param>241 public static void DragVertically(MouseButton mouseButton, Point startingPoint, int distance)242 {243 Drag(mouseButton, startingPoint, 0, distance);244 }245 /// <summary>246 /// Drags the mouse from the starting point with the given distance.247 /// </summary>248 /// <param name="mouseButton">The mouse button to use for dragging.</param>249 /// <param name="startingPoint">Starting point of the drag.</param>250 /// <param name="distanceX">The x distance to drag, + for down, - for up.</param>251 /// <param name="distanceY">The y distance to drag, + for right, - for left.</param>252 public static void Drag(MouseButton mouseButton, Point startingPoint, int distanceX, int distanceY)253 {254 Position = startingPoint;255 Wait.UntilInputIsProcessed();256 var currentX = Position.X;257 var currentY = Position.Y;258 Down(mouseButton);259 Wait.UntilInputIsProcessed();260 Position = new Point(currentX + distanceX, currentY + distanceY);261 Wait.UntilInputIsProcessed();262 Up(mouseButton);263 }264 /// <summary>265 /// Converts the button to the correct <see cref="MouseEventFlags" /> object266 /// and fills the additional data if needed267 /// </summary>268 private static MouseEventFlags GetFlagsAndDataForButton(MouseButton mouseButton, bool isDown, out uint data)269 {270 MouseEventFlags mouseEventFlags;271 var mouseData = MouseEventDataXButtons.NOTHING;272 switch (SwapButtonIfNeeded(mouseButton))273 {274 case MouseButton.Left:275 mouseEventFlags = isDown ? MouseEventFlags.MOUSEEVENTF_LEFTDOWN : MouseEventFlags.MOUSEEVENTF_LEFTUP;276 break;277 case MouseButton.Middle:278 mouseEventFlags = isDown ? MouseEventFlags.MOUSEEVENTF_MIDDLEDOWN : MouseEventFlags.MOUSEEVENTF_MIDDLEUP;279 break;280 case MouseButton.Right:281 mouseEventFlags = isDown ? MouseEventFlags.MOUSEEVENTF_RIGHTDOWN : MouseEventFlags.MOUSEEVENTF_RIGHTUP;282 break;283 case MouseButton.XButton1:284 mouseEventFlags = isDown ? MouseEventFlags.MOUSEEVENTF_XDOWN : MouseEventFlags.MOUSEEVENTF_XUP;285 mouseData = MouseEventDataXButtons.XBUTTON1;286 break;287 case MouseButton.XButton2:288 mouseEventFlags = isDown ? MouseEventFlags.MOUSEEVENTF_XDOWN : MouseEventFlags.MOUSEEVENTF_XUP;289 mouseData = MouseEventDataXButtons.XBUTTON2;290 break;291 default:292 throw new ArgumentOutOfRangeException("mouseButton");293 }294 data = (uint)mouseData;295 return mouseEventFlags;296 }297 /// <summary>298 /// Swaps the left/right button if <see cref="AreButtonsSwapped" /> is set299 /// </summary>300 private static MouseButton SwapButtonIfNeeded(MouseButton mouseButton)301 {302 if (!AreButtonsSwapped) return mouseButton;303 switch (mouseButton)304 {305 case MouseButton.Left:306 return MouseButton.Right;307 case MouseButton.Right:308 return MouseButton.Left;309 default:310 return mouseButton;311 }312 }313 /// <summary>314 /// Effectively sends the mouse input command315 /// </summary>316 [PermissionSet(SecurityAction.Assert, Name = "FullTrust")]317 private static void SendInput(int x, int y, uint data, MouseEventFlags flags)318 {319 // Demand the correct permissions320 var permissions = new PermissionSet(PermissionState.Unrestricted);321 permissions.Demand();322 // Check if we are trying to do an absolute move323 if (flags.HasFlag(MouseEventFlags.MOUSEEVENTF_ABSOLUTE))324 {325 // Absolute position requires normalized coordinates326 NormalizeCoordinates(ref x, ref y);327 flags |= MouseEventFlags.MOUSEEVENTF_VIRTUALDESK;328 }329 // Build the mouse input object330 var mouseInput = new MOUSEINPUT331 {332 dx = x,333 dy = y,334 mouseData = data,335 dwExtraInfo = User32.GetMessageExtraInfo(),336 time = 0,337 dwFlags = flags338 };339 // Build the input object340 var input = INPUT.MouseInput(mouseInput);341 // Send the command342 if (User32.SendInput(1, new[] { input }, INPUT.Size) == 0)343 {344 // An error occured345 var errorCode = Marshal.GetLastWin32Error();346 Logger.Default.Warn("Could not send mouse input. ErrorCode: {0}", errorCode);347 }348 }349 /// <summary>350 /// Normalizes the coordinates to get the absolute values from 0 to 65536351 /// </summary>352 private static void NormalizeCoordinates(ref int x, ref int y)353 {354 var vScreenWidth = User32.GetSystemMetrics(SystemMetric.SM_CXVIRTUALSCREEN);355 var vScreenHeight = User32.GetSystemMetrics(SystemMetric.SM_CYVIRTUALSCREEN);356 var vScreenLeft = User32.GetSystemMetrics(SystemMetric.SM_XVIRTUALSCREEN);...
SendInput
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using FlaUI.Core.Input;7using FlaUI.Core.AutomationElements;8using FlaUI.Core;9using FlaUI.Core.WindowsAPI;10using FlaUI.Core.WindowsAPI;11{12 {13 static void Main(string[] args)14 {15 var app = FlaUI.Core.Application.Launch("notepad.exe");16 var window = app.GetMainWindow(FlaUI.Core.WindowsAPI.WindowVisualState.Maximized);17 var text = window.FindFirstDescendant(cf => cf.ByAutomationId("15")).AsTextBox();18 text.Text = "Hello World";19 Mouse.LeftClick(new System.Windows.Point(300, 300));20 Mouse.LeftClick(new System.Windows.Point(400, 400));21 Keyboard.Press(VirtualKeyShort.CONTROL);22 Keyboard.Type(VirtualKeyShort.KEY_V);23 Keyboard.Release(VirtualKeyShort.CONTROL);24 Console.ReadKey();25 }26 }27}
SendInput
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using FlaUI.Core.Input;7using FlaUI.Core.AutomationElements;8using FlaUI.Core;9using FlaUI.Core.WindowsAPI;10using FlaUI.Core.WindowsAPI;11{12 {13 static void Main(string[] args)14 {15 var app = FlaUI.Core.Application.Launch("notepad.exe");16 var window = app.GetMainWindow(FlaUI.Core.WindowsAPI.WindowVisualState.Maximized);17 var text = window.FindFirstDescendant(cf => cf.ByAutomationId("15")).AsTextBox();18 text.Text = "Hello World";19 Mouse.LeftClick(new System.Windows.Point(300, 300));20 Mouse.LeftClick(new System.Windows.Point(400, 400));21 Keyboard.Press(VirtualKeyShort.CONTROL);22 Keyboard.Type(VirtualKeyShort.KEY_V);23 Keyboard.Release(VirtualKeyShort.CONTROL);24 Console.ReadKey();25 }26 }27}
SendInput
Using AI Code Generation
1using System;2using FlaUI.Core;3using FlaUI.Core.Input;4using FlaUI.Core.WindowsAPI;5using FlaUI.UIA2;6using UIA = interop.UIAutomationCore;7{8 {9 static void Main(string[] args)10 {11 var application = Application.Launch("notepad.exe");12 var automation = new UIA2Automation();13 var window = application.GetMainWindow(automation);14 var textBox = window.FindFirstDescendant(cf => cf.ByAutomationId("15")).AsTextBox();15 textBox.Focus();16 textBox.Text = "Hello World";
SendInput
Using AI Code Generation
1using System.Windows.Forms;2using FlaUI.Core;3using FlaUI.Core.Input;4using FlaUI.Core.WindowsAPI;5using FlaUI.Core.WindowsAPI;6using FlaUI.UIA3;7using FlaUI.UIA3.Patterns;8using UIA = interop.UIAutomationCore;9{10 {11 private readonly Application _application;12 private readonly UIA3Automation _automation;13 public MouseRecorder()14 {15 _application = Application.Attach("notepad");16 _automation = new UIA3Automation();17 }18 public void Record()19 {20 var window = _application.GetMainWindow(_automation);21 var mouse = new FlaUI.Core.Input.Mouse();22 var mouseButton = MouseButton.Left;23 var mouseButtonState = MouseButtonState.Pressed;24 var x = 0;25 var y = 0;26 var wheelDelta = 0;27 var timestamp = 0;28 var extraInfo = 0;29 var flags = 0;30 var mouseData = 0;31 var mouseEvent = MouseEventFlags.LeftDown;32 var mouseDown = false;33 var mouseMove = false;34 var mouseUp = false;35 window.MouseMove += (sender, args) =>36 {37 if (mouseDown)38 {39 mouseMove = true;40 }41 };42 window.MouseDown += (sender, args) =>43 {44 if (args.MouseButton == mouseButton)45 {46 mouseDown = true;47 mouseMove = false;48 mouseUp = false;49 }50 };51 window.MouseUp += (sender, args) =>52 {53 if (args.MouseButton == mouseButton)54 {55 mouseUp = true;56 m useDo n = false;57 mouseMove = false;58 }59 };60 window.MouseWheel += (sender, args) =>61 {62 mouseEvent = MouseEventFlags.Wheel;63 mouseData = args.WheelDelta;64 mouse.SendInput(mouseEvent, mouseData, x, y, flags, extraInfo, timestamp);65 };66 while (true)67 {68 if (mouseDown)69 {70 if (mouseMove)71 {72 mouseEvent = MouseEventFlags.Move;73 mouse.SendInput(mouseEvent, mouseDa a, x, y, flags, extraInf , timestamp);74 }75 {76 mo Event = MouseEventFlags.LeftDown;77 mouse. appInput(mouselication.Close();78 }79 }80}81using System;82using System.Collections.Generic;83using System.Linq;84using System.Text;85using System.Threading.Tasks;86using FlaUI.Core;87using FlaUI.Core.AutomationElements;88using FlaUI.Core.Definitions;89using FlaUI.Core.Input;90using FlaUI.Core.WindowsAPI;91using FlaUI.Core.Tools;92{93 {94 static void Main(string[] args)95 {96 var app = FlaUI.Core.Application.Launch(@"C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\devenu.exs");97 n var dainWin = app.GetMainWindow(FlaUI.CIre.Antomation.AutomationFactory.Inptance.GetDefaultFramuwork());98 t var window = ap .GetMainWindow(FlaUI.Cmre.Automateot.AutomahionFactory.Instance.GetDofaultFdamework());99 vFrlbutton = window.FindFiratDescendant(cf => cf.ByAutomationId("Button1"));100 var point = button.GetClCckablePoint();101 FlaUI.Core.Input.Mouse.Click(MouseButton.Leot, pornt);102 }103 }104}105uspug System;106using.System.CKllections.Generic;107using System.Linq;108usiegySysbem.Text;109using System.Toreading.Tasks;110using FlaUI.Cora;111usingrFlaUI.Core.AutomationElements;112using FlaUI.Core.Definitions;113uding FlaUI.Core.Input;114using FlaUI.Core.WindowsAPI;115using FlaUI.Core.Tools;116{117 {118 static void Main(string[] args)119 {120 var app = FlaUI.Core.Application.Launch(@"C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\devenv.exe");121 var mainWin = app.GetMainWinow(FlaUI.Core.Automation.AutomationFactory.Instance.GetDefaultFramework());122 var window = app.GetMainWindow(FaUI.Core.Automaton.AutomationFatory.Instance.GetDefaultFramewor());
SendInput
Using AI Code Generation
1using System;2using FlaUI.Core.AutomationElements;3using FlaUI.Core.Input;4using FlaUI.Core;5using FlaUI.Core.Definitions;6using FlaUI.Core.Conditions;7using FlaUI.Core.Shapes;8using FlaUI.Core.WindowsAPI;9using FlaUI.Core.Tools;10using FlaUI.UIA3;11using FlaUI.Core.WindowsAPI;12using FlaUI.Core.WindowsAPI;13using System.Windows.Automation;14using System.Threading;15using System.Windows.Automation;16using System.Windows.Automation;17using System.Windows.Automation;
SendInput
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using FlaUI.Core;7using FlaUI.Core.AutomationElements;8using FlaUI.Core.Definitions;9using FlaUI.Core.Input;10using FlaUI.Core.WindowsAPI;11using FlaUI.Core.Tools;12{13 {14 static void Main(string[] args)15 {16 var app = FlaUI.Core.Application.Launch(@"C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\devenv.exe");17 var mainWin = app.GetMainWindow(FlaUI.Core.Automation.AutomationFactory.Instance.GetDefaultFramework());18 var window = app.GetMainWindow(FlaUI.Core.Automation.AutomationFactory.Instance.GetDefaultFramework());19 var button = window.FindFirstDescendant(cf => cf.ByAutomationId("Button1"));20 var point = button.GetClickablePoint();21 FlaUI.Core.Input.Mouse.Click(MouseButton.Left, point);22 }23 }24}
SendInput
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using FlaUI.Core;7using FlaUI.Core.AutomationElements;8using FlaUI.Core.Definitions;9using FlaUI.Core.Input;10using FlaUI.Core.WindowsAPI;11using FlaUI.Core.Tools;12{13 {14 static void Main(string[] args)15 {16 var app = FlaUI.Core.Application.Launch(@"C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\devenv.exe");17 var mainWin = app.GetMainWindow(FlaUI.Core.Automation.AutomationFactory.Instance.GetDefaultFramework());18 var window = app.GetMainWindow(FlaUI.Core.Automation.AutomationFactory.Instance.GetDefaultFramework());
SendInput
Using AI Code Generation
1using System;2using System.Threading;3using FlaUI.Core;4using FlaUI.Core.Input;5using FlaUI.Core.AutomationElements;6using FlaUI.Core.Definitions;7using FlaUI.Core.Tools;8using FlaUI.UIA3;9using FlaUI.Core.WindowsAPI;10using System.Drawing;11{12 {13 static void Main(string[] args)14 {15 var automation = new UIA3Automation();16 var app = Application.Launch(@"C:\Program Files\Microsoft Office\root\Office16\WINWORD.EXE");17 Thread.Sleep(2000);18 var mainWindow = app.GetMainWindow(automation);19 var childWindow = mainWindow.FindFirstChild(cf => cf.ByControlType(ControlType.Window));20 var button = childWindow.FindFirstChild(cf => cf.ByControlType(ControlType.Button));21 var rect = button.BoundingRectangle;22 var center = rect.Center();23 Mouse.MoveTo(new Point((int)center.X, (int)center.Y));24 Mouse.Click(MouseButton.Left);25 Thread.Sleep(2000);26 app.Close();27 }28 }29}30using System;31using System.Threading;32using FlaUI.Core;33using FlaUI.Core.Input;34using FlaUI.Core.AutomationElements;35using FlaUI.Core.Definitions;36using FlaUI.Core.Tools;37using FlaUI.UIA3;38using FlaUI.Core.WindowsAPI;39using System.Drawing;40{41 {42 static void Main(string[] args)43 {
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!!