Vbs basics-Miscellaneous-sendkeys
Simulate a keyboard operation and send one or more key commands to a specified Windows window to control the application running
The format is as follows:Object. sendkeys (string)
Object: Wshshell object
String: Indicates the key command string to be sent, which must be placed in double quotation marks.
Basic Key
Each key is represented by one or more characters.
To specify a single keyboard character, you must press the character key. For example, to indicate the letter A, you can use ""
To indicate multiple characters, you must add another character directly after the character. For example, to indicate a, B, and C, "ABC" can be used as a string.
Special function key
For keys that need to be combined with shift, Ctrl, and ALT, sendkeys uses special characters:
Shift --------- wshshell. sendkeys "+"
CTRL --------- wshshell. sendkeys "^"
Alt --------- wshshell. sendkeys "%"
Since the "+" and "^" characters are used to represent special control keys, how do I represent 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 name.
For example, to send the Enter key, you need to use "wshshell. sendkeys" {enter;
The send-down arrow key is represented by "wshell. sendkeys" {down} ".
Space --------- wshshell. sendkeys ""
Enter --------- wshshell. sendkeys "{enter }"
Certificate --------- wshshell. sendkeys "{right }"
Certificate --------- wshshell. sendkeys "{up }"
F1 --------- wshshell. sendkeys "{F1 }"
Buttons |
Code |
Backspace |
{Backspace }, |
Break |
{Break} |
Caps |
Lock |
Del |
Or |
Down |
Arrow |
End |
{End} |
Enter |
{Enter} or |
ESC |
{ESC} |
Help |
{Help} |
Home |
{Home} |
INS |
Or |
Left |
Arrow |
Num |
Lock |
Page |
Down |
Page |
Up |
Print |
Screen |
Right |
Arrow |
Scroll |
Lock |
Tab |
{Tab} |
Up |
Arrow |
F1 |
{F1} |
F2 |
{F2} |
F3 |
{F3} |
F4 |
{F4} |
F5 |
{F5} |
F6 |
{F6} |
F7 |
{F7} |
F8 |
{F8} |
F9 |
{F9} |
F10 |
{F10} |
If you need to send multiple duplicate single-letter buttons, you do not need to enter the letter again. sendkeys allows you to describe in simplified format, in the format of "{key number }". For example, to send 10 letters "X", enter "wshshell. sendkeys" {x 10} ".
Next let's take a look at the instance:
Press F5 to refresh the Desktop
123 |
Dim WshShell,Path,i Set WshShell = Wscrpt.CreateObject( "Wscrpt.Shell" ) WshShell.SendKeys "{F5}" |
Automatic computer restart
1234 |
Dim WshShell Set WshShell = CreateObject( "Wscrpt.Shell" ) WshShell.SendKeys "^{ESC}u" WshShell.SendKeys "R" |
Start Task Manager
123 |
Dim WshShell Set WshShell = CreateObject( "Wscrpt.Shell" ) WshShell.SendKeys "^+{ESC}" |
Automatic shutdown
123456 |
Dim WshShell Set WshShell=Wscrpt.CreateObject( "Wscrpt.Shell" ) Wscrpt.Sleep 2000 WshShell.Run "shutdown -r -t 120" wscrpt.sleep 6000 WshShell.Run "shutdown -a" |
Enter happy birthday in Notepad! Save as birth.txt
1234567891011121314 |
Dim WshShell Set WshShell=Wscrpt.CreateObject( "Wscrpt.Shell" ) WshShell.Run "notepad" Wscrpt.Sleep 1500 WshShell.AppActivate "Ξ? - ?±" WshShell.SendKeys "Happy Birthdy!!!" Wscrpt.Sleep 500 WshShell.SendKeys "%FS" Wscrpt.Sleep 500 WshShell.SendKeys "birth.txt" Wscrpt.Sleep 500 WshShell.SendKeys "%S" Wscrpt.Sleep 500 WshShell.SendKeys "%FX" |
Simply put,SendKeyThis command is used to simulate a keyboard operation. One or more key commands are sent to a specified Windows window to control the application running. The format is as follows:
Wshshell. sendkeys string"String" indicates the key instruction string to be sent, which must be placed in double quotation marks.
1. Basic keys
Generally, the key commands to be sent can be expressed by the key character. 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, to send the key "cfan", 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 %
3. Combine buttons
If you want to send a combination of keys by pressing CTRL + E at the same time, you need to use wshshell. sendkeys "^ e" indicates that if the combination key to be sent is CTRL and E and C, 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.
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 enclose the key number name in braces. For example, to send the return key, you need to use wshshell. sendkeys "{enter}" indicates that wshshell is used to send the downward direction key. sendkeys "{down}" indicates.
4. Multiple duplicate buttons
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, to send 10 letters "X", enter wshshell. sendkeys "{x 10.
Let the vbs script automatically input a line of text "Hello, welcome to cfan" in the notepad ".
Dim wshshell
Set wshshell = wscript. Createobject ("wscript. Shell ")
Wshshell. Run "Notepad"
Wscript. Sleep 200
Wshshell. appactivate "untitled-Notepad"
Wshshell. sendkeys "Hello, welcome to cfan"
Notepad that can be automatically stored at a scheduled time
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:
'Part 1: define variables and objects
Dim wshshell, autosavetime, txtfilename
Autosavetime = 1000*60*5
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 itNotepad. vbsTo use Notepad, double-click the script file.
Detailed procedures:
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, for a non-system program, you can enter a full path. 3 format 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. run sleep autosavetime repeatedly. 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 the script.
Appendix: 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}