簡體   English   中英

按下特定鍵時快速按下此鍵。 如何防止觸發鍵被按下? (c++)

[英]Quickly press this key when a specific key is pressed. How prevent the trigger key to be pressed? (c++)

如果標題不清楚,我很抱歉.. buuut 我不知道如何縮短內容:c 如果按下/發送鍵“U”,下面的(提取)代碼應該快速按下/發送鍵“A” . 但是如果我按'U',它會先發送'U',然后是幾個'A'。 我怎樣才能使它根本不發送密鑰'U'? 我嘗試了不同的東西,但我無法讓它工作......我認為問題出在 if 條件中:/

INPUT ip; // Structure for creating keyboard-events
ip.type = INPUT_KEYBOARD; // Create a generic keyboard-event
ip.ki.wScan = 0;       // Hardware-Scan-Code for the key
ip.ki.time = 0;        //
ip.ki.dwExtraInfo = 0; //

for (;;) {
    sleep_for(milliseconds(1)); // Sleep in each iteration so the CPU / RAM have a relaxed life
    if (GetAsyncKeyState(0x55)) {   // If key 'U' is pressed
        ip.ki.wVk = 0x41;                 // Press key 'A'
        ip.ki.dwFlags = 0;                //
        SendInput(1, &ip, sizeof(INPUT)); //

        ip.ki.dwFlags = KEYEVENTF_KEYUP;    // Release key that was pressed
        SendInput(1, &ip, sizeof(INPUT));   //
    }
}

您在循環中調用 GetAsyncKeyState() 。 如果檢測到按鍵是否被按下。 在按下該鍵期間,它多次調用 function。 當您按下一個鍵時,通常會持續 5-25 毫秒,因此 GetASyncKeyState() 會多次檢測按鍵。

GetASyncKeyState() 返回一個短的 integer,如果設置了最低有效位,則在之前對 GetASyncKeyState() 的調用中已經檢測到密鑰

要修復您的代碼,只需使用按位運算符 AND 檢查此位

if (GetAsyncKeyState(0x55) &1)

這在切換布爾值或特征時也很有用。

暫無
暫無

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

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