簡體   English   中英

C ++隨機使用keybd_event次數

[英]C++ use keybd_event a random number of times

我希望我的程序在每個循環中隨機執行keybd_event次

if (KeyDown(0x46))

     {
          srand(time(NULL));

          keybd_event(VK_DOWN,0x28,(rand() % 1) ,rand() % 1);
          Sleep(rand() % 801 + 5);
          keybd_event(VK_DOWN,0x28, KEYEVENTF_KEYUP, 0);
          Sleep(rand() % 101);
          keybd_event(VK_RIGHT, 0x27, (rand() % 1), rand() % 1);
          Sleep(rand() % 801 + 5);
          keybd_event(VK_RIGHT, 0x27, KEYEVENTF_KEYUP, 0);
          Sleep(rand() % 101);
          keybd_event(VK_UP, 0x26, (rand() % 1), rand() % 1);
          Sleep(rand() % 801 + 5);
          keybd_event(VK_UP, 0x26, KEYEVENTF_KEYUP, 0);
          Sleep(rand() % 101);
          keybd_event(VK_LEFT, 0x25, rand() % 1, rand() % 1);
          Sleep(rand() % 801 + 5);
          keybd_event(VK_LEFT, 0x25, KEYEVENTF_KEYUP, 0);
          Sleep(rand() % 101);

例如,當我按F時,它希望在1至10次之間隨機按上,下,左,右箭頭鍵。 我想在keybd_event前面放一個rand()%10 func。 我怎么做?

謝謝。

您的問題對我來說不太清楚。 據我了解,您想按“ F”鍵並在[LEFT,TOP,RIGHT,DOWN]中隨機選擇一個鍵並將其模擬1到10次? 如果是這種情況,那么代碼應如下所示:

srand(time(NULL));

DWORD dwKeys[] = { VK_DOWN, VK_RIGHT, VK_LEFT, VK_TOP };
const DWORD key = rand() % _countof(dwKeys); // Randomly select a key
int times = rand() % 10;
for (int i = 0; i < times; i++)
{
    EmulateKeyPress(key);
}

暫無
暫無

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

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