This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
ExDomino's profile picture

Get window object on mouse event

Started by ExDomino, 01 August 2015 - 02:36 PM
ExDomino #1
Posted 01 August 2015 - 04:36 PM
Hello, I'm wondering if there is a way to know, when an mouse_up or mouse_down event occurs, on what window the mouse was. In order to easy create some widgets like buttons and so on. Thank you in advance!
MKlegoman357 #2
Posted 01 August 2015 - 05:09 PM
You simply check in what area the click occurred. If a button was placed in "x=5, y=4, width=7, height=1" then the area of the button would be: startx=5; endx=5+7-1=11; starty=4; endy=4;.

Here's some sample code:


local btnX, btnY = 4, 3
local btnText = "Click me"

term.setCursorPos(btnX, btnY)
term.setBackgroundColor(colors.gray)
term.setTextColor(colors.lightGray)

term.write(btnText )

while true do
  local event, b, x, y = os.pullEvent("mouse_click")

  if x >= btnX and x <= btnX + #btnText - 1 and y == btnY then
    term.setCursorPos(1, 1)
    term.write("Clicked at " .. os.clock())
  end
end