Input & Time
Functions for reading keyboard, mouse, and time values in Like2D.
Keyboard Input
isKeyDown(keyName)
Checks if a specific key is currently held down.
Parameters:
keyName(string): Name of the key (e.g., "Escape", "Space", "W", "Left")
Returns:
held(boolean): true if key is held, false otherwise
isKeyJustPressed(keyName)
Checks if a specific key was just pressed this frame.
Parameters:
keyName(string): Name of the key
Returns:
pressed(boolean): true if key was just pressed, false otherwise
isKeyJustReleased(keyName)
Checks if a specific key was just released this frame.
Parameters:
keyName(string): Name of the key
Returns:
released(boolean): true if key was just released, false otherwise
Example:
if isKeyJustPressed("Space") then
player.jump()
end
if isKeyDown("Right") then
player.x = player.x + 5
end
Mouse Input
isMousePressed(button)
Checks if a mouse button is currently held down.
Parameters:
button(integer): Button index —0= left click,1= right click,2= middle click
Returns: held (boolean)
if isMousePressed(0) then
print("Left mouse button is held")
end
if isMousePressed(1) then
print("Right click held")
end
getMousePos()
Returns the current mouse cursor position in screen coordinates.
Returns: x (integer), y (integer)
local mx, my = getMousePos()
print("Mouse at: " .. mx .. ", " .. my)
getMouseScroll()
Returns the scroll wheel delta since the last frame. Horizontal scroll is x, vertical scroll is y (positive = scroll up/right, negative = scroll down/left).
Returns: x (number), y (number)
local sx, sy = getMouseScroll()
if sy > 0 then
print("Scrolled up")
elseif sy < 0 then
print("Scrolled down")
end
Time Functions
getDeltaTime()
Returns the time elapsed since the last frame, in seconds. This is the most important function for smooth, frame-rate-independent game logic. At 60 FPS, this returns approximately 0.016.
Returns: dt (number): Seconds since last frame
local dt = getDeltaTime()
-- Move at 200 pixels/second regardless of frame rate
player.x = player.x + 200 * dt
getTime()
Returns the total time in milliseconds since the application started. Useful for timing events, cooldowns, and animations.
Returns: time (integer): Milliseconds since start
local start = getTime()
-- ... do something ...
local elapsed = getTime() - start
print("Took " .. elapsed .. "ms")
Utility Functions
print(message)
Outputs a message to the console (debug mode only). Only works when running with LikeC.exe.
print("Debug: Player position updated")
print("Score: " .. score)
dofile(filename)
Loads and executes a Luau script file. Use this to split code across multiple files.
-- In main.luau
dofile("anim.luau")
dofile("scripts/player.luau")
function onInit()
anim_init() -- Function defined in anim.luau
end
Key Name Reference
Use these string names with isKeyDown(), isKeyJustPressed(), and isKeyJustReleased():
Letters and Numbers
"a" through "z" -- All 26 lowercase letters
"0" through "9" -- All 10 number keys
Special Keys
"Space", "Return", "Escape", "Tab", "Backspace", "Delete"
"LShift", "RShift", "LCtrl", "RCtrl", "LAlt", "RAlt"
"Home", "End", "PageUp", "PageDown", "Insert"
"F1" through "F12"
Arrow Keys
"Up", "Down", "Left", "Right"
Note: Key names are case-sensitive strings. Use exactly the names shown above.