I’m currently trying to simulate holding a key down for a specified time. The code runs in its own thread so it’s not interrupting the UI. But the Sleep function is still interrupting the hold sequence. I actually wanted to use Sleep for the time, the key has to be hold down. Is there any way around?
int delay = 2000;
keybd_event(VK_W, 0, KEYEVENTF_EXTENDEDKEY, 0);
System.Threading.Thread.Sleep(delay);
keybd_event(VK_W, 0, KEYEVENTF_KEYUP, 0);
System.Threading.Thread.Sleep(50);
This should simulate holding down the key W, but all it does is pressing it, sleeping (it doesn’t do anything), and then releasing it.
If you want to repeatedly send key-down strokes for a specified number of milliseconds, this could work, but you don’t know how many times it’s really being pressed – you just know that it’s being pressed for the duration of 2000 milliseconds at some unknown discrete rate.
And you could call it like so: