原始输入是替代键盘挂钩吗?

发布于 2024-10-12 20:12:58 字数 462 浏览 4 评论 0原文

简单的问题——

我正在阅读有关键盘挂钩的内容,有人建议使用原始输入来执行此操作,但我还没有找到任何示例。 例如,我在

RAWINPUTDEVICE rid[1];
rid[0].usUsagePage = 0x01;
rid[0].usUsage = 0x06;
rid[0].hwndTarget = hWnd;
rid[0].dwFlags = 0;
RegisterRawInputDevices(rid, 1, sizeof(rid[0]));

应用程序自己的窗口中使用 And catchsign WM_INPUT 很好,但不在应用程序之外。这可以在应用程序之外实现吗?还是必须使用 WH_KEYBOARD 或 WH_KEYBOARD_LL? MSDN 没有明确说明原始输入是否可以在全球范围内进行。

编辑:我了解 Hooks,但我想知道你是否也可以使用原始输入来做到这一点!

干杯

Quick question --

I was reading about keyboard hooks and one suggested using Raw Input to do this, yet I havn't found any example of it.
For example I am using

RAWINPUTDEVICE rid[1];
rid[0].usUsagePage = 0x01;
rid[0].usUsage = 0x06;
rid[0].hwndTarget = hWnd;
rid[0].dwFlags = 0;
RegisterRawInputDevices(rid, 1, sizeof(rid[0]));

And catchign WM_INPUT fine in the applications own window, but not outside the application. Is this possible outside the application or do you have to use WH_KEYBOARD or WH_KEYBOARD_LL? MSDN didn't make it clear if Raw Input could be made globally.

EDIT: I know about Hooks but I want to know if you can do it with Raw input too!

Cheers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

倚栏听风 2024-10-19 20:12:58

查看该内容的 MSDN 文档,有一个名为 < code>RIDEV_INPUTSINK 描述为:“如果设置,即使调用者不在前台,也可以使调用者接收输入。”

我自己并没有搞乱这一点,但听起来它对于从应用程序窗口之外获取输入可能很有用。

Looking at the MSDN documentation for that stuff, there is a flag called RIDEV_INPUTSINK which is described as: "If set, this enables the caller to receive the input even when the caller is not in the foreground."

I haven't messed with that myself, but it sounds like it could be useful for getting input from beyond the application's window.

弄潮 2024-10-19 20:12:58

这就是我初始化 RAW INPUT 以全局拦截鼠标和键盘事件的方式。与钩子相比,最大的优点是不需要 DLL。
您可以使用 WM_INPUT 处理窗口过程中的原始输入事件。有关详细信息:原始输入< /a>

#include <Windows.h>

const USHORT HID_MOUSE    = 2;
const USHORT HID_KEYBOARD = 6;

bool HID_RegisterDevice(HWND hTarget, USHORT usage)
{
    RAWINPUTDEVICE hid;
    hid.usUsagePage = 1;
    hid.usUsage = usage;
    hid.dwFlags = RIDEV_DEVNOTIFY | RIDEV_INPUTSINK;
    hid.hwndTarget = hTarget;

    return !!RegisterRawInputDevices(&hid, 1, sizeof(RAWINPUTDEVICE));
}

void HID_UnregisterDevice(USHORT usage)
{
    RAWINPUTDEVICE hid;
    hid.usUsagePage = 1;
    hid.usUsage = usage;
    hid.dwFlags = RIDEV_REMOVE;
    hid.hwndTarget = NULL;

    RegisterRawInputDevices(&hid, 1, sizeof(RAWINPUTDEVICE));
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR cmd_line, int cmd_show)
{
    WNDCLASS wc;
    ...
    RegisterClass(&wc);

    HWND hwnd = CreateWindow(...);
    ...

    HID_RegisterDevice(hwnd, HID_KEYBOARD);
    HID_RegisterDevice(hwnd, HID_MOUSE);

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

    HID_UnregisterDevice(HID_MOUSE);
    HID_UnregisterDevice(HID_KEYBOARD);

    return (int) msg.wParam;
}

This is how I initialize RAW INPUT to globally intercept mouse and keyboard events. The great advantage compared to hooks is you don't need a DLL.
You treats the raw input events in the window procedure with WM_INPUT. For further informations : RAW INPUT

#include <Windows.h>

const USHORT HID_MOUSE    = 2;
const USHORT HID_KEYBOARD = 6;

bool HID_RegisterDevice(HWND hTarget, USHORT usage)
{
    RAWINPUTDEVICE hid;
    hid.usUsagePage = 1;
    hid.usUsage = usage;
    hid.dwFlags = RIDEV_DEVNOTIFY | RIDEV_INPUTSINK;
    hid.hwndTarget = hTarget;

    return !!RegisterRawInputDevices(&hid, 1, sizeof(RAWINPUTDEVICE));
}

void HID_UnregisterDevice(USHORT usage)
{
    RAWINPUTDEVICE hid;
    hid.usUsagePage = 1;
    hid.usUsage = usage;
    hid.dwFlags = RIDEV_REMOVE;
    hid.hwndTarget = NULL;

    RegisterRawInputDevices(&hid, 1, sizeof(RAWINPUTDEVICE));
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR cmd_line, int cmd_show)
{
    WNDCLASS wc;
    ...
    RegisterClass(&wc);

    HWND hwnd = CreateWindow(...);
    ...

    HID_RegisterDevice(hwnd, HID_KEYBOARD);
    HID_RegisterDevice(hwnd, HID_MOUSE);

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

    HID_UnregisterDevice(HID_MOUSE);
    HID_UnregisterDevice(HID_KEYBOARD);

    return (int) msg.wParam;
}
心如狂蝶 2024-10-19 20:12:58

Windows 挂钩是一种可以在事件到达应用程序之前拦截事件的机制。过滤器函数(接收事件的函数)根据事件类型进行分类。如果想要附加到 Windows 挂钩,则必须使用 SetWindowsHookEx 安装过滤器功能。我不得不提一下,全局钩子必须位于单独的 dll 文件中。您可以在 MSDN

A Windows hook is mechanism that one can use to intercept events before they reach an application. Filter functions (functions that receive events) are classified according to the event type. If one wants to attach to a Windows hook, the filter function has to be installed using SetWindowsHookEx. I have to mention that global hooks must be in a separate dll file. You can read more about hooks in MSDN.

仄言 2024-10-19 20:12:58

不确定原始输入,但对于键盘钩子,一般来说,您需要将其设为 dll 并在系统中注册,以便每个进程都会加载它。备注中的一些详细信息此处

not sure exactly about raw input, but for keyboard hook in general you need to make it a dll and register in system so it will be loaded by every process. some details here in remarks

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文