C: 最快的方法来确定是否按下了键盘上的任何键 [英] C: Fastest way to determine, if any key on the keyboard has been pressed

查看:27
本文介绍了C: 最快的方法来确定是否按下了键盘上的任何键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 C 语言中寻找最快的方法来确定是否按下了键盘上的任何键.

I am searching for the fastest possible way in C to determine if any key on the keyboard has been pressed.

我不是在寻找如何确定特定键是否被按下(在这种情况下,GetAsyncKeyState() 会起作用).

I am not looking for how to determine if a specific key is pressed (in that case, GetAsyncKeyState() would work).

另外,它需要在后台工作,所以程序窗口没有焦点(程序将在后台运行).

Also, it needs to work in the background, so without the program window having focus (the program will be running in the background).

该程序将对每个按键做出反应并输出声音.我希望它在每次输入内容时输出声音(例如在 Word 中).这也是它需要在后台运行的原因.我希望它很快,这样我就可以最大限度地减少按键和声音输出之间的延迟.

The program will react on every keypress with a sound output. I want it to output a sound everytime I type something (like in Word and such). That's also why it needs to run in the background. I want it to be fast, so I can minimize the delay between keypress and sound output.

我正在寻找除 Windows Hooks 之外的其他东西.虽然它确实可以在后台获得按键,但我正在寻找更快的东西(尽可能少的延迟).

I am searching for something else than Windows Hooks. While it does work for getting key presses in the background, I am looking for something that is faster (least delay possible).

例如:GetAsyncKeyState() 用于对特定按键做出反应,而程序窗口没有焦点.我正在寻找类似的东西,但能够对任何按键做出反应,而不是特定的按键.

For example: GetAsyncKeyState() works for reacting on specific keypresses, while the program window doesn't have focus. I am looking for something like that, but with the ability to react on any key press, not a specific one.

推荐答案

作为评论,您可以使用 RegisterRawInputDevices 作为这个示例一>.

As comment, you could use RegisterRawInputDevices as this sample.

  1. 创建一个仅消息窗口.
  2. 将 RAWINPUTDEVICE.hwndTarget 设置为步骤 1 中创建的窗口,这样您就无需关注窗口.
  3. 调用 GetRawInputData获取输入数据.

示例(删除了错误检查):

Sample(removed the error checking):

#include <windows.h>
#include <iostream>
using namespace std;
LRESULT CALLBACK WindProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
    if (Msg == WM_INPUT)
    {
        HRAWINPUT hRawInput = (HRAWINPUT)lParam;
        RAWINPUT input = { 0 };
        UINT size = sizeof(input);
        GetRawInputData(hRawInput, RID_INPUT,&input,&size,sizeof(RAWINPUTHEADER));
        
        printf("vkey: %x, flag: %d\n",input.data.keyboard.VKey, input.data.keyboard.Flags);
    }
    return DefWindowProc(hWnd, Msg, wParam, lParam);
}

int main()
{
    WNDCLASSEX wcx = { 0 };
    wcx.cbSize = sizeof(WNDCLASSEX);
    wcx.lpfnWndProc = WindProc;
    wcx.hInstance = GetModuleHandle(NULL);
    wcx.lpszClassName = TEXT("RawInputClass");
    RegisterClassEx(&wcx);
    HWND hWnd = CreateWindowEx(0, TEXT("RawInputClass"), NULL, 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, GetModuleHandle(NULL), NULL);

    RAWINPUTDEVICE rid = { 0 };
    rid.usUsagePage = 0x01;
    rid.usUsage = 0x06; //keyboard
    rid.dwFlags = RIDEV_INPUTSINK;
    rid.hwndTarget = hWnd;

    RegisterRawInputDevices(&rid, 1, sizeof(RAWINPUTDEVICE));

    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return 0;
}

这篇关于C: 最快的方法来确定是否按下了键盘上的任何键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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