How can I retrieve mouse position? E.g. local mouseX = mousePos.x

Does anyone know of a way to reference mouse position in Lua script? I roughly know how to listen for mouse events, but I can’t find any information about mouse position.

I’d love it if I could access the x and y positions of the mouse on the screen and assign them to local variables.

I’m trying to write a script where a user can paint a selection onto a sprite by having the user paint with the pencil tool, scanning for the fg color on the sprite, storing the pixel locations to an array, undoing their pencil tool stroke, and then building a selection exactly where they had drawn. I can do basically everything I need without referencing the mouse position… However, mouse position is of interest to me so that I can construct a bounding box around where the user has drawn without having to scan the whole canvas (I want this tool to work on large artwork).

--Here is the pseudo-code where I would like to reference mouse position. mouseMinX, mouseMinY, mouseMaxX, mouseMaxY are global variables.

function recordBrushExtrema ()

    --re-initialize the mins and maxes. No need to compare them against another value
    if !isDrawing then
        --mouseMinX = mousePos.x
        --moouseMinY = mousePos.y
        --mouseMaxX = mousePos.x
        --mouseMaxY = mousePos.y
        --we are drawing so we should continuously update the extrema
        --if mousePos.x < mouseMinX then mouseMinX = mousePos.x
        --elif mousePos.x > mouseMaxX then mouseMaxX = mousePos.x

        --if mousePos.y < mouseMinY then mouseMinY = mousePos.y
        --elif mousePos.y > mouseMaxY then mouseMaxY = mousePos.y    

        --set isDrawing boolean to true
        isDrawing = true

    return mouseMinX, mouseMaxX, mouseMinY, mouseMaxY

function computeDrawingBounds()
    --use the brush size and the mouse extrema to compute the drawing bounds

    local width = mouseMaxX - mouseMinX + size
    local height = mouseMaxY - mouseMinY + size

    return width, height

Any help would be greatly appreciated!

Short answer - you can’t, there’s no option for it in the currently available, documented Lua API.

I’m afraid you’ll need to scan the whole image for changes and draw a bounding box based on the changes you find.

1 Like