在使用GLFW按下某个键后,是否有办法只处理一个输入事件? [英] Is there a way to process only one input event after a key is pressed using GLFW?

查看:1323
本文介绍了在使用GLFW按下某个键后,是否有办法只处理一个输入事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,当按住所需的键时,输入会多次注册。是否有办法在按下键后仅处理第一个事件,并忽略以下事件,直到键被释放?

Currently, when holding down the desired key, the input registers multiple times. Is there a way to process only the first event after the key is pressed and ignore the following events until the key is released?

我正在使用processInput函数,条件如下:

I'm using a processInput function, with the following condition:

if (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS) {
        currentXPos--;
        if (currentXPos < 0)
            currentXPos = 0;
}

currentXPos 只是受左/右箭头键影响的整数。我还有一个等效的 currentYPos 整数,它受上/下箭头键的影响。每按一次键,我需要递增/递减currentXPos。我尝试添加一个最初设置为true的全局bool并在执行时将其设置为false,如下所示:

currentXPos is just an integer that's affected by the left/right arrow keys. I have an equivalent currentYPos integer also that's affected by the up/down arrow keys. I need to increment/decrement currentXPos once per key-press. I've tried adding a global bool initially set to true and on execution setting it to false, like this:

if (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS) {
        if (canMove) {
            canMove = false;
            currentXPos--;
            if (currentXPos < 0)
                currentXPos = 0;
        }
}

if (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_RELEASE) {
        canMove = true;
}

这适用于单键,但如果我使用右箭头键,(为了递增相同的值),下面的函数在第一次释放后不断返回GLFW_RELEASE,将canMove bool设置为true并最终使其变为多余;

This does work with the single key, but if I implement this functionality with the right arrow key as well, (for incrementing the same value), the below function constantly returns GLFW_RELEASE after the first release, setting the canMove bool to true and ultimately making it redundant;

if (glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_RELEASE) {
            canMove = true;
}

我尝试过使用glfwWaitEvents(),但在帮助时仍处理多个输入超过0.5秒左右(与在搜索栏/文本编辑器中按住键盘上任何字符的效果相同)。

I've tried using glfwWaitEvents() but that still processes multiple inputs when help for longer than 0.5 seconds or so (the same effect as holding down any character on the keyboard in a search bar/text editor).

推荐答案

当您只想处理每一个键时,最好的解决方案是监听键回调事件,而不是在每个帧中查询键状态。关键回调函数是一个可以挂钩到glfw的函数,每个键事件都会调用一次。

When you want to handle every key just once, the best solution is to listen to the key callback event instead of querying the key state in every frame. The key callback is a function is a function that can be hooked into glfw and is called once for every key event.

回调看起来应该是这样的:

The callback should look somehow like this:

void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
    if (key == GLFW_KEY_RIGHT && action == GLFW_PRESS)
    {
        currentXPos--;
        if (currentXPos < 0)
            currentXPos = 0;
    }
}

然后可以在点之后的某处注册此回调窗口已创建:

This callback can then be registered somewhere after the point where the window has been created:

glfwSetKeyCallback(window, key_callback);

更多细节可以在 GLFW输入指南

这篇关于在使用GLFW按下某个键后,是否有办法只处理一个输入事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆