Sending keystrokes to applications
The last-resort way of automating anything using VBScript is to launch a GUI application and just send keystrokes (but not mouse movement or clicks - try AutoIt if you need mouse control, too). Here’s an example, using Notepad, of how sending keystrokes works. Note that this takes a lot of trial, error, and careful documentation of the necessary keystrokes in order to get it working.
- '
- ' ScriptingAnswers.com Essentials - by Don Jones
- '
- ' SendKeys example
- ' This shows how to automate the Windows GUI (or any GUI app)
- ' by sending keystrokes to it from a VBScript
- 'first we'll launch Notepad - this could be ANY app
- 'we'll specify that the script NOT wait for it
- 'to finish running
- Dim objShell
- Set objShell = CreateObject("WScript.Shell")
- objShell.Run "C:\Windows\System32\Notepad.exe",,False
- 'we will wait a second for the app to start
- WScript.Sleep 1000
- 'now we need to activate the windows. we need it's window
- 'title, which by experimentation we've determined Is
- '"Untitled - Notepad." You don't need the EXACT title;
- 'Windows will try to activate the first matching window
- 'it finds
- objShell.AppActivate "Untitled - Notepad"
- 'now we can send keystroked - see the WshShell docs
- 'for details on what this can Do
- 'we'll start with simple text
- objShell.SendKeys "Hello, scripter "
- 'to send a square bracket you have to enclose it in curly braces
- objShell.SendKeys "{[}If indeed you are a scripter{]}"
- 'special keys have a name you can send
- objShell.SendKeys "{HOME}To whom it may concern:{ENTER}{ENTER}"
- 'if you're controlling a GUI, these might be useful:
- ' function keys are {F1}, {F2} and so forth
- ' Use +{F3} for Shift+F3, ^{F4} for Ctrl+F4, and %{F5} for Alt+F5
- ' Tab is {TAB}, backspace is {BACKSPACE}, Enter is {ENTER} pr ~
- ' Escape is {ESC}. The arrows are {LEFT} {RIGHT} {UP} and {DOWN}
- ' You can add + for Shift, ^ for Ctrl, and % for Alt to any of these
- ' Usually, just type the key's face name in {} and you've got it
- ' About the only thing you can't send is PrintScreen
Tags: gui, keystrokes, sendkeys, VBScript Scripts










