How to use SendInput method of FlaUI.Core.WindowsAPI.User32 class

Best FlaUI code snippet using FlaUI.Core.WindowsAPI.User32.SendInput

Keyboard.cs

Source:Keyboard.cs Github

copy

Full Screen

...32 // Check if the char is unicode or no virtual key could be found33 if (character > 0xFE || code == -1)34 {35 // It seems to be unicode36 SendInput(character, true, false, false, true);37 SendInput(character, false, false, false, true);38 }39 else40 {41 // Get the high-order and low-order byte from the code42 var high = (byte)(code >> 8);43 var low = (byte)(code & 0xff);4445 // Check for caps lock and unset it46 var isCapsLockToggled = false;47 if ((User32.GetKeyState((int)VirtualKeyShort.CAPITAL) & 0x0001) != 0)48 {49 isCapsLockToggled = true;50 Type(VirtualKeyShort.CAPITAL);51 }5253 // Check if there are any modifiers54 var modifiers = new List<VirtualKeyShort>();55 if (HasScanModifier(high, VkKeyScanModifiers.SHIFT))56 {57 modifiers.Add(VirtualKeyShort.SHIFT);58 }59 if (HasScanModifier(high, VkKeyScanModifiers.CONTROL))60 {61 modifiers.Add(VirtualKeyShort.CONTROL);62 }63 if (HasScanModifier(high, VkKeyScanModifiers.ALT))64 {65 modifiers.Add(VirtualKeyShort.ALT);66 }67 // Press the modifiers68 foreach (var mod in modifiers)69 {70 Press(mod);71 }72 // Type the effective key73 SendInput(low, true, false, false, false);74 SendInput(low, false, false, false, false);75 // Release the modifiers76 foreach (var mod in Enumerable.Reverse(modifiers))77 {78 Release(mod);79 }8081 // Re-toggle the caps lock if it was set before82 if (isCapsLockToggled)83 {84 Type(VirtualKeyShort.CAPITAL);85 }86 }87 }8889 /// <summary>90 /// Types the given keys, one by one.91 /// </summary>92 public static void Type(params VirtualKeyShort[] virtualKeys)93 {94 if (virtualKeys == null)95 {96 return;97 }98 foreach (var key in virtualKeys)99 {100 Press(key);101 Release(key);102 }103 }104105 /// <summary>106 /// Types the given keys simultaneously (starting with the first).107 /// </summary>108 public static void TypeSimultaneously(params VirtualKeyShort[] virtualKeys)109 {110 if (virtualKeys == null)111 {112 return;113 }114 foreach (var key in virtualKeys)115 {116 Press(key);117 }118 foreach (var key in virtualKeys.Reverse())119 {120 Release(key);121 }122 }123124 /// <summary>125 /// Types the given scan-code.126 /// </summary>127 public static void TypeScanCode(ushort scanCode, bool isExtendedKey)128 {129 PressScanCode(scanCode, isExtendedKey);130 ReleaseScanCode(scanCode, isExtendedKey);131 }132133 /// <summary>134 /// Types the given virtual key-code.135 /// </summary>136 public static void TypeVirtualKeyCode(ushort virtualKeyCode)137 {138 PressVirtualKeyCode(virtualKeyCode);139 ReleaseVirtualKeyCode(virtualKeyCode);140 }141142 /// <summary>143 /// Presses the given key.144 /// </summary>145 public static void Press(VirtualKeyShort virtualKey)146 {147 PressVirtualKeyCode((ushort)virtualKey);148 }149150 /// <summary>151 /// Presses the given scan-code.152 /// </summary>153 public static void PressScanCode(ushort scanCode, bool isExtendedKey)154 {155 SendInput(scanCode, true, true, isExtendedKey, false);156 }157158 /// <summary>159 /// Presses the given virtual key-code.160 /// </summary>161 public static void PressVirtualKeyCode(ushort virtualKeyCode)162 {163 SendInput(virtualKeyCode, true, false, false, false);164 }165166 /// <summary>167 /// Releases the given key.168 /// </summary>169 public static void Release(VirtualKeyShort virtualKey)170 {171 ReleaseVirtualKeyCode((ushort)virtualKey);172 }173174 /// <summary>175 /// Releases the given scan-code.176 /// </summary>177 public static void ReleaseScanCode(ushort scanCode, bool isExtendedKey)178 {179 SendInput(scanCode, false, true, isExtendedKey, false);180 }181182 /// <summary>183 /// Releases the given virtual key-code.184 /// </summary>185 public static void ReleaseVirtualKeyCode(ushort virtualKeyCode)186 {187 SendInput(virtualKeyCode, false, false, false, false);188 }189190 /// <summary>191 /// Presses the given keys and releases them when the returned object is disposed.192 /// </summary>193 public static IDisposable Pressing(params VirtualKeyShort[] virtualKeys)194 {195 return new KeyPressingActivation(virtualKeys);196 }197198 /// <summary>199 /// Checks if a given byte has a specific VkKeyScan-modifier set.200 /// </summary>201 private static bool HasScanModifier(byte b, VkKeyScanModifiers modifierToTest)202 {203 return (VkKeyScanModifiers)(b & (byte)modifierToTest) == modifierToTest;204 }205206 /// <summary>207 /// Effectively sends the keyboard input command.208 /// </summary>209 /// <param name="keyCode">The key code to send. Can be the scan code or the virtual key code.</param>210 /// <param name="isKeyDown">Flag if the key should be pressed or released.</param>211 /// <param name="isScanCode">Flag if the code is the scan code or the virtual key code.</param>212 /// <param name="isExtended">Flag if the key is an extended key.</param>213 /// <param name="isUnicode">Flag if the key is unicode.</param>214 private static void SendInput(ushort keyCode, bool isKeyDown, bool isScanCode, bool isExtended, bool isUnicode)215 {216 // Prepare the basic object217 var keyboardInput = new KEYBDINPUT218 {219 time = 0,220 dwExtraInfo = User32.GetMessageExtraInfo()221 };222223 // Add the "key-up" flag if needed. By default it is "key-down"224 if (!isKeyDown)225 {226 keyboardInput.dwFlags |= KeyEventFlags.KEYEVENTF_KEYUP;227 }228229 if (isScanCode)230 {231 keyboardInput.wScan = keyCode;232 keyboardInput.dwFlags |= KeyEventFlags.KEYEVENTF_SCANCODE;233 // Add the extended flag if the flag is set or the keycode is prefixed with the byte 0xE0234 // See https://msdn.microsoft.com/en-us/library/windows/desktop/ms646267(v=vs.85).aspx235 if (isExtended || (keyCode & 0xFF00) == 0xE0)236 {237 keyboardInput.dwFlags |= KeyEventFlags.KEYEVENTF_EXTENDEDKEY;238 }239 }240 else if (isUnicode)241 {242 keyboardInput.dwFlags |= KeyEventFlags.KEYEVENTF_UNICODE;243 keyboardInput.wScan = keyCode;244 }245 else246 {247 keyboardInput.wVk = keyCode;248 }249250 // Build the input object251 var input = INPUT.KeyboardInput(keyboardInput);252 // Send the command253 if (User32.SendInput(1, new[] { input }, INPUT.Size) == 0)254 {255 // An error occured256 var errorCode = Marshal.GetLastWin32Error();257 Logger.Default.Warn("Could not send keyboard input. ErrorCode: {0}", errorCode);258 }259 }260261 /// <summary>262 /// Disposable class which presses the keys on creation263 /// and disposes them on destruction.264 /// </summary>265 private class KeyPressingActivation : IDisposable266 {267 private readonly VirtualKeyShort[] _virtualKeys; ...

Full Screen

Full Screen

SendInput

Using AI Code Generation

copy

Full Screen

1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using FlaUI.Core;7using FlaUI.Core.WindowsAPI;8using FlaUI.Core.WindowsAPI.User32;9{10 {11 static void Main(string[] args)12 {13 var app = FlaUI.Core.Application.Attach("notepad");14 var window = app.GetMainWindow(FlaUI.Core.AutomationBase.FromPoint(0, 0));15 User32.SendInput(new List<INPUT>()16 {17 new INPUT() { type = 1, U = new INPUT.Union() { ki = new KEYBDINPUT() { wVk = VK.VK_A } } },18 new INPUT() { type = 1, U = new INPUT.Union() { ki = new KEYBDINPUT() { wVk = VK.VK_D } } },19 new INPUT() { type = 1, U = new INPUT.Union() { ki = new KEYBDINPUT() { wVk = VK.VK_A } } },20 new INPUT() { type = 1, U = new INPUT.Union() { ki = new KEYBDINPUT() { wVk = VK.VK_D } } }21 });22 Console.ReadLine();23 }24 }25}

Full Screen

Full Screen

SendInput

Using AI Code Generation

copy

Full Screen

1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using System.Windows.Forms;7using FlaUI.Core.WindowsAPI;8{9 {10 static void Main(string[] args)11 {12 var app = FlaUI.Core.Application.Launch("C:\\Users\\Administrator\\Desktop\\5.exe");13 var mainWindow = app.GetMainWindow(FlaUI.Core.Automation.WindowsFramework.WinForms);14 var textBox = mainWindow.FindFirstDescendant(FlaUI.Core.Definitions.TreeScope.Descendants, FlaUI.Core.Definitions.ConditionFactory.ByAutomationId("txtName"));15 var button = mainWindow.FindFirstDescendant(FlaUI.Core.Definitions.TreeScope.Descendants, FlaUI.Core.Definitions.ConditionFactory.ByAutomationId("btnSubmit"));16 textBox.AsTextBox().Text = "Hello World";17 button.AsButton().Click();18 textBox.AsTextBox().SendInput("Hello World");19 button.AsButton().SendInput();20 }21 }22}

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful