首页 > 解决方案 > pygame中的文本输入

问题描述

我正在尝试在 pygame 中制作文本输入功能,我几乎做到了,但有一个问题。

如果按退格键多少时间它只删除一个字符,它只会在我重新释放并再次按退格键时删除另一个字符。

我可以按一次退格键,它会每半秒连续删除一次字符,直到我松开退格键。

这是我的文本输入功能代码,但功能还没有完全完成。

def text_input(x, y, color, char=None, char_x=None, char_y=None, fill=(0, 0, 0), thing=None):
    """

    :param x: Defines the x axis or the width position where the text input should be drawn.
    :param y: Defines the y axis or the height position where the text input should be drawn.
    :param color: Defines the color of the text input.
    :param char: If the parameter char in not entered it does not do anything otherwise it display's the the parameter
                 char's text on the screen
    :param char_x: If the parameter char is entered char_x Defines the x axis or the width position where the text
                   should be drawn.
    :param char_y: If the parameter char is entered char_y Defines the y axis or the height position where the text
                   should be drawn.
    :param fill: If the parameter fill is entered it fills the display with the tuple of the rgb color pixels which the
                 user entered. But if the parameter fill is not entered it fills the display with the rgb color code
                 black.
    :param thing: If the user wants something to been drawn on the display the fill the parameter thing with the object
                  they want to draw. But if the parameter thing is not entered
    :return: It Display's the text input on the window and the updates it
    """

    characters = ""

    while True:
        for detect in pygame.event.get():
            if detect.type == QUIT:
                exit()

            elif detect.type == KEYDOWN:
                if detect.key == K_BACKSPACE:
                    characters = characters[:-1]
                    print(characters)
                elif detect.key == K_RETURN:
                    global text
                    text = characters
                    return

                else:
                    characters = characters + detect.unicode

                WINDOW.fill((0, 0, 0))
                text = FONT.render(f"{characters}", True, color)
                draw(text, x, y, True)

如果你想知道我的完整代码,这里是:

import pygame
from pygame.locals import *
from win32api import GetSystemMetrics
pygame.init()
WIDTH = GetSystemMetrics(0)
HEIGHT = GetSystemMetrics(1)-64
WIDTH_HEIGHT = (WIDTH, HEIGHT)

WINDOW = pygame.display.set_mode(WIDTH_HEIGHT)
FONT = pygame.font.Font("Password generator font.ttf", 32)
text = ""


def draw(thing, x, y, update=None):
    """

    :param thing: The object what needs to be drawn on the screen
    :param x: Defines the x axis or the width position where the object should be drawn.
    :param y: Defines the y axis or the height position where the object should be drawn.
    :param update: If the parameter update is not passed we don't update the display.
                   But if the parameter update is passed as True we update the display.
    :return: It returns the object on the display at its specific place or it returns
            the object on the display at its specific place and updates and updates the display based on the parameter
            update.
    """

    if update is True:
        WINDOW.blit(thing, (x, y))
        return pygame.display.update()

    else:
        return WINDOW.blit(thing, (x, y))


def text_input(x, y, color, char=None, char_x=None, char_y=None, fill=(0, 0, 0), thing=None):
    """

    :param x: Defines the x axis or the width position where the text input should be drawn.
    :param y: Defines the y axis or the height position where the text input should be drawn.
    :param color: Defines the color of the text input.
    :param char: If the parameter char in not entered it does not do anything otherwise it display's the the parameter
                 char's text on the screen
    :param char_x: If the parameter char is entered char_x Defines the x axis or the width position where the text
                   should be drawn.
    :param char_y: If the parameter char is entered char_y Defines the y axis or the height position where the text
                   should be drawn.
    :param fill: If the parameter fill is entered it fills the display with the tuple of the rgb color pixels which the
                 user entered. But if the parameter fill is not entered it fills the display with the rgb color code
                 black.
    :param thing: If the user wants something to been drawn on the display the fill the parameter thing with the object
                  they want to draw. But if the parameter thing is not entered
    :return: It Display's the text input on the window and the updates it
    """

    characters = ""

    while True:
        for detect in pygame.event.get():
            if detect.type == QUIT:
                exit()

            elif detect.type == KEYDOWN:
                if detect.key == K_BACKSPACE:
                    characters = characters[:-1]
                    print(characters)
                elif detect.key == K_RETURN:
                    global text
                    text = characters
                    return

                else:
                    characters = characters + detect.unicode

                WINDOW.fill((0, 0, 0))
                text = FONT.render(f"{characters}", True, color)
                draw(text, x, y, True)


running = True

while running:
    for event in pygame.event.get():
        if event.type == QUIT:
            exit()

    text_input(0, 0, (255, 255, 255))

标签: pythoninputpygamekeybackspace

解决方案


用于pygame.key.set_repeat()控制按住键的重复方式:

启用键盘重复时,按住的键将生成多个pygame.KEYDOWN事件。[...]

[...] 要禁用键重复调用此函数,不带参数或延迟设置为 0。

只需调用pygame.key.set_repeat()以禁用重复键。例如:

pygame.key.set_repeat(100, 100)

推荐阅读