简体   繁体   English

C ++随机使用keybd_event次数

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

I want that my program executes the keybd_event a random number of times per Loop 我希望我的程序在每个循环中随机执行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);

for example I want when I press F that it presses the Up,down,left,right arrow keys randomly between 1 - 10 times. 例如,当我按F时,它希望在1至10次之间随机按上,下,左,右箭头键。 I want a rand() % 10 func in front of the keybd_event. 我想在keybd_event前面放一个rand()%10 func。 How do I do that? 我怎么做?

Thanks. 谢谢。

Your question is not quite clear for me. 您的问题对我来说不太清楚。 As far as I understood you want to press 'F' key and randomly choose a key among [LEFT, TOP, RIGHT, DOWN] and emulate it 1 to 10 times? 据我了解,您想按“ F”键并在[LEFT,TOP,RIGHT,DOWN]中随机选择一个键并将其模拟1到10次? If this is the case then the code should look something like this: 如果是这种情况,那么代码应如下所示:

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