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'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 of LEFT
.
Look up the documentation to find out exactly what SCROLL_UP
is called.
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.
import sys
import pygame as pg
pg.init()
screen = pg.display.set_mode((640, 480))
BLACK = pg.Color('black')
FONT = pg.font.Font(None, 32)
def game():
clock = pg.time.Clock()
timer = 0
dt = 0
running = True
while running:
for event in pg.event.get():
if event.type == pg.QUIT:
running = False
if event.type == pg.MOUSEBUTTONDOWN:
if event.button == 1:
if timer == 0: # First mouse click.
timer = 0.001 # Start the timer.
# Click again before 0.5 seconds to double click.
elif timer < 0.5:
print('double click')
timer = 0
# Increase timer after mouse was pressed the first time.
if timer != 0:
timer += dt
# Reset after 0.5 seconds.
if timer >= 0.5:
print('too late')
timer = 0
screen.fill(BLACK)
txt = FONT.render(str(round(timer, 2)), True, (180, 190, 40))
screen.blit(txt, (40, 40))
pg.display.flip()
# dt == time in seconds since last tick.
# / 1000 to convert milliseconds to seconds.
dt = clock.tick(30) / 1000
if __name__ == '__main__':
game()
pg.quit()
sys.exit()
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:
def run():
global clock, double_click_event, timer
double_click_event = pygame.USEREVENT + 1
timer = 0
while True:
clock.tick(60)
check_events()
frame.update()
screen.blit(frame, (0,0))
pygame.display.flip()
def check_events():
global dispatcher, double_click_event, timer
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
if timer == 0:
pygame.time.set_timer(double_click_event, 500)
timerset = True
else:
if timer == 1:
pygame.time.set_timer(double_click_event, 0)
double_click()
timerset =False
if timerset:
timer = 1
return
else:
timer = 0
return
elif event.type == double_click_event:
# timer timed out
pygame.time.set_timer(double_click_event, 0)
timer = 0
print "evt = dble click"
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
A very simple solution is to define a pygame.time.Clock()
to keep track of the time between two consecutive MOUSEBUTTONDOWN
event.
Before the main loop define:
dbclock = pygame.time.Clock()
and in the event loop controller:
if event.type == pygame.MOUSEBUTTONDOWN:
if dbclock.tick() < DOUBLECLICKTIME:
print("double click detected!")
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 to dbclock.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
and MOUSEBUTTONUP
events (it's considered a button). I can be identified by the event.button
parameter, which is 4
when the wheel is rolled up, and 5
when the wheel is rolled down.