-
pygame.mouse
- pygame module to work with the mouse
— get the state of the mouse buttons — get the mouse cursor position — get the amount of mouse movement — set the mouse cursor position — hide or show the mouse cursor — get the current visibility state of the mouse cursor — check if the display is receiving mouse input — set the mouse cursor to a new cursor — get the current mouse cursor 鼠标功能可用于获取鼠标设备的当前状态。这些功能还可以更改鼠标的系统光标。
设置显示模式后,事件队列将开始接收鼠标事件。鼠标按键生成
pygame.MOUSEBUTTONDOWN
和pygame.MOUSEBUTTONUP
事件,当它们被按下并释放时。这些事件包含一个按钮属性,表示按下了哪个按钮。鼠标滚轮将生成pygame.MOUSEBUTTONDOWN
和pygame.MOUSEBUTTONUP
滚动时的事件。当滚轮向上滚动时,该按钮将设置为4,当滚轮向下滚动时,该按钮将设置为按钮5。每当移动鼠标时,它都会生成一个pygame.MOUSEMOTION
事件。鼠标移动被分解为小而准确的运动事件。当鼠标移动时,许多运动事件将被放置到队列中。未从事件队列中正确清除的鼠标移动事件是事件队列填满的主要原因。如果鼠标光标被隐藏,并且输入被抓取到当前显示器,则鼠标将进入虚拟输入模式,在该模式下,鼠标的相对移动将永远不会被屏幕的边界所阻止。请参阅函数
pygame.mouse.set_visible()
和pygame.event.set_grab()
来进行配置。PYGAME 2中的鼠标轮行为
对于鼠标滚轮行为,有适当的功能,并支持pyGame 2
pygame.MOUSEWHEEL
事件。新事件支持水平和垂直滚动移动,有符号整数值表示滚动量 (x
和y
),以及flipped
方向(翻转为每个轴设置的正值和负值)。在此处阅读有关SDL2输入相关更改的更多信息 https://wiki.libsdl.org/MigrationGuide#Input在pyGame 2中,鼠标滚轮功能可以通过监听
pygame.MOUSEWHEEL
事件类型(请记住,它们仍会发出pygame.MOUSEBUTTONDOWN
在pyGame 1.x中也有类似的事件)。触发此事件时,开发人员可以访问相应的Event
具有以下内容的对象pygame.event.get()
。该对象可用于访问有关鼠标滚动的数据,例如which
(它将告诉您触发事件的具体鼠标设备)。# Taken from husano896's PR thread (slightly modified) import pygame from pygame.locals import * pygame.init() screen = pygame.display.set_mode((640, 480)) clock = pygame.time.Clock() def main(): while True: for event in pygame.event.get(): if event.type == QUIT: pygame.quit() return elif event.type == MOUSEWHEEL: print(event) print(event.x, event.y) print(event.flipped) print(event.which) # can access properties with # proper notation(ex: event.y) clock.tick(60) # Execute game: main()
- pygame.mouse.get_pressed()¶
- get the state of the mouse buttonsget_pressed(num_buttons=3) -> (button1, button2, button3)get_pressed(num_buttons=5) -> (button1, button2, button3, button4, button5)
返回表示所有鼠标按键状态的布尔值序列。True值表示调用时鼠标当前正被按下。
请注意,要获取所有鼠标事件,最好使用
pygame.event.wait()
或pygame.event.get()
并检查所有这些事件,看看它们是否MOUSEBUTTONDOWN
,MOUSEBUTTONUP
,或MOUSEMOTION
。请注意,打开
X11
一些X服务器使用中键模拟。当您同时单击两个按钮时1
和3
同时,一个2
可以发出按钮事件。注意,记得打电话给
pygame.event.get()
在此函数之前。否则,它将不会像预期的那样工作。支持五键鼠标,可选参数
num_buttons
已添加到pyGame 2中。当此选项设置为5
,button4
和button5
添加到返回的元组中。仅限3
和5
是此参数的有效值。Changed in pygame 2.0.0:
num_buttons
添加了参数
- pygame.mouse.get_pos()¶
- get the mouse cursor positionget_pos() -> (x, y)
返回
x
和y
鼠标光标的位置。该位置相对于显示屏的左上角。光标位置可以位于显示窗口之外,但始终限制在屏幕上。
- pygame.mouse.get_rel()¶
- get the amount of mouse movementget_rel() -> (x, y)
中的移动量
x
和y
自上次调用此函数以来。鼠标光标的相对移动被限制在屏幕的边缘,但请参阅虚拟输入鼠标模式以了解解决此问题的方法。页面顶部描述了虚拟输入模式。
- pygame.mouse.set_pos()¶
- set the mouse cursor positionset_pos([x, y]) -> None
将当前鼠标位置设置为给定的参数。如果鼠标光标可见,它将跳到新的坐标。移动鼠标将生成一个新的
pygame.MOUSEMOTION
eve NT。
- pygame.mouse.set_visible()¶
- hide or show the mouse cursorset_visible(bool) -> bool
如果bool参数为真,则鼠标光标将可见。这将返回光标以前的可见状态。
- pygame.mouse.get_visible()¶
- get the current visibility state of the mouse cursorget_visible() -> bool
获取鼠标光标的当前可见性状态。
True
如果鼠标可见,False
不然的话。New in pygame 2.0.0.
- pygame.mouse.get_focused()¶
- check if the display is receiving mouse inputget_focused() -> bool
当pyGame正在接收鼠标输入事件(或者,在窗口术语中,是“活动的”或具有“焦点”)时,返回TRUE。
此方法在窗口中工作时最有用。相比之下,在全屏模式下,此方法始终返回TRUE。
注:在
MS
窗口中,具有鼠标焦点的窗口也具有键盘焦点。但在X-Windows下,一个窗口可以接收鼠标事件,另一个窗口可以接收键盘事件。pygame.mouse.get_focused()
指示pyGame窗口是否接收鼠标事件。
- pygame.mouse.set_cursor()¶
- set the mouse cursor to a new cursorset_cursor(pygame.cursors.Cursor) -> Noneset_cursor(size, hotspot, xormasks, andmasks) -> Noneset_cursor(hotspot, surface) -> Noneset_cursor(constant) -> None
将鼠标光标设置为新的内容。此函数接受显式
Cursor
对象或参数来创建Cursor
对象。看见
pygame.cursors.Cursor
pygame object representing a cursor 有关创建游标的帮助和示例。Changed in pygame 2.0.1.
- pygame.mouse.get_cursor()¶
- get the current mouse cursorget_cursor() -> pygame.cursors.Cursor
获取有关鼠标系统光标的信息。返回值包含与传入的参数相同的数据
pygame.mouse.set_cursor()
set the mouse cursor to a new cursor 。备注
将GET_CURSOR()调用解包为
size, hotspot, xormasks, andmasks
仍然有效,假设调用返回的是旧式类型的游标。Changed in pygame 2.0.1.
Edit on GitHub