Core Function SendKeys

From Sputnik Wiki
Jump to: navigation, search
SendKeys( <keydef>, <flag> )

Contents

Description

Sends simulated keystrokes to the active window.

keydef

The sequence of keys to send.

flag

Optional; Changes how "keys" is processed:

By setting the flag parameter to 1 the "special" processing will be disabled.

This is useful when you want to send some text copied from a variable and you want the text sent exactly as written.

Return Value

Success: true Failure: false

Remarks

Since Sputnik uses { } as physical code inside strings you must either escape it like \{ } or place it inside a string type that is not parsed such as 'Test{ }Test' or @"Test{ }Test" etc.

Sputnik can send all ASCII and Extended ASCII characters (0-255), to send UNICODE characters you must use the "ASC" option and the code of the character you wish to send (see {ASC} at the bottom of the table below).

Most laptop computer keyboards have a special Fn key. This key cannot be simulated.

The "SendKeys" command syntax is similar to that of ScriptIt and the Visual Basic "SendKeys" command. Characters are sent as written with the exception of the following characters:

'!' This tells Sputnik to send an ALT keystroke, therefore SendKeys("This is text!a") would send the keys "This is text" and then press "ALT+a".

N.B. Some programs are very choosy about capital letters and ALT keys, i.e. "!A" is different to "!a". The first says ALT+SHIFT+A, the second is ALT+a. If in doubt, use lowercase!

'+' This tells Sputnik to send a SHIFT keystroke, therefore SendKeys("Hell+o") would send the text "HellO". Sputnik("!+a") would send "ALT+SHIFT+a".

'^' This tells Sputnik to send a CONTROL keystroke, therefore SendKeys("^!a") would send "CTRL+ALT+a".

N.B. Some programs are very choosy about capital letters and CTRL keys, i.e. "^A" is different to "^a". The first says CTRL+SHIFT+A, the second is CTRL+a. If in doubt, use lowercase!

'#' The hash now sends a Windows keystroke; therefore, SendKeys("#r") would send Win+r which launches the Run dialog box.

You can set SendCapslockMode to make CAPS LOCK disabled at the start of a Send operation and restored upon completion. However, if a user is holding down the Shift key when a Send function begins, text may be sent in uppercase. One workaround is to SendKeys('{SHIFTDOWN}{SHIFTUP}') before the other Send operations.

Certain special keys can be sent and should be enclosed in braces:

Send Command (if zero flag) Resulting Keypress 
{!} 					! 
{#} 					# 
{+} 					+ 
{^} 					^ 
{{} 					{ 
{}} 					} 
{SPK command} 				Execute Sputnik code (See example below)
{SPACE} 				SPACE 
{ENTER} 				ENTER key on the main keyboard 
{ALT} 					ALT 
{BACKSPACE} or {BS} 			BACKSPACE 
{DELETE} or {DEL} 			DELETE 
{UP} 					Cursor up 
{DOWN} 					Cursor down 
{LEFT} 					Cursor left 
{RIGHT} 				Cursor right 
{HOME} 					HOME 
{END} 					END 
{ESCAPE} or {ESC}			ESCAPE 
{INSERT} or {INS} 			INS 
{PGUP} 					PageUp 
{PGDN} 					PageDown 
{F1} - {F12} 				Function keys 
{TAB} 					TAB 
{PRINTSCREEN} 				Print Screen key 
{LWIN} 					Left Windows key 
{RWIN} 					Right Windows key 
{NUMLOCK on} 				NUMLOCK (on/off/toggle) 
{CAPSLOCK off} 				CAPSLOCK (on/off/toggle) 
{SCROLLLOCK toggle} 			SCROLLLOCK (on/off/toggle) 
{CTRLBREAK} 				Ctrl+Break 
{PAUSE} 				PAUSE 
{NUMPAD0} - {NUMPAD9} 			Numpad digits 
{NUMPADMULT} 				Numpad Multiply 
{NUMPADADD} 				Numpad Add 
{NUMPADSUB} 				Numpad Subtract 
{NUMPADDIV} 				Numpad Divide 
{NUMPADDOT} 				Numpad period 
{NUMPADENTER} 				Enter key on the numpad 
{APPSKEY} 				Windows App key 
{LALT} 					Left ALT key 
{RALT} 					Right ALT key 
{LCTRL} 				Left CTRL key 
{RCTRL} 				Right CTRL key 
{LSHIFT} 				Left Shift key 
{RSHIFT} 				Right Shift key 
{SLEEP} 				Computer SLEEP key 
{ALTDOWN} 				Holds the ALT key down until {ALTUP} is sent 
{SHIFTDOWN} 				Holds the SHIFT key down until {SHIFTUP} is sent 
{CTRLDOWN} 				Holds the CTRL key down until {CTRLUP} is sent 
{LWINDOWN} 				Holds the left Windows key down until {LWINUP} is sent 
{RWINDOWN} 				Holds the right Windows key down until {RWINUP} is sent 
{ASC nnnn} 				Send the ALT+nnnn key combination 
{BROWSER_BACK} 				Select the browser "back" button 
{BROWSER_FORWARD} 			Select the browser "forward" button 
{BROWSER_REFRESH} 			Select the browser "refresh" button 
{BROWSER_STOP} 				Select the browser "stop" button 
{BROWSER_SEARCH} 			Select the browser "search" button 
{BROWSER_FAVORITES} 			Select the browser "favorites" button 
{BROWSER_HOME} 				Launch the browser and go to the home page 
{VOLUME_MUTE}				Mute the volume 
{VOLUME_DOWN} 				Reduce the volume 
{VOLUME_UP} 				Increase the volume 
{MEDIA_NEXT} 				Select next track in media player 
{MEDIA_PREV} 				Select previous track in media player 
{MEDIA_STOP} 				Stop media player 
{MEDIA_PLAY_PAUSE} 			Play/pause media player 
{LAUNCH_MAIL} 				Launch the email application 
{LAUNCH_MEDIA} 				Launch media player 
{LAUNCH_APP1} 				Launch user app1 
{LAUNCH_APP2} 				Launch user app2 

See Windows' Help--press Win+F1--for a complete list of keyboard shortcuts if you don't know the importance of Alt+F4, PrintScreen, Ctrl+C, and so on.

Example

// To send the ASCII value A (same as pressing ALT+065 on the numeric keypad)
SendKeys('{ASC 065}');
// (When using 2 digit ASCII codes you must use a leading 0
// otherwise an obselete 437 code page is used).


// To send the ASCII value A using its VK virtual key constant
SendKeys('{64}');
// This sends raw VK codes
 
// You can also just press it down
SendKeys('{64 DOWN}');
 
// You can also just press it up
SendKeys('{64 UP}');


// To send UNICODE characters enter the character code
// for example this sends a Chinese character
SendKeys('{ASC 2709}');


// Single keys can also be repeated, e.g.
SendKeys('{DEL 4}'); // Presses the DEL key 4 times
SendKeys('{S 30}'); // Sends 30 'S' characters
SendKeys('+{TAB 4}'); // Presses SHIFT+TAB 4 times


// To hold a key down (generally only useful for games)
SendKeys('{a down}'); // Holds the A key down
SendKeys('{a up}'); // Releases the A key


// To set the state of the capslock, numlock and scrolllock keys
SendKeys('{NumLock on}'); // Turns the NumLock key on
SendKeys('{CapsLock off}'); // Turns the CapsLock key off
SendKeys('{ScrollLock toggle}'); // Toggles the state of ScrollLock


// If you with to use a variable for the count, try
$n = 4;
SendKeys('+{TAB ' . $n . '}');


// If you wish to send the ASCII value A four times, then try
$x = Chr(65);
SendKeys('{' . $x . ' 4}');


// For example, open Folder Options (in the control panel) and try the following:
SendKeys('{TAB}'); // Navigate to next control (button, checkbox, etc)  
SendKeys('+{TAB}'); // Navigate to previous control.  
SendKeys('^{TAB}'); // Navigate to next WindowTab (on a Tabbed dialog window)  
SendKeys('^+{TAB}'); // Navigate to previous WindowTab.  
SendKeys('{SPACE}'); // Can be used to toggle a checkbox or click a button.  
SendKeys('{+}'); // Usually checks a checkbox (if it's a "real" checkbox.)  
SendKeys('{-}'); // Usually unchecks a checkbox. 
SendKeys('{NumPadMult}'); // Recursively expands folders in a SysTreeView32.


// Use Alt-key combos to access menu items. Also, open Notepad and try the following:
SendKeys('!f'); // Send Alt+f, the access key for Notepad's file menu. Try other letters!
SendKeys('{DOWN}'); // Move down a menu.  
SendKeys('{UP}'); // Move up a menu.  
SendKeys('{LEFT}'); // Move leftward to new menu or expand a submenu.  
SendKeys('{RIGHT}'); // Move rightward to new menu or collapse a submenu.


// Sputnik can send all ASCII and Extended ASCII characters (0-255)
// to send UNICODE characters you must use the "ASC" option and the
// code of the character you wish to send (see {ASC} below).
// To send the ASCII value A (same as pressing ALT+065 on the numeric keypad)
SendKeys('{ASC 065}');
// (When using 2 digit ASCII codes you must use a leading 0, otherwise an
// obselete 437 code page is used).
//To send UNICODE characters enter the character code, for example this sends a
// Chinese character
SendKeys("{ASC 2709}");


// The code after {SPK and before } will be executed
// as if it was any other Sputnik code
// in this case the code is lol(); meaning to call
// the function "lol"
SendKeys( 'hello{SPK lol();}XD'  );
Function lol()
{
	println("Hello World!");
}
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox