The Onemacro Lua API is a set of functions using the Lua programming language and provides advanced scripting functionality for any mice and keyboards with the Onemacro program.
This page assumes a working knowledge of the Lua programming language. Further information can be obtained from www.lua.org.
The script is invoked using an event handler, OnEvent. Users may examine the various events exposed in this handler to perform their desired actions.
The OnEvent() function serves as the event handler for the script. You will need to implement this function.
function OnEvent(event, arg)
end
Event | arg | Description |
---|---|---|
"PROFILE_ACTIVATED" | None | Profile has been activated. This is the first event seen. |
"PROFILE_DEACTIVATED" | None | Profile has been deactivated. This is the last event seen |
“MOUSE_BUTTON_PRESSED” | 2=Mouse Button 2 3=Mouse Button 3 4=Mouse Button 4 ... | Profile has been deactivated. This is the last event seen |
“MOUSE_BUTTON_RELEASED” | 2=Mouse Button 2 3=Mouse Button 3 4=Mouse Button 4 ... | NOTE: Left Mouse Button (1) is not reported by default. Use ‘EnablePrimaryMouseButtonEvents’ to override this. |
-- This is the primary event handler
-- You must implement this function
function OnEvent(event, arg)
if (event == "PROFILE_ACTIVATED") then
--profile has been activated
end
if (event == "PROFILE_DEACTIVATED") then
--profile has been deactivated
end
if (event == "MOUSE_BUTTON_PRESSED" and arg == 3) then
--Mouse Button 3 has been pressed
End
if (event == "MOUSE_BUTTON_RELEASED" and arg == 3) then
--Mouse Button 3 has been released
end
end
Sleep() will cause the script to pause for the desired amount of time.
Sleep( timeout );
-- Sleeping for 20 milliseconds
Sleep(20)
The PressKey() function is used to simulate a keyboard key press. NOTE: Calling IsModifierPressed or IsKeyLockOn immediately afterwards for a simulated modifier or lock key will likely return the previous state. It will take a few milliseconds for the operation to complete.
PressKey( scancode [,scancode] );
PressKey( keyname [,keyname] );
-- Simulate "a" pressed using the scancode
PressKey(30)
-- Simulate "a" pressed using the keyname
PressKey("a")
-- Simulate "a" and "b" being pressed
PressKey("a", "b")
The ReleaseKey() function is used to simulate a keyboard key release.
ReleaseKey( scancode [,scancode] );
ReleaseKey( keyname [,keyname] );
-- Simulate "a" released using the scancode
ReleaseKey(30)
-- Simulate "a" released using the keyname
ReleaseKey("a")
-- Simulate "a" and "b" being released
ReleaseKey("a", "b")
The PressAndReleaseKey() function is used to simulate a keyboard key press followed by a release. NOTE: Calling IsModifierPressed or IsKeyLockOn immediately afterwards for a simulated modifier or lock key will likely return the previous state. It will take a few milliseconds for the operation to complete.
ReleaseKey( scancode [,scancode] );
ReleaseKey( keyname [,keyname] );
-- Simulate "a" pressed and released using the scancode
PressAndReleaseKey(30)
-- Simulate "a" pressed and released using the keyname
PressAndReleaseKey("a")
-- Simulate "a" and "b" being pressed and released
PressAndReleaseKey("a", "b")
The IsModifierPressed() function is used to determine if a particular modifier key is currently in a pressed state.
boolean IsModifierPressed ( keyname );
Modifier | Description |
---|---|
"lalt", "ralt", "alt" | Left, right, or either Alt key |
"lshift", "rshift", "shift" | Left, right, or either Shift key |
"lctrl", "rctrl", "ctrl" | Left, right, or either Ctrl key |
-- Press a specific modifier
PressKey("lshift")
if IsModifierPressed("shift") then
--shift is pressed
end
-- Release the key so it is no longer pressed
ReleaseKey("lshift")
if not IsModifierPressed("shift") then
--shift is not pressed
end
The PressMouseButton() function is used to simulate a mouse button press. NOTE: Calling IsMouseButtonPressed immediately afterwards, will likely return the previous state. It will take a few milliseconds for the operation to complete.
PressMouseButton( button )
Button Value | Location |
---|---|
1 | Left Mouse Button |
2 | Middle Mouse Button |
3 | Right Mouse Button |
-- Simulate left mouse button press
PressMouseButton(1)
-- Simulate right mouse button press
PressMouseButton(3)
The ReleaseMouseButton() function is used to simulate a mouse button release
ReleaseMouseButton( button )
Button Value | Location |
---|---|
1 | Left Mouse Button |
2 | Middle Mouse Button |
3 | Right Mouse Button |
-- Simulate a left mouse button click (press and release)
PressMouseButton(1)
ReleaseMouseButton(1)
The PressAndReleaseMouseButton() function is used to simulate a mouse button press followed by a release. NOTE: Calling IsMouseButtonPressed immediately afterwards, will likely return the previous state. It will take a few milliseconds for the operation to complete.
PressAndReleaseMouseButton( button )
Button Value | Location |
---|---|
1 | Left Mouse Button |
2 | Middle Mouse Button |
3 | Right Mouse Button |
-- Simulate a left mouse button click (press and release)
PressAndReleaseMouseButton(1)
The IsMouseButtonPressed() function is used to determine if a particular mouse button is currently in a pressed state.
boolean IsMouseButtonPressed( button )
Button Value | Location |
---|---|
1 | Left Mouse Button |
2 | Middle Mouse Button |
3 | Right Mouse Button |
-- Press a mouse button
PressMouseButton(1)
if IsMouseButtonPressed(1) then
--Left mouse button is pressed
end
-- Release the button so it is no longer pressed
ReleaseMouseButton(1)
if not IsMouseButtonPressed(1) then
--Left mouse button is not pressed
The MoveMouseTo() function is used to move the mouse cursor to an absolute position on the screen.
MoveMouseTo( x, y, )
-- Move mouse to upper, left corner
MoveMouseTo(0, 0)
-- Move mouse to center of screen
MoveMouseTo(32767, 32767)
-- Move mouse to lower, right corner
MoveMouseTo(65535, 65535)
The MoveMouseWheel() function is used to simulate mouse wheel movement.
MoveMouseWheel( click )
-- Simulate mouse wheel 3 clicks up
MoveMouseWheel(3)
-- Simulate mouse wheel 1 click down
MoveMouseWheel(-1)
The MoveMouseRelative() function is used to simulate relative mouse movement.
MoveMouseRelative( x, y, )
-- Simulate relative mouse movement upwards in 1 pixel increments
for i = 0, 50 do
MoveMouseRelative(0, -1)
Sleep(8)
end
The MoveMouseToVirtual() function is used to move the mouse cursor to an absolute position on a multi-monitor screen layout.
MoveMouseToVirtual( x, y, )
-- Move mouse to upper, left corner of virtual desktop
MoveMouseToVirtual(0, 0)
-- Move mouse to center of virtual desktop
MoveMouseToVirtual (32767, 32767)
-- Move mouse to lower, right corner of virtual desktop
MoveMouseToVirtual (65535, 65535)
The IsKeyLockOn() function used to determine if a particular lock button is currently in an enabled state.
IsKeyLockOn( key )
Key name | Location |
---|---|
"scrolllock" | Scroll Lock |
"capslock" | Caps Lock |
"numlock" | Number Lock |
-- Check if the numlock is on and turn it off if it is
if ( IsKeyLockOn("numlock" ) then
PressAndReleaseKey("numlock");
end
EnablePrimaryMouseButtonEvents() enables event reporting for mouse button 1.
EnablePrimaryMouseButtonEvents(enable);
-- Enable event reporting for mouse button 1
EnablePrimaryMouseButtonEvents(true);
-- Disable event reporting for mouse button 1
EnablePrimaryMouseButtonEvents(false);
The following standard library functions are supported:
Table of scancodes and keynames used in PressKey(), ReleaseKey(), IsModifierPressed().
Keyname | Scancode (hex) |
---|---|
"escape" | 0x01 |
"f1" | 0x3b |
"f2" | 0x3c |
"f3" | 0x3d |
"f4" | 0x3e |
"f5" | 0x3f |
"f6" | 0x40 |
"f7" | 0x41 |
"f8" | 0x42 |
"f9" | 0x43 |
"f10" | 0x44 |
"f11" | 0x57 |
"f12" | 0x58 |
"f13" | 0x64 |
"f14" | 0x65 |
"f15" | 0x66 |
"f16" | 0x67 |
"f17" | 0x68 |
"f18" | 0x69 |
"f19" | 0x6a |
"f20" | 0x6b |
"f21" | 0x6c |
"f22" | 0x6d |
"f23" | 0x6e |
"f24" | 0x76 |
"printscreen" | 0x137 |
"scrolllock" | 0x46 |
"pause" | 0x146 |
"tilde" | 0x29 |
"1" | 0x02 |
"2" | 0x03 |
"3" | 0x04 |
"4" | 0x05 |
"5" | 0x06 |
"6" | 0x07 |
"7" | 0x08 |
"8" | 0x09 |
"9" | 0x0a |
"0" | 0x0b |
"minus" | 0x0c |
"equal" | 0x0d |
"backspace" | 0x0e |
"tab" | 0x0f |
"q" | 0x10 |
"w" | 0x11 |
"e" | 0x12 |
"r" | 0x13 |
"t" | 0x14 |
"y" | 0x15 |
"u" | 0x16 |
"i" | 0x17 |
"o" | 0x18 |
"p" | 0x19 |
"lbracket | 0x1a |
"rbracket" | 0x1b |
"backslash" | 0x2b |
"capslock" | 0x3a |
"a" | 0x1e |
"s" | 0x1f |
"d" | 0x20 |
"f" | 0x21 |
"g" | 0x22 |
"h" | 0x23 |
"j" | 0x24 |
"k" | 0x25 |
"l" | 0x26 |
"semicolon | 0x27 |
"quote" | 0x28 |
"enter" | 0x1c |
"lshift" | 0x2a |
"non_us_slash" | 0x56 |
"z" | 0x2c |
"x" | 0x2d |
"c" | 0x2e |
"v" | 0x2f |
"b" | 0x30 |
"n" | 0x31 |
"m" | 0x32 |
"comma" | 0x33 |
"period" | 0x34 |
"slash" | 0x35 |
"rshift" | 0x36 |
"lctrl" | 0x1d |
"lgui" | 0x15b |
"lalt" | 0x38 |
"spacebar" | 0x39 |
"ralt" | 0x138 |
"rgui" | 0x15c |
"appkey" | 0x15d |
"rctrl" | 0x11d |
"insert" | 0x152 |
"home" | 0x147 |
"pageup" | 0x149 |
"delete" | 0x153 |
"end" | 0x14f |
"pagedown" | 0x151 |
"up" | 0x148 |
"left" | 0x14b |
"down" | 0x150 |
"right" | 0x14d |
"numlock" | 0x45 |
"numslash" | 0x135 |
"numminus" | 0x4a |
"num7" | 0x47 |
"num8" | 0x48 |
"num9" | 0x49 |
"numplus" | 0x4e |
"num4" | 0x4b |
"num5" | 0x4c |
"num6" | 0x4d |
"num1" | 0x4f |
"num2" | 0x50 |
"num3" | 0x51 |
"numenter" | 0x11c |
"num0" | 0x52 |
"numperiod" | 0x53 |