python鼠标事件函数举例
时间: 2023-10-13 22:24:08 浏览: 153
以下是几个常用的 Python 鼠标事件函数示例:
1. 鼠标左键单击事件:
```python
import pygame
def handle_events():
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1: # 鼠标左键单击事件
print("Left button clicked at", event.pos)
```
2. 鼠标移动事件:
```python
import pygame
def handle_events():
for event in pygame.event.get():
if event.type == pygame.MOUSEMOTION: # 鼠标移动事件
print("Mouse moved to", event.pos)
```
3. 鼠标滚轮事件:
```python
import pygame
def handle_events():
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 4: # 鼠标滚轮向上滚动事件
print("Mouse wheel up")
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 5: # 鼠标滚轮向下滚动事件
print("Mouse wheel down")
```
注意:以上示例中需要使用 Pygame 模块来处理鼠标事件。
阅读全文