8. VbScript | SendKeys

Remember to try the codes shown down below for practice & for understanding the material. Ask questions in the comments below, if needed.

Today we are going to talk about Sendkeys. Sendkeys sends keyboard key input, just without the physical keyboard.


Sendkeys is very similar to the run command; both use the same base code:


Note that the sendkeys starts typing right after notepad is opened. The problem here is that it takes a few seconds for notepad to open, so not all of the keys will be typed onto the notepad. To fix this, you have to tell the VbScript to wait. You can use the wscript.sleep command, which counts in miliseconds:


To tell sendkeys to hit a specific keyboard key, you use {}



Generally, you can use sendkeys anywhere you can navigate without using the mouse. With this constraint in mind, there is a way to save your notepad after using sendkeys to type in the notepad. Notice how if you press the ALT key while you have the notepad selected, that the the File tab is selected from the menu


Pressing the F key, the File tab opens.


And pressing the S key on your keyboard leads you to the saving menu. Note that the whatever is in the filename is already highlighted, so what you type in next removes what is highlighted. Finally, hitting the enter key saves the file.


Also note that the tab key moves the selection around to different parts of the save menu, including the directory. So you can customize where the file is saved by changing the directory.

Example: (note that % represents the ALT key in VbScript ↓)

  • step1: open notepad
  • step2: tell sendkeys to type “hello there”
  • step3: tell sendkeys to hit the enter button
  • step4: tell sendkeys to type “How are you doing?”
  • step5: Tell sendkeys to press the ALT key, the f key, and the s key.
  • step6: tell sendkeys to save the filename as “test.txt” and to hit the enter key
Example2: Searching on Firefox

  • step1: open an input box that stores your search term as a variable; in this case it is called filler.
  • step2: run firefox
  • step3: enter the keystrokes of filler with sendkeys
  • step4: tell sendkeys to hit the enter keyboard key
Example2 v2: wscript.shell automatically selects internet explorer when told to “run” a website name. www.google.com in this example.
To recap, the Sendkeys command allows you to mimic keyboard key strokes, which in turn allows you to run scripts that able to interact with your computer.

What's Your Opinion?