VBS SENDKEY Usage Sendkey keyboard corresponding to the Code table _vbs

Source: Internet
Author: User
The format used is:
Object. SendKeys string
"Object": Represents the WshShell object
"String": Represents the key command string to be sent, which needs to be placed in double quotes in English.
1. Basic key
In general, the key instructions to be sent can be represented directly by the key character itself, such as sending the letter "X", using the
Wshshell.sendkeys "X" can be.
You can also send a number of key commands directly, just put the key characters in order together. For example, to send the key "Cfan", you can use "Wshshell.sendkeys" Cfan "".
2. Special function keys
For key SendKeys that need to be combined with the shift, Ctrl, alt three control keys, use special characters to indicate:
Special Control key special characters
Shift +
Ctrl ^
Alt%
If you want to send the combination button is pressed at the same time ctrl+e, you need to use the "Wshshell.sendkeys" ^e "" said,
If the combination key you want to send is to press the E and C two keys while holding down the CTRL key, use parentheses to enclose the letters in the form of "Wshshell.sendkeys" ^ (EC) "". Notice it here with "Wshshell.sendkeys" ^ec "" , which means that the combination key is holding down the CTRL and E keys while releasing the CTRL key and pressing the "C" letter key individually
How do you represent these keys because the characters "+" and "^" are used to represent special control buttons? Just enclose the characters in curly braces. For example, to send the plus sign "+", use "Wshshell.sendkeys" {+} "". In addition, for some control function keys that do not generate characters , you also need to use braces to enclose the key number name, such as to send the return key, you need to use the "Wshshell.sendkeys" {ENTER} "" "To send the downward arrow keys with" Wshshell.sendkeys "{Down}" ".
Small Tips
If you need to send multiple duplicate single letter keystrokes, you do not have to enter the letter repeatedly, Sendkdys allows you to use a simplified format for the description, using the format "{Key number}". For example, to send a 10 letter "x", enter "Wshshell.sendkeys" {x 10} "".
3. Examples of hands-on translation
"Wshshell.sendkeys" ^{esc}u "" "The Translation: Press the CTRL+ESC key combination (equivalent to press the win key) Open the" Start "menu, press the U key to open the" Shutdown "menu.
---Just do It---------------------------------
Let the VBS script automatically enter a line of text in the word "Hello, Welcome to Cfan".
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 Word, WPS, such as automatic timing disk function, in fact, using the VBS script PLUS SendKeys command, can make up for this regret. Open Notepad and enter the following (for easy description and analysis, dividing the code into four parts):
' The first part: defining Variables and objects
Dim WshShell, Autosavetime, Txtfilename
autosavetime=300000
Set wshshell=wscript.createobject ("Wscript.Shell")
Txtfilename=inputbox ("Please enter the name of the file you want to create (not in Chinese and plain numbers):"
' Part Two: open and activate Notepad
Wshshell.run "Notepad"
Wscript.Sleep 200
Wshshell.appactivate "Untitled-Notepad"
' Part Three: Save with the file name you entered
Wshshell.sendkeys "^s"
Wscript.Sleep 300
Wshshell.sendkeys Txtfilename
Wscript.Sleep 300
Wshshell.sendkeys '%s '
Wscript.Sleep Autosavetime
' Part Four: automatic timing disk
While Wshshell.appactivate (Txtfilename) =true
Wshshell.sendkeys "^s"
Wscript.Sleep Autosavetime
Wend
Wscript.Quit
Save it as Notepad, and when you want to use Notepad later, open it by double-clicking the script file.
Program translation
The basic idea of this script is to send the ctrl+s to Notepad periodically.
Part One: Defines the variables and objects that are needed in the script. The autosavetime variable is used to set the automatic disk interval, in milliseconds, and set to 5 minutes. Txtfilename variable gets the name of the text file you want to create by entering the box.
Part II: Run Notepad, for Windows itself provided programs, such as calculators, can be directly in the "Wshshell.run" after the input program name, such as "Calc" for non-system programs, you can enter a full path, but pay attention to the use of 8.3 format input, such as "" D:\ Progra~1\tencent\qq.exe ""
Part III: This procedure is performed using the SendKeys command (note the use of the delay command between each operation):
Press Ctrl+s key in Notepad → pop-up save file window → enter filename → press alt+s key to save (the default is saved in the My Documents directory).
Part IV: The key to timing the disk, through the "while ... Wend "This when the condition is true" cyclic command, implementation of the automatic disk code "Wshshell.sendkeys" ^s "" "and Timed Code" Wscript.Sleep autosavetime "repeated execution. Because you can't let this timed disk loop run all the time, After exiting Notepad, you must automatically exit the script and end the loop, so you have designed a cyclic judgment condition, "Wshshell.appactivate txtfilename=true", which, when Notepad is running, activates the Notepad window, which runs with the result "True". The timer disk loop is always executed, after exiting Notepad, the script cannot activate the Notepad window, it will loop out and execute the "Wscript.Quit" Exit script after "Wend".
Code table corresponding to 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}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.