How to use SendInput method of FlaUI.Core.Input.Keyboard class

Best FlaUI code snippet using FlaUI.Core.Input.Keyboard.SendInput

Keyboard.cs

Source:Keyboard.cs Github

copy

Full Screen

...30 // Check if the char is unicode or no virtual key could be found31 if (character > 0xFE || code == -1)32 {33 // It seems to be unicode34 SendInput(character, true, false, false, true);35 SendInput(character, false, false, false, true);36 }37 else38 {39 // Get the high-order and low-order byte from the code40 var high = (byte)(code >> 8);41 var low = (byte)(code & 0xff);42 // Check for caps lock and unset it43 var isCapsLockToggled = false;44 if ((User32.GetKeyState((int)VirtualKeyShort.CAPITAL) & 0x0001) != 0)45 {46 isCapsLockToggled = true;47 Type(VirtualKeyShort.CAPITAL);48 }49 // Check if there are any modifiers50 var modifiers = new List<VirtualKeyShort>();51 if (HasScanModifier(high, VkKeyScanModifiers.SHIFT))52 {53 modifiers.Add(VirtualKeyShort.SHIFT);54 }55 if (HasScanModifier(high, VkKeyScanModifiers.CONTROL))56 {57 modifiers.Add(VirtualKeyShort.CONTROL);58 }59 if (HasScanModifier(high, VkKeyScanModifiers.ALT))60 {61 modifiers.Add(VirtualKeyShort.ALT);62 }63 // Press the modifiers64 foreach (var mod in modifiers)65 {66 Press(mod);67 }68 // Type the effective key69 SendInput(low, true, false, false, false);70 SendInput(low, false, false, false, false);71 // Release the modifiers72 foreach (var mod in Enumerable.Reverse(modifiers))73 {74 Release(mod);75 }76 // Re-toggle the caps lock if it was set before77 if (isCapsLockToggled)78 {79 Type(VirtualKeyShort.CAPITAL);80 }81 }82 }83 /// <summary>84 /// Types the given keys, one by one.85 /// </summary>86 public static void Type(params VirtualKeyShort[] virtualKeys)87 {88 if (virtualKeys == null)89 {90 return;91 }92 foreach (var key in virtualKeys)93 {94 Press(key);95 Release(key);96 }97 }98 /// <summary>99 /// Types the given keys simultaneously (starting with the first).100 /// </summary>101 public static void TypeSimultaneously(params VirtualKeyShort[] virtualKeys)102 {103 if (virtualKeys == null)104 {105 return;106 }107 foreach (var key in virtualKeys)108 {109 Press(key);110 }111 foreach (var key in virtualKeys.Reverse())112 {113 Release(key);114 }115 }116 /// <summary>117 /// Types the given scan-code.118 /// </summary>119 public static void TypeScanCode(ushort scanCode, bool isExtendedKey)120 {121 PressScanCode(scanCode, isExtendedKey);122 ReleaseScanCode(scanCode, isExtendedKey);123 }124 /// <summary>125 /// Types the given virtual key-code.126 /// </summary>127 public static void TypeVirtualKeyCode(ushort virtualKeyCode)128 {129 PressVirtualKeyCode(virtualKeyCode);130 ReleaseVirtualKeyCode(virtualKeyCode);131 }132 /// <summary>133 /// Presses the given key.134 /// </summary>135 public static void Press(VirtualKeyShort virtualKey)136 {137 PressVirtualKeyCode((ushort)virtualKey);138 }139 /// <summary>140 /// Presses the given scan-code.141 /// </summary>142 public static void PressScanCode(ushort scanCode, bool isExtendedKey)143 {144 SendInput(scanCode, true, true, isExtendedKey, false);145 }146 /// <summary>147 /// Presses the given virtual key-code.148 /// </summary>149 public static void PressVirtualKeyCode(ushort virtualKeyCode)150 {151 SendInput(virtualKeyCode, true, false, false, false);152 }153 /// <summary>154 /// Releases the given key.155 /// </summary>156 public static void Release(VirtualKeyShort virtualKey)157 {158 ReleaseVirtualKeyCode((ushort)virtualKey);159 }160 /// <summary>161 /// Releases the given scan-code.162 /// </summary>163 public static void ReleaseScanCode(ushort scanCode, bool isExtendedKey)164 {165 SendInput(scanCode, false, true, isExtendedKey, false);166 }167 /// <summary>168 /// Releases the given virtual key-code.169 /// </summary>170 public static void ReleaseVirtualKeyCode(ushort virtualKeyCode)171 {172 SendInput(virtualKeyCode, false, false, false, false);173 }174 /// <summary>175 /// Presses the given keys and releases them when the returned object is disposed.176 /// </summary>177 public static IDisposable Pressing(params VirtualKeyShort[] virtualKeys)178 {179 return new KeyPressingActivation(virtualKeys);180 }181 /// <summary>182 /// Checks if a given byte has a specific VkKeyScan-modifier set.183 /// </summary>184 private static bool HasScanModifier(byte b, VkKeyScanModifiers modifierToTest)185 {186 return (VkKeyScanModifiers)(b & (byte)modifierToTest) == modifierToTest;187 }188 /// <summary>189 /// Effectively sends the keyboard input command.190 /// </summary>191 /// <param name="keyCode">The key code to send. Can be the scan code or the virtual key code.</param>192 /// <param name="isKeyDown">Flag if the key should be pressed or released.</param>193 /// <param name="isScanCode">Flag if the code is the scan code or the virtual key code.</param>194 /// <param name="isExtended">Flag if the key is an extended key.</param>195 /// <param name="isUnicode">Flag if the key is unicode.</param>196 private static void SendInput(ushort keyCode, bool isKeyDown, bool isScanCode, bool isExtended, bool isUnicode)197 {198 // Prepare the basic object199 var keyboardInput = new KEYBDINPUT200 {201 time = 0,202 dwExtraInfo = User32.GetMessageExtraInfo()203 };204 // Add the "key-up" flag if needed. By default it is "key-down"205 if (!isKeyDown)206 {207 keyboardInput.dwFlags |= KeyEventFlags.KEYEVENTF_KEYUP;208 }209 if (isScanCode)210 {211 keyboardInput.wScan = keyCode;212 keyboardInput.dwFlags |= KeyEventFlags.KEYEVENTF_SCANCODE;213 // Add the extended flag if the flag is set or the keycode is prefixed with the byte 0xE0214 // See https://msdn.microsoft.com/en-us/library/windows/desktop/ms646267(v=vs.85).aspx215 if (isExtended || (keyCode & 0xFF00) == 0xE0)216 {217 keyboardInput.dwFlags |= KeyEventFlags.KEYEVENTF_EXTENDEDKEY;218 }219 }220 else if (isUnicode)221 {222 keyboardInput.dwFlags |= KeyEventFlags.KEYEVENTF_UNICODE;223 keyboardInput.wScan = keyCode;224 }225 else226 {227 keyboardInput.wVk = keyCode;228 }229 // Build the input object230 var input = INPUT.KeyboardInput(keyboardInput);231 // Send the command232 if (User32.SendInput(1, new[] { input }, INPUT.Size) == 0)233 {234 // An error occured235 var errorCode = Marshal.GetLastWin32Error();236 Logger.Default.Warn("Could not send keyboard input. ErrorCode: {0}", errorCode);237 }238 }239 /// <summary>240 /// Disposable class which presses the keys on creation241 /// and disposes them on destruction.242 /// </summary>243 private class KeyPressingActivation : IDisposable244 {245 private readonly VirtualKeyShort[] _virtualKeys;246 public KeyPressingActivation(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.AutomationElements;7using FlaUI.Core.Input;8using FlaUI.Core.WindowsAPI;9using FlaUI.UIA2;10using FlaUI.Core;11using FlaUI.Core.Definitions;12{13 {14 static void Main(string[] args)15 {16 var app = FlaUI.Core.Application.Launch(@"C:\Windows\System32\calc.exe");17 var automation = new UIA2Automation();18 var calcWindow = app.GetMainWindow(automation);19 var button = calcWindow.FindFirstDescendant(cf => cf.ByControlType(ControlType.Button).And(cf.ByName("1")));20 button.AsButton().Click();21 System.Threading.Thread.Sleep(2000);22 Keyboard.Press(VirtualKeyShort.CONTROL);23 Keyboard.Press(VirtualKeyShort.VK_C);24 Keyboard.Release(VirtualKeyShort.CONTROL);25 Keyboard.Release(VirtualKeyShort.VK_C);26 System.Threading.Thread.Sleep(2000);27 app.Close();28 }29 }30}

Full Screen

Full Screen

SendInput

Using AI Code Generation

copy

Full Screen

1using FlaUI.Core;2using FlaUI.Core.Input;3using FlaUI.Core.WindowsAPI;4using System;5using System.Collections.Generic;6using System.Linq;7using System.Text;8using System.Threading.Tasks;9{10 {11 static void Main(string[] args)12 {13 var application = Application.Launch(@"C:\Program Files (x86)\Notepad++\notepad++.exe");14 var window = application.GetMainWindow();15 window.Focus();16 Keyboard.Type("Hello World!");

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.Automation;7using FlaUI.Core;8using FlaUI.Core.AutomationElements;9using FlaUI.Core.Definitions;10using FlaUI.Core.Input;11using FlaUI.Core.WindowsAPI;12using FlaUI.UIA2;13using FlaUI.UIA3;14using FlaUI.UIA3.Patterns;15using FlaUI.UIA3.Converters;16using FlaUI.Core.Tools;17using FlaUI.Core.Conditions;18{19 {20 static void Main(string[] args)21 {22 var app = FlaUI.Core.Application.Launch("calc.exe");23 var automation = new UIA3Automation();24 var mainWindow = app.GetMainWindow(automation);25 var window = mainWindow.AsWindow();26 window.Focus();27 window.SetForeground();28 System.Threading.Thread.Sleep(3000);29 AutomationElement element = AutomationElement.RootElement.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "Calculator"));30 System.Threading.Thread.Sleep(3000);31 Keyboard.Type(element, "2");32 Keyboard.Type(element, "3");33 Keyboard.Type(element, "4");34 Keyboard.Type(element, "5");35 Keyboard.Type(element, "6");36 Keyboard.Type(element, "7");37 Keyboard.Type(element, "8");38 Keyboard.Type(element, "9");39 Keyboard.Type(element, "0");40 Keyboard.Type(element, "1");41 Keyboard.Type(element, "2");42 Keyboard.Type(element, "3");43 Keyboard.Type(element, "4");44 Keyboard.Type(element, "5");45 Keyboard.Type(element, "6");46 Keyboard.Type(element, "7");47 Keyboard.Type(element, "8");

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.

Run FlaUI automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful