2015-12-15 226 views

回答

7
if event.type == pygame.MOUSEBUTTONDOWN: 
    print event.button 

event.button可以等於幾個整數值:

1 - 左鍵點擊

2 - 中點擊

3 - 點擊右鍵

4 - 滾動

5 - 向下滾動


相反的事件,就可以得到當前按鈕狀態,以及:

pygame.mouse.get_pressed() 

這會返回一個元組:

(leftclick,middleclick,rightclick)

每一個都是一個表示按鈕向上/向下的布爾整數。

4

你可能想仔細看看這個tutorial,以及在n.st的回答this SO question

所以,告訴您如何在右邊和左擊區分的代碼是這樣的:

#!/usr/bin/env python 
import pygame 

LEFT = 1 
RIGHT = 3 

running = 1 
screen = pygame.display.set_mode((320, 200)) 

while running: 
    event = pygame.event.poll() 
    if event.type == pygame.QUIT: 
     running = 0 
    elif event.type == pygame.MOUSEBUTTONDOWN and event.button == LEFT: 
     print "You pressed the left mouse button at (%d, %d)" % event.pos 
    elif event.type == pygame.MOUSEBUTTONUP and event.button == LEFT: 
     print "You released the left mouse button at (%d, %d)" % event.pos 
    elif event.type == pygame.MOUSEBUTTONDOWN and event.button == RIGHT: 
     print "You pressed the right mouse button at (%d, %d)" % event.pos 
    elif event.type == pygame.MOUSEBUTTONUP and event.button == RIGHT: 
     print "You released the right mouse button at (%d, %d)" % event.pos 

    screen.fill((0, 0, 0)) 
    pygame.display.flip() 
相關問題