我正在编写一个小的Pygame脚本,我需要知道鼠标是否已离开Pygame窗口
我不知道怎么解释它。看起来很简单,但我无法在任何地方找到解决方案。
答案 0 :(得分:4)
pygame.mouse.focus()
给出0
#!/usr/bin/env python3
import pygame
pygame.init()
screen = pygame.display.set_mode((800,600))
is_running = True
while is_running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
is_running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
is_running = False
print(pygame.mouse.get_focused())
pygame.quit()
答案 1 :(得分:1)
我不明白为什么没有人告诉您使用ACTIVEEVENT。
在较旧的pygame版本中我不知道,但是在Windows 10,python 3.7.3,pygame 1.9.6中,我可以使用此功能:
import { IPanelMenuPropsController, PanelMenuControllerState } from "../Interface";
import PanelMenuView from "../View";
interface IPanel {
mapGL: any,
blocked: boolean,
selectedTab: string,
savedMap: boolean,
nameMap: string,
error: boolean,
}
const PanelView: IPanel = PanelMenuView;
class PanelMenu extends Component<IPanelMenuPropsController, PanelMenuControllerState> {
render() {
const { mapGL, loadable, selectedTab, savedMap, nameMap } = this.props;
const disableAllOptions = !loadable ? true : false;
return (
<PanelView
mapGL={mapGL}
blocked={disableAllOptions}
selectedTab={selectedTab}
savedMap={savedMap}
nameMap={nameMap}
error={false}
/>
)
}
}
function selectStateApp(state: any) {
return {
selectedTab: state.app.selectedTab,
savedMap: state.map.savedMap,
nameMap: state.map.nameMap,
};
}
export default connect(
selectStateApp,
dispatch => ({
setNameMap: (name: string) => MapActions.setNameMap(name)(dispatch),
setTab: (tab: string) => AppActions.setTab(tab)(dispatch),
})
)(PanelMenu);
我已经测试过了。可行!
答案 2 :(得分:0)
我做了一些测试......
if not bool(game.mouse.get_focused()):
print("Mouse has left (Method 1)")
和...
elif event.type == game.MOUSEMOTION:
checkFocus(event, self.canvas)
def checkFocus(e, display):
x, y = e.pos
MX, MY = display.get_size()
MX -= 1 # 0 - based
MY -= 1
if x <= 0 or y <= 0 or x >= MX or y >= MY:
print("Mouse has left (Method 2)")
和方法1一直在工作,而方法2只在大部分时间工作(特别是在最大X和Ys [MX&amp; MY]附近)
这是一个真实的记录,让我像疯子一样摆动我的鼠标......
Mouse has left (Method 1)
Mouse has left (Method 2)
Mouse has left (Method 1) <--
Mouse has left (Method 1) <--
Mouse has left (Method 2)
Mouse has left (Method 1)
正如您所看到的,在这个简短的示例中,方法1比方法2更有效。
感谢所有帮助过的人!