简体   繁体   English

C ++ WM_KEYDOWN不同的间隔

[英]C++ WM_KEYDOWN different intervals

I am currently trying to get KeyBoard Input from the WM_KEYDOWN and WM_CHAR message for my own InputBox. 我目前正在尝试从WM_KEYDOWNWM_CHAR消息中获取自己的InputBox的键盘输入。

This is the code that I am using for basic input, which works fine for characters: 这是我用于基本输入的代码,适用于字符:

if(msg.message == WM_KEYDOWN)
{
    keyHandled = false;
    //handle other keys here, e.g. VK_LEFT
}
else if(msg.message == WM_CHAR && !keyHandled)
{
    keyHandled = true;
    gui->UpdateInput(msg.wParam);
}

If the key that is being pressed is also a key that triggers the WM_CHAR message, the interval is as in usual input boxes. 如果所按下的键也是触发WM_CHAR消息的键,则间隔与通常的输入框中相同。
However, if it is a key like VK_LEFT , it keeps receiving the WM_KEYDOWN message without any delay. 但是,如果它是VK_LEFT之类的密钥 ,它将继续接收WM_KEYDOWN消息而没有任何延迟。

Is there any way that I can receive all keys with the same interval or do I have to implement a timer that handles the delay between the messages? 有什么办法可以以相同的间隔接收所有密钥,还是必须实现一个处理消息之间延迟的计时器? I have also had a look at the WM_KEYDOWN message on msdn, but I could not find anything related to the intervals. 我也查看了msdn上的WM_KEYDOWN消息 ,但是找不到与间隔有关的任何内容。

Windows has its own delays for sending Keyboard events from Keyboard input and this isn't something you can simply change. Windows有自己的延迟,无法从键盘输入发送键盘事件,这不是您可以简单更改的。 As you know, holding down a key will result in a first message, a delay, and then a series of more messages with a quicker interval. 如您所知,按住一个键将导致第一个消息,一个延迟,然后以更快的间隔产生更多的消息。 You can get around this by requesting states rather than relying on the messages directly. 您可以通过请求状态来解决此问题,而不是直接依赖消息。 This is called Unbuffered input and is state oriented. 这称为无缓冲输入,并且是面向状态的。 To store your states, you can do the following: 要存储状态,可以执行以下操作:

bool keys[256];

When you are checking windows events, you can update the key states accordingly like this: 在检查Windows事件时,可以像下面这样相应地更新键状态:

//In your WinProc function most likely
if (msg.message == WM_KEYDOWN)
{
    keys[msg.wParam] = true;
}

if (msg.message == WM_KEYUP)
{
    keys[msg.wParam] = false;
}

Then whenever you'd like, you can request the state of a specific key through the following function: 然后,您可以根据需要通过以下函数请求特定键的状态:

bool getKeyPressed(char keycode)
{
    return keys[keycode];
}

For example: 例如:

//This could be in some random update function and called
//whenever you need the information.
if (getKeyPressed(VK_UP))
{
//Do something here...
}

The above function can be used wherever you'd like, therefore the frequency at which you update is completely up to you at that point. 上面的功能可以在任何地方使用,因此更新的频率完全取决于您。 As mentioned before, this is Unbuffered input and is State oriented, whereas Buffered Input is Event oriented. 如前所述,这是未缓冲的输入,并且是面向状态的,而缓冲输入的是事件是面向的。

I think that this internal delay can not be modified easily. 我认为这种内部延迟不能轻易修改。 One approach could be writing your own general key handler that keeps track of the states of all keys. 一种方法是编写自己的通用密钥处理程序,以跟踪所有密钥的状态。 For example, a list containg the keycodes of all pressed keys. 例如,一个列表包含所有按下的键的键控代码。 On WM_KEYDOWN you add the keycode to the list and on WM_KEYUP you remove it. WM_KEYDOWN上 ,将密钥代码添加到列表中,在WM_KEYUP上 ,将其删除。 Then create something like a timer that simply notifies you in the desired delay times and calls your key handling function for each element of the list. 然后创建类似计时器的内容,该计时器仅在所需的延迟时间内通知您,并为列表的每个元素调用密钥处理函数。

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

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