使用拦截发送按键

Sending Key Presses with Interception

本文关键字:      更新时间:2023-10-16

我尝试了所有伪造键盘动作的正常方法(SendInput/SendKeys/等),但似乎没有一种适用于使用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;
    }

我注释掉的代码是我尝试过的,但没有成功。

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

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

void ThreadMethod()
{
    while (true)
    {
        if (turn)
        {
            for (int i = 0; i < 10; i++)
            {
                Sleep(1000);
                InterceptionKeyStroke stroke;
                stroke.code = SCANCODE_Y;
                stroke.state = 0;
                interception_send(context, device, (const InterceptionStroke *)&stroke, 1);
                Sleep(1);
                stroke.state = 1;
                interception_send(context, device, (const InterceptionStroke *)&stroke, 1);
                turn = false;
            }
        }
        else Sleep(1);
    }
}
            
CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)ThreadMethod, NULL, NULL, NULL);
            
while (interception_receive(context, device = interception_wait(context), (InterceptionStroke*)&stroke, 1) > 0)
{
    if (stroke.code == SCANCODE_F5) turn = true;
            
    interception_send(context, device, (InterceptionStroke*)&stroke, 1);
            
    if (stroke.code == SCANCODE_ESC) break;
}
相关文章:
  • 没有找到相关文章