简体   繁体   English

如何正确使用`sdl2.SDL_GetKeyboardState`?

[英]How can `sdl2.SDL_GetKeyboardState` be used correctly?

I'm attempting to use the python library pysdl2 to construct an emulator. 我正在尝试使用python库pysdl2来构建模拟器。 The library has been working well so far, however I've been having problems receiving keyboard input. 到目前为止,该库运行良好,但是在接收键盘输入时遇到了问题。

What I essentially needed to do is test if certain keys are pressed. 我本质上需要做的是测试是否按下了某些键。 After doing a bit of research, I discovered sdl2.SDL_GetKeyboardState which supposedly is the same SDL function as SDL_GetKeyboardState . 经过一些研究,我发现了sdl2.SDL_GetKeyboardState ,它应该是与SDL_GetKeyboardState相同的SDL函数。 Following the previously linked documentation and this article on the Lazy Foo' Productions website , I constructed the following script: 按照先前链接的文档和Lazy Foo Productions网站上的本文 ,我构建了以下脚本:

import sdl2
sdl2.ext.init()

window = sdl2.ext.Window('Test', size=(640, 480))
window.show()

key_states = sdl2.SDL_GetKeyboardState(None)
running = True

while running:
    for event in sdl2.ext.get_events():
        if event.type == sdl2.SDL_QUIT:
            running = False
            break
    if key_states[sdl2.SDL_SCANCODE_A]:
        print('A key pressed')
    window.refresh()

The above code is suppose to detect if the a key is pressed and if so print a message out. 上面的代码假设用于检测a键是否被按下,如果是,则打印一条消息。 When the above program is run, a window does appear, but when the a key is pressed, 'A key pressed' is printed over four thousand times . 当运行上述程序时,会出现一个窗口,但是当按a键时, 'A key pressed'被打印四千多次 It does not continue to print the message, it only prints it once thousands of times, then stops. 它不会继续打印消息,它只会打印一次数千次,然后停止。

At first, I consider that the problem may have been that the key deduction code (lines 15-16) should be inside of the the event loop (lines 11-14). 首先,我认为问题可能在于密钥推导代码(第15-16行)应该位于事件循环(第11-14行)的内部。 It worked to some degree. 它在某种程度上起作用。 Rather than 'A key pressed' being printed out thousands of times per key press, it was only being printed out twice per key press. 而不是'A key pressed'被打印出成千上万的每个键按压次数,它只是被打印出来两次每按键。

Is there a problem with my code? 我的代码有问题吗? Am I missing something about how to correctly use the sdl2.SDL_GetKeyboardState function? 我是否缺少有关如何正确使用sdl2.SDL_GetKeyboardState函数的内容? How can I properly detect key presses? 如何正确检测按键?

Sounds like it's working the way it's supposed to. 听起来它正在按预期的方式运行。 key_states[sdl2.SDL_SCANCODE_A] will return true whenever a is pressed. 每当按下a时, key_states[sdl2.SDL_SCANCODE_A]将返回true。 And there's not much processing going on in your loop, so it'll loop as fast as your CPU allows, printing out "A key pressed" hundreds or thousands of times per second, until you release the key. 而且循环中没有太多的处理,因此它将以您的CPU允许的最快速度循环,每秒打印数百或数千次“按键”,直到您松开按键为止。

You could check for a different event type, such as SDL_KEYDOWN , which operates more like how you're wanting, or you can keep track of the keypress with a variable, for example: 您可以检查其他事件类型,例如SDL_KEYDOWN ,其操作方式更像您想要的那样,或者您可以使用变量来跟踪按键,例如:

key_down = False
while running:
    for event in sdl2.ext.get_events():
        if event.type == sdl2.SDL_QUIT:
            running = False
            break
    if key_states[sdl2.SDL_SCANCODE_A] and not key_down:
        print('A key pressed')
        key_down = True
    elif not key_states[sdl2.SDL_SCANCODE_A] and key_down:
        print('A key released')
        key_down = False
    window.refresh()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM