Difference between revisions of "How to detect single mouse clicks"

From MiniScript Wiki
Jump to navigation Jump to search
(Added explanation text.)
(Added a "see also")
Line 16: Line 16:
 
yield
 
yield
 
end while</ms>
 
end while</ms>
 +
 +
== See also ==
 +
 +
* [[Mouse]]
  
 
[[Category:How To]]
 
[[Category:How To]]
 
[[Category:Mini Micro]]
 
[[Category:Mini Micro]]
 
[[Category:Soda]]
 
[[Category:Soda]]

Revision as of 15:14, 20 November 2021

When trying to detect single mouse clicks, using just the mouse.button function alone won't work.

This function returns 1 (a true value) as long as the mouse button is being clicked / held down.

What you want instead is to register the first instant the mouse went down (was "clicked") and then ignore "mouse down" events until the mouse button is released. Then you are ready to react to a next mouse down / held / released cycle.

For that, you can use a loop similar to this:

wasDown = false
while true  // press Control-C to exit
	isDown = mouse.button
	if isDown and not wasDown then
		print "Mouse button clicked!"
	end if
	wasDown = isDown
	yield	
end while

See also