By using this site you agree to the use of cookies by Brugbart and our partners.

Learn more

Automating the Keyboard

How to automate keystrokes using autoit, without really using the keyboard.

Edited: 2015-02-13 00:36

Next: Automating the Mouse

AutoIt Logo

AutoIt also allows you to automate the keyboard input, or rather, to send keystrokes without really involving the keyboard at all. Sending keystrokes is done using the send function of AutoIt – if a flag value of 1 is used rather than a value of 0, keys will be sent raw – otherwise certain keys will have special meaning.

Automating the keyboard is really simple, so this will be a very short AutoIt Tutorial.

Keyboard Automation With AutoIt

The first example we will be dealing with, is showing how to write in notepad by sending keystrokes using the send function. When sending keystrokes, it is important that the window you want to send input to is active – to make sure of this, you can use the WinWaitActive function.

Open a new text document in notepad, then run the below script – the script will automatically write "" as soon as you activate the notepad window.


Sending Keystrokes and Keycodes

Certain keys can only be pressed if you use their keycodes – a list of keycodes can be found in the reference. To press the enter key, you need to use curly brackets around the enter word, which in this case will create a new line. Of course the function of the enter key depends on what you are trying to automate.

Send("Line 1{enter}Line 2")

Some characters can only be typed if you hold down shift, and you can also be limited by the keyboard layout used on a given system. It is possible to type these characters by first pressing shift. I.e.

Send("Black is +3000000 in hex..")

Should give you the hex code of black.

If you set the flag to 1 rather than its default, it will be far easier to send exactly the input you want. I.e.

Send("Black is #000000 in hex..{enter}Line 2?", 1)

But as you can see, you won't be able to send certain keystroke combinations.