The format is as follows:
Object. sendkeys string
"Object": wshshell object
"String": the string of the key command to be sent, which must be placed in double quotation marks.
1. Basic Key
Generally, the key commands to be sent can be expressed by the key character itself. For example, to send the letter "X", use
Wshshell. sendkeys "X.
You can also directly send multiple key commands. You only need to sort the key characters in order. for example, you can use "wshshell. sendkeys "cfan "".
2. special function keys
Use special characters for the keys sendkeys that need to be combined with shift, Ctrl, and ALT:
Special Control Key special characters
Shift +
CTRL ^
Alt %
If you want to send a combination of keys by pressing CTRL + E at the same time, you need to use "wshshell. sendkeys" ^ e,
If the combination of keys to be sent is to press the ctrl key and the E and C keys at the same time, enclose the letters in parentheses in the format of "wshshell. sendkeys "^ (EC )"". pay attention to it and "wshshell. the difference between sendkeys "^ EC", which means that the combination of keys is to hold down the CTRL and E keys at the same time, then release the ctrl key, separately press the "c" keys key
Since the "+" and "^" characters are used to represent special control keys, how do we express these keys? You only need to enclose these characters in braces. for example, to send the plus sign "+", you can use "wshshell. sendkeys "{+ }"". in addition, for some control buttons that do not generate characters, you also need to use braces to enclose the key number name. For example, to send the return key, you need to use "wshshell. sendkeys "{enter}" "indicates that" wshshell. sendkeys "{down}" "indicates.
Tips
If you need to send multiple duplicate single-letter buttons, you do not need to enter the letter again. sendkdys allows you to describe the key in simplified format, in the format of "{key number }". for example, if you want to send 10 letters "X", enter "wshshell. sendkeys "{x 10.
3. Examples of getting started with translation
"Wshshell. sendkeys "^ {ESC} U" press Ctrl + ESC (equivalent to pressing win) to open the "Start" menu, press u to open the "shutdown" menu.
--- Just Do It ---------------------------------
Let the vbs script automatically input a line of text "Hello, welcome to cfan" in the text ".
Dim wshshell
Set wshshell = wscript. Createobject ("wscript. Shell ")
Wshshell. Run "Notepad"
Wscript. Sleep 200
Wshshell. appactivate "untitled-Notepad"
Wshshell. sendkeys "Hello, welcome to cfan"
Our most commonly used notepad does not have the automatic timing storage function like word or Wps. In fact, we can make up for this regret by adding the sendkeys command to the vbs script. open notepad and enter the following content (for easy description and analysis Code Is divided into four parts ):
'Part 1: define variables and objects
Dim wshshell, autosavetime, txtfilename
Autosavetime = 300000
Set wshshell = wscript. Createobject ("wscript. Shell ")
Txtfilename = inputbox ("enter the name of the file you want to create (Chinese and pure numbers are not supported ):")
'Part 2: open and activate notepad
Wshshell. Run "Notepad"
Wscript. Sleep 200
Wshshell. appactivate "untitled-Notepad"
'Part 3: Save the disk with the input file name
Wshshell. sendkeys "^ s"
Wscript. Sleep 300
Wshshell. sendkeys txtfilename
Wscript. Sleep 300
Wshshell. sendkeys "% s"
Wscript. Sleep autosavetime
'Part 4: Automatic timed disk storage
While wshshell. appactivate (txtfilename) = true
Wshshell. sendkeys "^ s"
Wscript. Sleep autosavetime
Wend
Wscript. Quit
Save it as Notepad. vbs. When you want to use Notepad later, double-click the script file to open it.
Program Translation
The basic idea of this script is to regularly send Ctrl + S to the notebook.
Part 1: defines the variables and objects needed in the script. the "autosavetime" variable is used to set the Automatic Storage interval, in milliseconds. Here it is set to 5 minutes. the "txtfilename" variable uses the input box to obtain the name of the text file you want to create.
Part 2: Run notepad. For programs provided by windows, such as calculators, you can directly access wshshell. run and enter the program name. For example, if "calc" is not a system program, you can enter a full path. However, you must use the 8.3 format for input, such as "D: \ progra ~ 1 \ Tencent \ qq.exe ""
Part 3: The sendkeys command is used to execute such an operation procedure (note the use of the delayed command between each operation ):
In notepad, press Ctrl + S to save the file. In the displayed window, enter the file name, and press Alt + S to save the file. The file is saved in the my documents directory by default ).
Part 4: The Key to timed disk storage ...... wend "when the condition is" true ", the cyclic command is used to implement automatic disk storage code" wshshell. sendkeys "^ s" "and timing code" wscript. repeated execution of sleep autosavetime. because this timed storage cycle cannot be executed all the time, after exiting notepad, the script must be automatically exited and the loop must be ended. Therefore, a circular judgment condition "wshshell is designed. appactivate txtfilename = true ". When notepad is running, you can activate the notepad window. The running result of this condition is" true ". The periodical inventory cycle is always executed. After you exit notepad, if the script cannot activate the notepad window, it will generate a loop and execute "wscript" next to "Wend. quit "exits the script.
Code table corresponding to the sendKey keyboard in vbs
key code
----------------------------
SHIFT +
CTRL ^
alt %
backspace {backspace}, {BS }, or {bksp}
break {break}
Caps Lock {capslock}
Del or delete {Delete} or {del}
down arrow {down}
end {end}
enter {enter} Or ~
ESC {ESC}
help {help}
Home {home}
ins or insert {insert} or {ins}
left arrow {left}
Num Lock {numlock}
page down {pgdn}
page up {pgup}
Print Screen {prtsc}
right arrow {right}
scroll lock {scrolllock}
tab {tab}
up arrow {up}
F1 {F1}
F2 {F2}
F3 {F3}
F4 {F4}
F5 {F5}
F6 {F6}
F7 {F7}
F8 {F8}
F9 {F9}
F10 {F10}
F11 {F11}
F12 {F12}
f13 {f13}
F14 {F14}
F15 {F15}
F16 {F16}