Show / Hide Table of Contents

Method keys.send


Overload

Generates virtual keystrokes (keys, text).

public static void send(params KKeysEtc[] keysEtc)
Parameters
keysEtc  (KKeysEtc[])

Arguments of these types:

  • string - keys. Key names separated by spaces or operators, like "Enter A Ctrl+A".
    Tool: in "" string press Ctrl+Space.
  • string with prefix "!" - literal text.
    Example: var p = "pass"; keys.send("!user", "Tab", "!" + p, "Enter");
  • string with prefix "%" - HTML to paste. Full or fragment.
  • clipboardData - clipboard data to paste.
  • KKey - a single key.
    Example: keys.send("Shift+", KKey.Left, "*3"); is the same as keys.send("Shift+Left*3");
  • int - sleep milliseconds. Max 10000.
    Example: keys.send("Left", 500, "Right");
  • System.Action - callback function.
    Example: Action click = () => mouse.click(); keys.send("Shift+", click);
  • KKeyScan - a single key, specified using scan code and/or virtual-key code and extended-key flag.
    Example: keys.send(new KKeyScan(0x3B, false)); //key F1
    Example: keys.send(new KKeyScan(KKey.Enter, true)); //numpad Enter
  • char - a single character. Like text with OKeyText.KeysOrChar or operator ^.
Exceptions
ArgumentException

An invalid value, for example an unknown key name.

AuException

Failed. When sending text, fails if there is no focused window.

InputDesktopException

Remarks

Usually keys are specified in string, like in this example:

keys.send("A F2 Ctrl+Shift+A Enter*2"); //keys A, F2, Ctrl+Shift+A, Enter Enter

Key names:

Group Keys Info
Named keys Modifier: Alt, Ctrl, Shift, Win, RAlt, RCtrl, RShift, RWin
Navigate:Esc, End, Home, PgDn, PgUp, Down, Left, Right, Up
Other:Back, Del, Enter, Apps, Pause, PrtSc, Space, Tab
Function:F1-F24
Lock:CapsLock, NumLock, ScrollLock, Ins
Start with an uppercase character. Only the first 3 characters are significant; others can be any ASCII letters. For example, can be "Back", "Bac", "Backspace" or "BACK", but not "back" or "Ba" or "Back5".

Alias: AltGr (RAlt), Menu (Apps), PageDown or PD (PgDn), PageUp or PU (PgUp), PrintScreen or PS (PrtSc), BS (Back), PB (Pause/Break), CL (CapsLock), NL (NumLock), SL (ScrollLock), HM (Home).
Text keys Alphabetic: A-Z (or a-z)
Number:0-9
Numeric keypad:#/#*#-#+#.#0-#9
Other:`-=[]\;',./
Spaces between keys are optional, except for uppercase A-Z. For example, can be "A B", "a b", "A b" or "ab", but not "AB" or "Ab".

For `[]\;',./ also can be used ~{}|:"<>?.
Other keys Names of enum KKey members. Example: keys.send("BrowserBack");
Other keys Virtual-key codes. Start with VK or Vk. Example: keys.send("VK65 VK0x42");
Forbidden Fn, Ctrl+Alt+Del, Win+L, some other. Programs cannot press these keys.
Special characters Operator: + * ( ) _ ^
Numpad key prefix: #
Text/HTML argument prefix: ! %
Reserved: @ $ &
These characters cannot be used as keys. Instead use =890-6315247.

Operators:

Operator Examples Description
*n "Left*3"
$"Left*{i}"
Press key n times, like "Left Left Left".
See keys.AddRepeat.
*down "Ctrl*down" Press key and don't release.
*up "Ctrl*up" Release key.
+ "Ctrl+Shift+A"
"Alt+E+P"
The same as "Ctrl*down Shift*down A Shift*up Ctrl*up" and "Alt*down E*down P E*up Alt*up".
+() "Alt+(E P)" The same as "Alt*down E P Alt*up".
Inside () cannot be used operators +, +() and ^.
_ "Tab _A_b Tab"
"Alt+_e_a"
"_**20"
Send next character like text with option OKeyText.KeysOrChar.
Can be used to Alt-select items in menus, ribbons and dialogs regardless of current keyboard layout.
Next character can be any 16-bit character, including operators and whitespace.
^ "Alt+^ea" Send all remaining characters and whitespace like text with option OKeyText.KeysOrChar.
For example "Alt+^ed b" is the same as "Alt+_e_d Space _b".
Alt is released after the first character. Don't use other modifiers.

Operators and related keys can be in separate arguments. Examples: keys.send("Shift+", KKey.A); keys.send(KKey.A, "*3");.

Uses opt.key:

Option Default Changed
OKey.NoBlockInput false. Blocks user-pressed keys. Sends them afterwards.
If the last argument is "sleep", stops blocking before executing it; else stops blocking after executing all arguments.
true. Does not block user-pressed keys.
OKey.NoCapsOff false. If the CapsLock key is toggled, untoggles it temporarily (presses it before and after). true. Does not touch the CapsLock key.
Alphabetic keys of "keys" arguments can depend on CapsLock. Text of "text" arguments doesn't depend on CapsLock, unless OKey.TextHow is KeysX.
OKey.NoModOff false. Releases modifier keys (Alt, Ctrl, Shift, Win).
Does it only at the start; later they cannot interfere, unless OKey.NoBlockInput is true.
true. Does not touch modifier keys.
OKey.TextSpeed 0 ms. 0 - 1000. Changes the speed for "text" arguments.
OKey.KeySpeed 2 ms. 0 - 1000. Changes the speed for "keys" arguments.
OKey.KeySpeedClipboard 5 ms. 0 - 1000. Changes the speed of Ctrl+V keys when pasting text or HTML using clipboard.
OKey.SleepFinally 10 ms. 0 - 10000.
Tip: to sleep finally, also can be used code like this: keys.send("keys", 1000);.
OKey.TextHow OKeyText.Characters. KeysOrChar, KeysOrPaste or Paste.
OKey.TextShiftEnter false. true. When sending text, instead of Enter send Shift+Enter.
OKey.PasteLength 200.
This option is used for "text" arguments. If text length >= this value, uses clipboard.
>=0.
OKey.PasteWorkaround false.
This option is used for "text" arguments when using clipboard.
true.
OKey.RestoreClipboard true. Restore clipboard data (by default only text).
This option is used for "text" and "HTML" arguments when using clipboard.
false. Don't restore clipboard data.
OKey.Hook null. Callback function that can modify options depending on active window etc.

This function does not wait until the target app receives and processes sent keystrokes and text; there is no reliable way to know it. It just adds small delays depending on options (OKey.SleepFinally etc). If need, change options or add "sleep" arguments or wait after calling this function. Sending text through the clipboard normally does not have these problems.

Don't use this function to automate windows of own thread. Call it from another thread. See example with async/await.

Administrator and uiAccess processes don't receive keystrokes sent by standard user processes. See UAC.

Mouse button codes/names (eg KKey.MouseLeft) cannot be used to click. Instead use callback, like in the "Ctrl+click" example.

You can use a keys variable instead of this function. Example: new keys(null).Add("keys", "!text").SendNow();. More examples in keys.keys topic.

This function calls keys.Add, which calls these functions depending on argument type: keys.AddKeys, keys.AddText, keys.AddChar, keys.AddClipboardData, keys.AddKey, keys.AddKey, keys.AddSleep, keys.AddAction. Then calls keys.SendNow.

Uses API SendInput.

Examples

//Press key Enter.
keys.send("Enter");

//Press keys Ctrl+A.
keys.send("Ctrl+A");

//Ctrl+Alt+Shift+Win+A.
keys.send("Ctrl+Alt+Shift+Win+A");

//Alt down, E, P, Alt up.
keys.send("Alt+(E P)");

//Alt down, E, P, Alt up.
keys.send("Alt*down E P Alt*up");

//Send text "Example".
keys.send("!Example");
keys.sendt("Example"); //same

//Press key End, key Backspace 3 times, send text "Text".
keys.send("End Back*3", "!Text");

//Press Tab n times, send text "user", press Tab, send text "password", press Enter.
int n = 5; string pw = "password";
keys.send($"Tab*{n}", "!user", "Tab", "!" + pw, "Enter");

//Press Ctrl+V, wait 500 ms, press Enter.
keys.send("Ctrl+V", 500, "Enter");

//F2, Ctrl+K, Left 3 times, Space, A, comma, 5, numpad 5, BrowserBack.
keys.send("F2 Ctrl+K Left*3 Space a , 5 #5", KKey.BrowserBack);

//Shift down, A 3 times, Shift up.
keys.send("Shift+A*3");

//Shift down, A 3 times, Shift up.
keys.send("Shift+", KKey.A, "*3");

//Shift down, A, wait 500 ms, B, Shift up.
keys.send("Shift+(", KKey.A, 500, KKey.B, ")");

//Send keys and text slowly.
opt.key.KeySpeed = opt.key.TextSpeed = 50;
keys.send("keys Shift+: Space 123456789 Space 123456789 ,Space", "!text: 123456789 123456789\n");

//Ctrl+click
Action click = () => mouse.click();
keys.send("Ctrl+", click);

//Ctrl+click
keys.send("Ctrl+", new Action(() => mouse.click()));

Show window and send keys/text to it when button clicked.

var b = new wpfBuilder("Window").WinSize(250);
b.R.AddButton("Keys", async _ => {
	//keys.send("Tab", "!text", 2000, "Esc"); //no
	await Task.Run(() => { keys.send("Tab", "!text", 2000, "Esc"); }); //use other thread
});
b.R.Add("Text", out TextBox text1);
b.R.AddOkCancel();
b.End();
if (!b.ShowDialog()) return;