In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article is about how to use SendKey in vbs. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
The format of its use is:
Object.SendKeys string
"object": represents a WshShell object
"string": indicates the keystroke instruction string to be sent, which needs to be enclosed in English double quotes.
1. Basic key
In general, the keystroke instruction to be sent can be directly represented by the keystroke character itself, such as to send the letter "x", using the
WshShell.SendKeys "x" is fine.
You can also send multiple keystroke instructions directly, just put the keystroke characters together in order. For example, to send the button "cfan", you can use "WshShell.SendKeys" cfan "."
two。 Special function key
The key SendKeys that needs to be combined with the three control keys Shift, Ctrl, and Alt is represented by special characters:
Special control key special character
Shift +
Ctrl ^
Alt%
If the combined key to be sent is to press Ctrl+E at the same time, it needs to be indicated by "WshShell.SendKeys" ^ e "".
If the key combination to be sent is to hold down the Ctrl key while pressing the E and C keys, use parentheses to enclose the letters in the format "WshShell.SendKeys" ^ (ec) "". Note the difference between it and "WshShell.SendKeys" ^ ec "", which means that the combined keys press and hold down the Ctrl and E keys at the same time, then release the Ctrl key and press the "C" key alone.
Since the characters "+" and "^" are used to represent special control keys, how do you represent these keys? 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 curly braces to enclose the key number name. For example, to send an enter key, you need to use "WshShell.SendKeys" {ENTER} "", and the direction key sent down is indicated by "WshShell.SendKeys" {DOWN} ".
Tips
If you need to send multiple duplicate single-letter keys, you don't have to type the letter repeatedly. SendKdys allows you to describe it in a simplified format, using the format "{keystroke number}". For example, to send 10 letters "x", enter "WshShell.SendKeys" {x 10} ".
3. Translation of practical examples
Press the Ctrl+Esc key combination (equivalent to pressing the win key) to open the start menu and press the U key to open the shutdown menu.
-Just Do It--
Let the VBS script automatically enter a line of text "Hello, welcome to cfan" into the lexicon.
Dim WshShell
Set WshShell=WScript.CreateObject ("WScript.Shell")
WshShell.Run "notepad"
WScript.Sleep 200
WshShell.AppActivate "No title-notepad"
WshShell.SendKeys "hello, welcome to cfan"
Our most commonly used notepad does not have the automatic timing save function such as Word and WPS. In fact, we can make up for this regret by using VBS script plus SendKeys command. Open notepad and enter the following (divide the code into four parts for easy description and analysis):
'part I: define variables and objects
Dim WshShell, AutoSaveTime, TXTFileName
AutoSaveTime=300000
Set WshShell=WScript.CreateObject ("WScript.Shell")
TXTFileName=InputBox ("Please enter the file name you want to create (not in Chinese and pure numbers):")
Part II: open and activate notepad
WshShell.Run "notepad"
WScript.Sleep 200
WshShell.AppActivate "No title-notepad"
Part III: save the disk with the file name entered
WshShell.SendKeys "^ s"
WScript.Sleep 300
WshShell.SendKeys TXTFileName
WScript.Sleep 300
WshShell.SendKeys 's'
WScript.Sleep AutoSaveTime
Part IV: automatic timing 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 send the save key Ctrl+S to notepad regularly.
The first part defines the variables and objects that need to be used in the script. The "AutoSaveTime" variable is used to set the automatic save interval in milliseconds, which is set here to 5 minutes. The "TXTFileName" variable gets the name of the text file you want to create through the input box.
The second part: run notepad. For programs provided by Windows itself, such as calculators, you can enter the program name directly after "WshShell.Run". For example, "calc" for non-system programs, you can enter the full path, but you should pay attention to using the 8.3 format, such as "D:\ Progra~1\ Tencent\ QQ.exe".
Part 3: this operation flow is performed here with the SendKeys command (note the use of the delay command between each operation):
Press the Ctrl+S key combination in notepad → pop-up window to save the file → input file name → press the Alt+S key combination to save (the default is saved in the "my documents" directory).
The fourth part: the key of timing saving, through the cyclic command "While.Wend" when the condition is "true", the repeated execution of automatic save code "WshShell.SendKeys" ^ s "" and timing code "WScript.Sleep AutoSaveTime" is realized. Because this timed save loop cannot be executed all the time, after exiting notepad, it is necessary to automatically exit the script and end the loop, so a loop judgment condition "WshShell.AppActivate TXTFileName=True" is designed. When notepad is running, the notepad window can be activated. The result of this condition is "True", and the timed save loop is executed all the time. After exiting notepad, the script cannot activate the notepad window and will exit the loop. 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}
CAPSLOCK {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}
NUMLOCK {NUMLOCK}
PAGE DOWN {PGDN}
PAGE UP {PGUP}
PRINT SCREEN {PRTSC}
RIGHT ARROW {RIGHT}
SCROLLLOCK {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}
Thank you for reading! This is the end of the article on "how to use SendKey in vbs". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.