Keyboard Control
Contents
keyboardPress
keyboardPress(key)
keyboardPress(key, modifier)
Simulates the pressing of the specified key. See Virtual Keys for more information.
If you are running input in attached mode (through use of attach()) it will only send input to the target window, and will not interfere if you decide to switch windows.
If the target program does not appear to be picking up input through the use of this function, you might try calling keyboardSetDelay() somewhere in your script to increase the delay.
'modifier' should be a second key (one of VK_CONTROL, VK_SHIFT, or VK_ALT) to use in a key combination such as ALT+1.
Example
keyboardPress( key.VK_ENTER ); -- The ENTER key will be pressed
keyboardPress( key.VK_1, key.VK_ALT ); -- Press ALT+1
keyboardHold
keyboardHold(key)
Holds the specified key until it is pressed or released (either physically, or virtually). See Virtual Keys for more information.
If you are running input in attached mode (through use of attach()) it will only send input to the target window, and will not interfere if you decide to switch windows.
If the target program does not appear to be picking up input through the use of this function, you might try calling keyboardSetDelay() somewhere in your script to increase the delay.
Example
keyboardHold( key.VK_A ); -- The A key will be held down
yrest(50);
keyboardRelease( key.VK_A ); -- The A key is now released
keyboardRelease
keyboardRelease(key)
Releases the specified key. See Virtual Keys for more information.
If you are running input in attached mode (through use of attach()) it will only send input to the target window, and will not interfere if you decide to switch windows.
If the target program does not appear to be picking up input through the use of this function, you might try calling keyboardSetDelay() somewhere in your script to increase the delay.
Example
keyboardRelease( key.VK_A ); -- A is no longer being held
keyPressed
int keyPressed(key)
Checks if a key is being held down or pressed. Returns a boolean: true if held/pressed, otherwise returns false. See Virtual Keys for more information.
The check for this is done globally, meaning that you do not need to have the calling MicroMacro window, or it's attached window, focused for a keypress to register. If you only want to gather input from MicroMacro or it's attached window, use keyPressedLocal() instead.
Example
if( keyPressed( key.VK_A ) ) then
-- A is pressed. Do something!
end
keyPressedLocal
int keyPressedLocal(key)
Checks if a key is being held down or pressed. Returns a boolean: true if held/pressed, otherwise returns false. See Virtual Keys for more information.
The check for this is done locally, meaning that you must have the calling MicroMacro window, or it's attached window, focused for a keypress to register. If you want to gather input globally, use keyPressed() instead.
Example
if( keyPressedLocal( key.VK_A ) ) then
-- A is pressed. Do something!
end
keyboardType
keyboardType(message)
Types the message specified, as if typed by the user.
Example
keyboardType("This is an example! ;)"); -- This will output the following text: This is an example! ;)
keyboardSetDelay
keyboardSetDelay(time)
Sets the automatic delay between key presses and releases for functions like keyboardType() or keyboardPress(). 'time' is specified in miliseconds. If the target program does not appear to be picking up input well using the current delay, use this function to increase it and give the target program more time to detect the press/release. Note that it will then take longer to process the key press if you increase the delay.
The default keyboard delay is 50ms.
Example
keyboardSetDelay(100);
keyboardType("This is an example! ;)");
-- This will output the string with 100ms delay between each character
getKeyName
string getKeyName(key)
Returns the symbolic name for the key specified. That is, passing key.VK_SPACE to getKeyName() will result in the function returning "SPACE". This function is affected by your computer's language settings.
Example
printf("Keyname: %s\n", getKeyName(key.VK_SPACE));
keyboardBufferClear
keyboardBufferClear()
Flushes any remaining characters left in the keyboard buffer. You typically would do this only before reading information from the standard input stream.
Example
keyboardBufferClear(); -- clear keyboard buffer
io.stdin:flush(); -- clear standard input buffer
-- everything is clean, now time to read input.
printf("Please enter your name: ");
local name = io.stdin:read();
printf("\n\nYour name is %s\n", name);
keyboardState
table keyboardState()
Returns a table describing which keys are pressed and which are not. If you need to check only a single key, you should use keyPressed() instead.
Example
local ks = keyboardState();
if( ks[key.VK_SPACE] ) then
printf("Space bar is pressed.\n");
end