Skip to content

OnMouseButton

A mouse button went down or came up while the player’s cursor is up.

Every press (pressed is true) and release (false) of the left, right or middle button while the cursor is up, with the pointer’s place in the UI space (GetScreenSize: 1080 rows high) - what a menu needs for a drag, a slider or a button that acts on the release. A press this heard always gets its release: if the cursor goes away with the button still down (one of the game’s own menus, the cursor key), the release comes right before OnCursorChange. A release whose press came before the cursor was up is not reported.

The button is only observed - it also reaches what is on screen under the pointer - unless the script holds the mouse with SetMouseCapture: then it is the script’s alone.

function OnMouseButton(button, pressed, x, y)
-- ...
end
Parameter Type
button string "left", "right" or "middle"
pressed boolean true when the button went down, false when it came up
x number the pointer’s place across, in the UI space
y number the pointer’s place down, in the UI space (0 at the top, 1080 at the bottom)

nothing

-- a slider of the script's own: 300 wide at 100, 500
local dragging = false
local function setFromX(x)
local v = math.max(0, math.min(1, (x - 100) / 300))
SetUiSize("slider_fill", 300 * v, 20)
end
function OnMouseButton(button, pressed, x, y)
if button ~= "left" then return end
if pressed and x >= 100 and x <= 400 and y >= 500 and y <= 520 then
dragging = true
setFromX(x)
elseif not pressed then
dragging = false
end
end
function OnMouseMove(x, y)
if dragging then setFromX(x) end
end

OnMouseMove · OnMouseWheel · OnCursorClick · SetMouseCapture · the The game’s UI for a script group of the index