簡體   English   中英

發送帶有攔截的按鍵

[英]Sending Key Presses with Interception

我已經嘗試了所有偽造鍵盤操作的常規方法(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;
    }

我注釋掉的代碼是我試過的,但沒有用。

您需要調整 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。 箭頭鍵可能是網站上提到的特殊鍵。

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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM