Search code examples
c#visual-studioperformancemouseeventkeyboard-events

C# keybd_event and mouse_event uses too much CPU


I try to programm a macro recorder on my own. I have problems finding out why my Programm takes up so much CPU. When recording the programm it only takes about 0.2% cpu usage. On playback it gets up to around 25% usage.

To simulate the keyboard input I call this code that opens a new task.

   var kTask = new Task(() => new KeyboardTask(keys_stop[index], keys[index]));
   kTask.Start();

The Task calls this Class:

class KeyboardTask
{

    [DllImport("user32.dll")] static extern short VkKeyScan(char ch);
    // Import the user32.dll
    [DllImport("user32.dll", SetLastError = true)]
    static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo);

    // Declare some keyboard keys as constants with its respective code
    // See Virtual Code Keys: https://msdn.microsoft.com/en-us/library/dd375731(v=vs.85).aspx
    public const int KEYEVENTF_EXTENDEDKEY = 0x0001; //Key down flag
    public const int KEYEVENTF_KEYUP = 0x0002; //Key up flag
    public const int VK_RCONTROL = 0xA3; //Right Control key code
    public const int VK_LCONTROL = 0xA2; //Left Control key code
    public const int VK_LSHIFT = 0xA0;
    public const int VK_RSHIFT = 0xA1;

    byte key;
    long cooldown;

    long cooldown_time = 500;
    long delay;
    long delay_now;
    bool first_press = false;

    DateTime lastCooldown;
    DateTime start;
    DateTime stop;
    TimeSpan ts;


    public KeyboardTask(long delay, Key run_key)
    {
        key = (byte)KeyInterop.VirtualKeyFromKey(run_key);
        lastCooldown = DateTime.Now;

        this.delay = delay;


        runOtherKeys();




        if (run_key == Key.LeftShift || run_key == Key.RightShift)
        {
            runShift();
        }
        else if (run_key == Key.LeftCtrl)
        {
            runCtrl();
        }



    }


    private void runShift()
    {
        start = DateTime.Now;

        while (delay >= delay_now)
        {
            stop = DateTime.Now;
            ts = (stop - start);
            delay_now = (long)ts.TotalMilliseconds;
            if (!first_press)
            {
                keybd_event(key, 0, KEYEVENTF_EXTENDEDKEY, 0);
                first_press = true;
            }
        }
        keybd_event(VK_LSHIFT, 0, KEYEVENTF_KEYUP, 0);
        keybd_event(VK_RSHIFT, 0x45, KEYEVENTF_KEYUP, 0);

    }

    private void runCtrl()
    {
        start = DateTime.Now;

        while (delay >= delay_now)
        {
            stop = DateTime.Now;
            ts = (stop - start);
            delay_now = (long)ts.TotalMilliseconds;
            if (!first_press)
            {
                keybd_event(VK_LCONTROL, 0x9d, 0, 0);
                first_press = true;
            }

        }
        keybd_event(VK_LCONTROL, 0x9d, KEYEVENTF_KEYUP, 0);



    }

    private void runOtherKeys()
    {
        start = DateTime.Now;

        while (delay >= delay_now)
        {
            stop = DateTime.Now;
            ts = (stop - start);
            delay_now = (long)ts.TotalMilliseconds;


            if (first_press)
            {
                cooldown = getCooldown();
                if (cooldown >= cooldown_time)
                {
                    lastCooldown = DateTime.Now;
                    cooldown_time = 33;


                    keybd_event(key, 0x45, 0, 0);
                    keybd_event(key, 0x45, KEYEVENTF_KEYUP, 0);


                }
            }
            else
            {
                lastCooldown = DateTime.Now;
                keybd_event(key, 0x45, 0, 0);
                keybd_event(key, 0x45, KEYEVENTF_KEYUP, 0);
                first_press = true;
            }



        }

        keybd_event(key, 0x45, KEYEVENTF_KEYUP, 0);

    }
    private long getCooldown()
    {
        DateTime cooldown_stop = DateTime.Now;
        TimeSpan cooldown_timespan = (cooldown_stop - lastCooldown);
        return (long)cooldown_timespan.TotalMilliseconds;

    }

}

And to simulate mouse input i have thise code here:

public void Start()
{
     Task.Run(() => runRecordMouse());      
} 


private void runRecordMouse()
    {

        DateTime start = DateTime.Now;
        DateTime stop;
        TimeSpan ts;
        long time;
        int count = 0;
        int max = x_arr.Length;
        long delay = delay_arr_mouse[count];

        while (count < max)
        {
            
         stop = DateTime.Now;
         ts = (stop - start);
         time = (long)ts.TotalMilliseconds;

         if (delay <= time)
          {

                    performMouseEvent(count);
                    count++;
                    if (count != max) delay = delay_arr_mouse[count];
                    start = DateTime.Now;
                }
            
        }

    }


    private void performMouseEvent(int pos)
    {

        mouse_button = mouse_click_arr[pos];
        x = x_arr[pos];
        y = y_arr[pos];

        if (mouse_button_pre != mouse_button && mouse_button_pre == 1)
        {
            mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);
        }
        if (mouse_button_pre != mouse_button && mouse_button_pre == 2) mouse_event(MOUSEEVENTF_RIGHTUP, x, y, 0, 0);

        switch (mouse_button)
        {
            case 0:
                SetCursorPos(x, y); //move
                break;

            case 1:

                if (mouse_button_pre == mouse_button)
                {
                    Drag(x_arr[pos - 1], y_arr[pos - 1], x, y); //left Click drag
                }
                else
                {
                    mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0); //left Click
                }

                break;

            case 2:
                mouse_event(MOUSEEVENTF_RIGHTDOWN, x, y, 0, 0); //right Click
                break;


        }


        mouse_button_pre = mouse_button;

    }

    private static void Drag(int xStart, int yStart, int x, int y)
    {
        
        x = x - xStart;
        y = y - yStart;
        SetCursorPos(xStart, yStart);
        mouse_event(MOUSEEVENTF_MOVE, x, y, 0, 0);
    }

Performance Profiler

It's written in C# .NET Framework.

MY PC: CPU: AMD Ryzen 5 3600 RAM: 32 GB GPU: AMD Radeon RX 5700


Solution

  • I just added

    Thread.Sleep(1);
    

    to all while loops and now the programm only takes about 0.5% cpu usage.