API Reference
Complete reference for all functions available in MoonKey Lua scripts.
Logging
log(message)
Prints a message to console.
Parameters:
message(string) - Text to print
Example:
Hotkey Management
bind(modifiers, key, callback, [windowTitle])
Registers a global hotkey.
Parameters:
modifiers(number) - Modifier keys (useMODconstants)key(number) - Virtual key code (useKEYconstants)callback(function) - Function to execute when hotkey is pressedwindowTitle(string, optional) - Only trigger when this window is active
Examples:
bind(MOD.ALT, KEY.F1, function() end) -- Alt + F1
bind(MOD.CTRL + MOD.SHIFT, KEY.T, function() end) -- Ctrl + Shift + T
bind(MOD.WIN, KEY.R, function() end) -- Windows + R
Input Simulation
send(key)
Simulates a key press.
Parameters:
key(number) - Virtual key code (useKEYconstants)
Example:
Notes:
- Simulates pressing and immediately releasing a key
- Equivalent to a quick tap on the keyboard
- Uses Windows SendInput() with KEYDOWN and KEYUP events
- For modifier keys, consider holding them with multiple calls
write(text)
Types text as if from keyboard.
Parameters:
text(string) - String to type
Example:
Notes:
- Simulates typing complete strings with proper shift key handling for uppercase and special characters
- Includes small delays between keystrokes for reliability with some applications
- Handles shift states for characters like @, !, %, etc.
- Includes 15ms delay between keystrokes
- Not suitable for extremely fast typing simulation
Window Management
focus(windowTitle)
Brings window to foreground and gives it focus.
Parameters:
windowTitle(string) - Window title or class name to find
Example:
Notes:
- Searches for a window with the specified title or class name and attempts to bring it to the foreground
- Tries two search methods:
- By window title (caption)
- By window class name
- Uses FindWindow() with NULL class name, then with NULL window name
- Shows window with SW_RESTORE if minimized
- May fail due to Windows security restrictions on foreground changes
Warning
- Some applications block foreground window changes for security
- UWP/Store apps may not be accessible with this method
Timing Functions
wait(seconds)
Pauses script execution (seconds).
Parameters:
seconds(number) - Time to wait in seconds (can be fractional)
Example:
sleep(milliseconds)
Pauses script execution (milliseconds).
Parameters:
milliseconds(number) - Time to wait in milliseconds
Example:
set_interval(ms, callback, [targetWindow])
Sets a repeating timer.
Parameters:
ms(number) - Interval in milliseconds between callback executionscallback(function) - Lua function to call when timer tickstargetWindow(string, optional) - Optional window title for context-sensitive timers
Example:
Notes:
- If targetWindow is specified, the callback will only execute when that window is active
- If empty, the timer will execute regardless of active window
Mouse Control
mouse_move(x, y)
Moves cursor to X, Y coordinates.
Parameters:
x(number) - X coordinate (0 = left edge, screen width = right edge)y(number) - Y coordinate (0 = top edge, screen height = bottom edge)
Example:
Notes:
- Moves cursor instantly to specified position using absolute coordinates
- Coordinates are transformed to Windows' virtual screen space (0-65535)
- Uses MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE flags
- Coordinates outside screen bounds are clamped by Windows
mouse_click(button)
Clicks mouse button.
Parameters:
button(number) - Button to click0= left1= right2= middle
Example:
Notes:
- Simulates pressing and immediately releasing the specified mouse button
- Includes both down and up events for complete click simulation
mouse_pos()
Returns current mouse position.
Returns:
- Lua table with
{x = number, y = number}
Example:
Notes:
- Returns the current cursor position in screen coordinates
- Position is relative to the primary monitor