How to detect single mouse clicks

From MiniScript Wiki
Revision as of 15:13, 20 November 2021 by SebNozzi (talk | contribs) (Added explanation text.)
Jump to navigation Jump to search

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