how can we simulate keyboard keys using vbs?

南笙酒味 提交于 2021-02-05 11:17:06

问题


I'm aware of the probably most common form:

set wShell = createObject("wscript.shell")
wShell.sendKeys ":){ENTER}"

this uncommon and limited way:

Set ShellApp = CreateObject("Shell.Application")
ShellApp.WindowSwitcher

we can hackishly use sleep if we want a sequence of keys which depends on other events:

WScript.sleep 987
wShell.sendKeys "foo{!}~"
WScript.sleep 789
wShell.sendKeys "^a^c"

and we can't really keep a key pressed but we can repeat it many times:

wShell.sendKeys "{LEFT 42}"

now... am I missing something?


回答1:


yeah, I'm probably missing something.

meanwhile I figured this useful tabled reference might still be useful if shamelessly adapted here:

Most ASCII characters can be represented simply by the character itself.

E.g, the key sequence FRED can be represented by "FRED".

Special keys such as the control keys, function keys etc are encoded with {braces}

................................................................................................................
:     Key/Character     :                SendKey                :                 Description                  :
:.......................:.......................................:..............................................:
: ~                     : {~}                                   : Send a tilde (~)                             :
: !                     : {!}                                   : Send an exclamation point (!)                :
: ^                     : {^}                                   : Send a caret (^)                             :
: +                     : {+}                                   : Send a plus sign (+)                         :
: Backspace             : {BACKSPACE} or {BKSP} or {BS}         : Send a Backspace keystroke                   :
: Break                 : {BREAK}                               : Send a Break keystroke                       :
: Caps Lock             : {CAPSLOCK}                            : Press the Caps Lock Key (toggle on or off)   :
: Clear                 : {CLEAR}                               : Clear the field                              :
: Delete                : {DELETE} or {DEL}                     : Send a Delete keystroke                      :
: Insert                : {INSERT} or {INS}                     : Send an Insert keystroke                     :
: Cursor control arrows : {LEFT} / {RIGHT} / {UP} / {DOWN}      : Send a Left/Right/Up/Down Arrow              :
: End                   : {END}                                 : Send an End keystroke                        :
: Enter                 : {ENTER} or ~                          : Send an Enter keystroke                      :
: Escape                : {ESCAPE}                              : Send an Esc keystroke                        :
: F1 through F16        : {F1} through {F16}                    : Send a Function keystroke                    :
: Help                  : {HELP}                                : Send a Help keystroke                        :
: Home                  : {HOME}                                : Send a Home keystroke                        :
: Page Down             : {PGDN}                                : Send a Page Down keystroke                   :
: Page Up               : {PGUP}                                : Send a Page Up keystroke                     :
: Numlock               : {NUMLOCK}                             : Send a Num Lock keystroke                    :
: Scroll lock           : {SCROLLLOCK}                          : Press the Scroll lock Key (toggle on or off) :
: Print Screen          : {PRTSC}                               : Send a Print Screen keystroke                :
:.......................:.......................................:..............................................:

To specify keys with any combination of SHIFT, CTRL and ALT keys, precede them as following:

For SHIFT prefix with +
For CTRL  prefix with ^
For ALT   prefix with %


来源:https://stackoverflow.com/questions/43168311/how-can-we-simulate-keyboard-keys-using-vbs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!