c++ - 将输入发送到后台窗口

标签 c++ winapi sdl sendinput directinput

我想将从 Android 客户端接收到的鼠标和键盘输入发送到在 Windows 上运行的游戏。 SendInput 适用于迄今为止我玩过的几乎所有游戏。但要使 SendInput 正常工作,游戏必须是前台窗口。

为了解决这个问题,我使用了 PostMessage(hwnd,...) ,其中 hwnd 是游戏窗口的句柄。但如果游戏使用 DirectInput,则此方法不起作用。通过 Hook GetDeviceState 解决了这个问题。现在我开始制作的另一个游戏正在使用 WM_INPUT 或原始输入,我必须创建原始输入才能使其工作。

根据这个MSDN Article

DirectInput is a set of API calls that abstracts input devices on the system. Internally, DirectInput creates a second thread to read WM_INPUT data, and using the DirectInput APIs will add more overhead than simply reading WM_INPUT directly.

directInput 使用 WM_INPUT 工作。

The SendInput function inserts the events in the INPUT structures serially into the keyboard or mouse input stream. These events are not interspersed with other keyboard or mouse input events inserted either by the user (with the keyboard or mouse) or by calls to keybd_event, mouse_event, or other calls to SendInput.

所以SendInput也提供了抽象。

我想要的只是将输入独立发送到应用程序,即使它的窗口没有焦点。这样我就可以同时向多个游戏发送输入。有没有办法使用 SendInput 等更高级别的 API 调用来实现这一目标?这可以用 SendInput 来完成吗?有没有相应的 C/C++ 库?

最佳答案

使用RAWPUTDEVICE结构注册输入设备时, 设置 dwFlags = RIDEV_EXINPUTSINK 以便在进程处于后台时接收输入。

示例:

RAWINPUTDEVICE rid;

rid.usUsagePage = 1;
rid.usUsage     = 4;    // Joystick
rid.dwFlags     = RIDEV_EXINPUTSINK;
rid.hwndTarget  = window;

if (!RegisterRawInputDevices(&rid, 1, sizeof(RAWINPUTDEVICE)))
    return -1;

关于c++ - 将输入发送到后台窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35408495/

相关文章:

c++ - 双 'button' SDL_Event 成员访问?

c++ - 如何处理带有操纵杆按钮的设备?

c++ - 通过套接字发送西里尔文消息的问题

c++ - 这是动态绑定(bind)还是静态绑定(bind)?

C++ 定时器不工作?

delphi - CopyFile docx 对 doc 进行隐藏转换

c++ - SDL2窗口不会立即关闭吗?

c++ - 如何在 C/C++ 中使用 lame 将 mp3 解码为 wav?

c++ - RegOpenKeyEx 返回 ERROR_NOACCESS

c++ - 限制程序对 unc 服务器的访问