我已经尝试了所有常见的伪造键盘操作的方法(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;
    }我注释掉的代码是我尝试过的东西,但它不起作用。
发布于 2012-01-12 11:15:56
您需要调整向上和向下状态的按键状态才能获得按键。注意while循环,变量device是由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。箭头键可能是网站上提到的特殊键。
https://stackoverflow.com/questions/8821895
复制相似问题