I know I can check if there was a left click
event.type == pygame.MOUSEBUTTONDOWN and event.button == LEFT
but how can I check if they double clicked? Also is there any way to check if the user moved the scroll wheel forward or backwards?
I know I can check if there was a left click
event.type == pygame.MOUSEBUTTONDOWN and event.button == LEFT
but how can I check if they double clicked? Also is there any way to check if the user moved the scroll wheel forward or backwards?
I'd just use the delta time value that
clock.tick
returns to increase a timer. In this example you have 0.5 seconds to double click otherwise the timer is reset.I've never used
pygame
- but:Detecting double clicks: at a guess, instead of processing each click immediately, apply a 50ms delay and see if you get another click event in that time. The user probably won't notice the 50ms delay.
Distinguishing between scrollwheel up/down: see the comments on this documentation page. Apparently there are five buttons defined - left, middle, right, scrollwheel-up and scrollwheel-down. That is, you can capture scrollwheel events the same way you're capturing left clicks - you just need to look for
SCROLL_UP
or similar instead ofLEFT
.Look up the documentation to find out exactly what
SCROLL_UP
is called.A very simple solution is to define a
pygame.time.Clock()
to keep track of the time between two consecutiveMOUSEBUTTONDOWN
event.Before the main loop define:
and in the event loop controller:
where
DOUBLECLICKTIME
is the maximum time allowed (in milliseconds) between two clicks for them being considered a double click. Define it before the mainloop. For example, to allow a maximum delay of half a second between the two clicks:DOUBLECLICKTIME = 500
.In pygame is possible to create as many
pygame.time.Clock()
objects are needed.dbclock
must be used only for this purpose (I mean, no other calls todbclock.tick()
anywhere in the main loop) or it will mess with the tracking of the time between the two clicks.For the sake of completeness, let me add also the answer about the scroll wheel, even if other answers already covered it.
The scroll wheel emits
MOUSEBUTTONDOWN
andMOUSEBUTTONUP
events (it's considered a button). I can be identified by theevent.button
parameter, which is4
when the wheel is rolled up, and5
when the wheel is rolled down.There doesn't appear to be a native double-click event. I'll guess you'd need to check the time between consecutive MOUSEBUTTONDOWN events.
The mouse wheel will generate pygame.MOUSEBUTTONDOWN events when rolled. The button will be set to 4 when the wheel is rolled up, and to button 5 when the wheel is rolled down
Set a timer when the mouse is pressed the first time to place a userevent on the pygame event queue, and set a variable to 1 to indicate a click. When the second click occurs, check the variable and set the timer event object to off. Check if the userevent comes up on the queue as this means the timer has timed out. see this beautiful answer for more information: Move an object every few seconds in Pygame
Here is the code, replace the
double_click()
call with your own function call: