6

我正在尝试向应用程序发送 WM_INPUT 消息,但遇到了一些我无法解决的障碍。我创建了如下所示的 RAWINPUT 结构:

//try sending 'W'
    RAWINPUT raw = {0};
    char c = 'W';
    //header
    raw.header.dwSize = sizeof(raw);
    raw.header.dwType = RIM_TYPEKEYBOARD;
    raw.header.wParam = 0; //(wParam & 0xff =0 => 0)
    raw.header.hDevice = hDevice;

    //data
    raw.data.keyboard.Reserved = 0;
    raw.data.keyboard.Flags = RI_KEY_MAKE;      //Key down
    raw.data.keyboard.MakeCode = static_cast<WORD>(MapVirtualKeyEx(c, MAPVK_VK_TO_VSC, GetKeyboardLayout(0)));
    raw.data.keyboard.Message = WM_KEYDOWN;
    raw.data.keyboard.VKey = VkKeyScanEx(c, GetKeyboardLayout(0));
    raw.data.keyboard.ExtraInformation = 0;         //???

    //Send the message
    SendMessage(hPSWnd, WM_INPUT, 0, (LPARAM)raw/*Raw input handle*/);      //TODO: Handle to raw input

我被困在的地方至少有两个位置:

  1. 是否需要将一些特殊的东西传递给raw.data.keyboard.ExtraInformation,或者是GetMessageExtraInfo(),还是不需要在这里传递任何东西?

  2. WM_INPUT 消息的 LPARAM 参数包含指向 RAWINPUT 结构的句柄,而不是地址或结构本身……如何创建这样的句柄?

我不想使用 SendInput,因为它要求窗口是活动窗口。我已经这样做了,它运行良好,但是当我激活另一个窗口时 - 当然 - 它停止在前一个窗口上工作。

所以我想要实现的是,将输入发送到不需要成为活动的应用程序。

4

1 回答 1

6

All of the raw input documentation is geared toward how to handle raw messages sent to your application by the system. There's little indication it will work properly if your application sends such messages to another application. The receiving application must register to receive WM_INPUT messages, and most applications don't.

You probably want to use the Microsoft UI Automation instead.

But if you want to experiment with WM_INPUT...

The LPARAM-parameter of the WM_INPUT-message contains a handle to a RAWINPUT-structure not an address or the structure itself... How to create such a handle?

This is a very old API that expects you to use handles from one of the handle-based memory managers.

HGLOBAL hRaw = ::GlobalAlloc(GHND, sizeof(RAWINPUT));
RAWINPUT *pRaw = reinterpret_cast<RAWINPUT*>(::GlobalLock(hRaw));
// initialize the structure using pRaw
::GlobalUnlock(hRaw);
// use hRaw as the LPARAM
于 2012-09-25T20:26:26.130 回答