1

我已经尝试了所有伪造键盘操作的常规方法(SendInput/SendKeys/etc),但它们似乎都不适用于使用 DirectInput 的游戏。经过大量阅读和搜索后,我偶然发现了Interception,这是一个 C++ 库,可让您连接到您的设备。

自从我使用 C++(C# 不存在)以来已经有很长时间了,所以我遇到了一些麻烦。我已经粘贴了下面的示例代码。

看起来无论如何都会使用此代码从代码中启动关键操作?这些示例都只是连接到设备并重写操作(x 键打印 y、反转鼠标轴等)。

enum ScanCode
{
    SCANCODE_X   = 0x2D,
    SCANCODE_Y   = 0x15,
    SCANCODE_ESC = 0x01
};

int main()
{
    InterceptionContext context;
    InterceptionDevice device;
    InterceptionKeyStroke stroke;

    raise_process_priority();

    context = interception_create_context();

    interception_set_filter(context, interception_is_keyboard, INTERCEPTION_FILTER_KEY_DOWN | INTERCEPTION_FILTER_KEY_UP);

    /*
    for (int i = 0; i < 10; i++)
    {
        Sleep(1000);
        stroke.code = SCANCODE_Y;
        interception_send(context, device, (const InterceptionStroke *)&stroke, 1);
    }
    */

    while(interception_receive(context, device = interception_wait(context), (InterceptionStroke *)&stroke, 1) > 0)
    {
        if(stroke.code == SCANCODE_X) stroke.code = SCANCODE_Y;

        interception_send(context, device, (const InterceptionStroke *)&stroke, 1);

        if(stroke.code == SCANCODE_ESC) break;
    }

我注释掉的代码是我试过的,但没有用。

4

1 回答 1

2

您需要调整 UP 和 DOWN 状态的键状态以获得按键。注意在while循环中,interception_wait返回了变量设备,你注释掉的代码会将事件发送到什么?设备未初始化!忘记您的代码并尝试一些更基本的代码。查看带有interception_send 调用的循环内的行,在它之后再进行两次调用,但不要忘记在每次调用之前使用INTERCEPTION_KEY_DOWN 和INTERCEPTION_KEY_UP 更改stroke.state,以便您伪造down 和up 事件。您将在每个键盘事件中获得额外的按键。

此外,您可以尝试使用 INTERCEPTION_FILTER_KEY_ALL 而不是 INTERCEPTION_FILTER_KEY_DOWN | INTERCEPTION_FILTER_KEY_UP。箭头键可能是网站上提到的特殊键。

于 2012-01-12T03:15:56.343 回答