我如何在Pygame中制作热键

发布于 2025-02-13 10:29:48 字数 374 浏览 0 评论 0原文

我正在使用Pygame进行游戏,我想添加一个热键以关闭游戏。现在我正在使用:

keys_pressed = pygame.key.get_pressed()

# Hotkey for exiting game. CTRL + SHIFT + Q
if keys_pressed[pygame.K_LCTRL] and keys_pressed[pygame.K_LSHIFT] and keys_pressed[pygame.K_q]:
    pygame.quit()
    sys.exit()

这可以关闭游戏,但是如果我按其他键(ex/ ctrl + shift + l + q),它也会关闭游戏。有什么方法可以解决此问题,并且只有按下所需的键,而仅此而已。

I'm making a game with pygame and I want to add a hotkey to close the game. Right now I am using:

keys_pressed = pygame.key.get_pressed()

# Hotkey for exiting game. CTRL + SHIFT + Q
if keys_pressed[pygame.K_LCTRL] and keys_pressed[pygame.K_LSHIFT] and keys_pressed[pygame.K_q]:
    pygame.quit()
    sys.exit()

This works in closing the game but if I press other keys with it (Ex/ CTRL + SHIFT + L + Q), it also closes the game. Is there a way I can fix this and have it only work if my desired keys are pressed and nothing else.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

゛时过境迁 2025-02-20 10:29:48

如果您要处理keydownkeyup 事件,事件对象具有一个bit Five属性,称为mod,它将告诉您哪个其他修饰符键被按下。

这是一个最小示例:

import pygame

screen = pygame.display.set_mode((480, 320))
pygame.init()

run = True
clock = pygame.time.Clock()
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        elif event.type == pygame.KEYDOWN:
            print(f"Key {pygame.key.name(event.key)} pressed")
            if event.key == pygame.K_q:
                if event.mod & pygame.KMOD_CTRL and event.mod & pygame.KMOD_SHIFT:      
                    print("Hotkey Exit!")
                    run = False       
        elif event.type == pygame.KEYUP:
            print(f"Key {pygame.key.name(event.key)} released")

    screen.fill(pygame.Color("grey"))
    pygame.display.update()
    clock.tick(60)  # limit to 60 FPS

pygame.quit()

运行代码时,然后按 ctrl + shift + q q >在控制台上您会看到以下内容:

$ python3.9 -i pyg_simple_hotkey.py
pygame 2.1.2 (SDL 2.0.18, Python 3.9.13)
Hello from the pygame community. https://www.pygame.org/contribute.html
Key left ctrl pressed
Key left shift pressed
Key q pressed
Hotkey Exit!
>>>

If you're handling KEYDOWN or KEYUP events, the event object has a bit field attribute called mod that will tell you which other modifier keys are pressed.

Here's a minimal example:

import pygame

screen = pygame.display.set_mode((480, 320))
pygame.init()

run = True
clock = pygame.time.Clock()
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        elif event.type == pygame.KEYDOWN:
            print(f"Key {pygame.key.name(event.key)} pressed")
            if event.key == pygame.K_q:
                if event.mod & pygame.KMOD_CTRL and event.mod & pygame.KMOD_SHIFT:      
                    print("Hotkey Exit!")
                    run = False       
        elif event.type == pygame.KEYUP:
            print(f"Key {pygame.key.name(event.key)} released")

    screen.fill(pygame.Color("grey"))
    pygame.display.update()
    clock.tick(60)  # limit to 60 FPS

pygame.quit()

When running the code and then pressing Ctrl+Shift+Q you'll see the following on the console:

$ python3.9 -i pyg_simple_hotkey.py
pygame 2.1.2 (SDL 2.0.18, Python 3.9.13)
Hello from the pygame community. https://www.pygame.org/contribute.html
Key left ctrl pressed
Key left shift pressed
Key q pressed
Hotkey Exit!
>>>
月亮坠入山谷 2025-02-20 10:29:48

只需计算按下的键总数,并将其作为条件在您的中包括在语句中:

import pygame
import sys
import time

pygame.init()

display = pygame.display.set_mode((300, 300))
clock = pygame.time.Clock()

while True:
    clock.tick(100)
    pygame.event.pump()
    keys_pressed = pygame.key.get_pressed()
    num_keys_pressed = len([x for x in keys_pressed if x])

    if (
        num_keys_pressed == 3
        and keys_pressed[pygame.K_LCTRL]
        and keys_pressed[pygame.K_LSHIFT]
        and keys_pressed[pygame.K_q]
    ):
        break

pygame.quit()

Just count the total number of keys pressed and include that as a condition in your if statement:

import pygame
import sys
import time

pygame.init()

display = pygame.display.set_mode((300, 300))
clock = pygame.time.Clock()

while True:
    clock.tick(100)
    pygame.event.pump()
    keys_pressed = pygame.key.get_pressed()
    num_keys_pressed = len([x for x in keys_pressed if x])

    if (
        num_keys_pressed == 3
        and keys_pressed[pygame.K_LCTRL]
        and keys_pressed[pygame.K_LSHIFT]
        and keys_pressed[pygame.K_q]
    ):
        break

pygame.quit()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文