Help needed with mouse events.

Hi All,

I’ve recently gone back to working on a minesweeper game that I wrote a while back as a learning process.

I originally had the squares setup with touch listeners to detect when one was clicked and also if the CTRL key was held down while clicking to clear/mark cells in the grid but I’m currently trying to implement proper left click/right click  instead to make it feel more natural.

The problem I’m having however is that the mouse events don’t have the usual began/ended phases so when I right click to place a flag it is doing it straight away rather then when I release the button.

Does anybody know if there is a way to detect a mouse click with either button rather than just a touch?

Surely a mouse click event, using any button, should be treated just like a touch/tap event with all of the same phases.

There’s quite a bit undocumented with the mouse event. You probably want  event.type. There you have: up, down, drag and move.

You can combine these to test if event.type is down and primary or secondary mouse button is pressed.

local function onMouseEvent( event ) print( "-------" ) print( "type:", event.type ) print( "isPrimaryButtonDown:", event.isPrimaryButtonDown ) print( "isSecondaryButtonDown:", event.isSecondaryButtonDown ) end Runtime:addEventListener( "mouse", onMouseEvent )

You should, however, add some checks there to ensure that the code is only run once per click, i.e. that the user has to let go before anything happens again.

Oooh lovely.   So I’ll effectively have to write my own function to detect a click/release then.

I’ll have a play about with that tonight after work.  

Just out of interest, how do people find out about these undocumented things?

By doing some investigative work. This is again something that should be added to the docs, and since the docs are open source, anyone could do it (but I’m personally too busy right now to do it this week :P).

You can find stuff out by checking everything that is actually passed into the event via:

local function onMouseEvent( event ) print( "-------------" ) for i, j in pairs( event ) do print( i, j ) end end Runtime:addEventListener( "mouse", onMouseEvent ) --[[Which outputs a whole lot: 17:54:45.220 ------------- 17:54:45.220 isSecondaryButtonDown false 17:54:45.220 isMiddleButtonDown false 17:54:45.220 isCtrlDown false 17:54:45.220 type move 17:54:45.220 isPrimaryButtonDown false 17:54:45.220 scrollY 0 17:54:45.220 isCommandDown false 17:54:45.220 y 179.19999694824 17:54:45.220 x 576.90002441406 17:54:45.220 name mouse 17:54:45.220 isShiftDown false 17:54:45.220 isAltDown false 17:54:45.220 time 58546.2 17:54:45.220 scrollX 0 17:54:45.220 clickCount 0]]

There’s quite a bit undocumented with the mouse event. You probably want  event.type. There you have: up, down, drag and move.

You can combine these to test if event.type is down and primary or secondary mouse button is pressed.

local function onMouseEvent( event ) print( "-------" ) print( "type:", event.type ) print( "isPrimaryButtonDown:", event.isPrimaryButtonDown ) print( "isSecondaryButtonDown:", event.isSecondaryButtonDown ) end Runtime:addEventListener( "mouse", onMouseEvent )

You should, however, add some checks there to ensure that the code is only run once per click, i.e. that the user has to let go before anything happens again.

Oooh lovely.   So I’ll effectively have to write my own function to detect a click/release then.

I’ll have a play about with that tonight after work.  

Just out of interest, how do people find out about these undocumented things?

By doing some investigative work. This is again something that should be added to the docs, and since the docs are open source, anyone could do it (but I’m personally too busy right now to do it this week :P).

You can find stuff out by checking everything that is actually passed into the event via:

local function onMouseEvent( event ) print( "-------------" ) for i, j in pairs( event ) do print( i, j ) end end Runtime:addEventListener( "mouse", onMouseEvent ) --[[Which outputs a whole lot: 17:54:45.220 ------------- 17:54:45.220 isSecondaryButtonDown false 17:54:45.220 isMiddleButtonDown false 17:54:45.220 isCtrlDown false 17:54:45.220 type move 17:54:45.220 isPrimaryButtonDown false 17:54:45.220 scrollY 0 17:54:45.220 isCommandDown false 17:54:45.220 y 179.19999694824 17:54:45.220 x 576.90002441406 17:54:45.220 name mouse 17:54:45.220 isShiftDown false 17:54:45.220 isAltDown false 17:54:45.220 time 58546.2 17:54:45.220 scrollX 0 17:54:45.220 clickCount 0]]