I have written a code will press a key depending on what time(mSec) they're pressed. I'm using the answer on this question to send key events.
This is the code:
public void DebugReplayKeys()
{
long startTime = 0;
Thread td = new Thread(() =>
{
int currentIndex = 0;
bool flag = true;
while (flag)
{
long ctime = DateTimeOffset.Now.ToUnixTimeMilliseconds();
long runtime = ctime - startTime;
var rpk = recordedPressedKeys[currentIndex];
if (runtime >= rpk.eventTime)
{
//create timer for simulate key hold time??
//then release key if timer is elapsed??
Console.WriteLine(rpk.key); //gonna be replaced by (keybd_event)
currentIndex++;
}
if(currentIndex > recordedPressedKeys.Count -1)
{
flag = false;
}
}
});
td.SetApartmentState(ApartmentState.STA);
startTime = DateTimeOffset.Now.ToUnixTimeMilliseconds();
td.Start();
}
This is the object used to store the key events:
class RecordedPressedKeys
{
public long eventTime;
public Keys key;
public long holdTime;
}
Now, How can I simulate how long the keyboard is pressed?
Do I need to use Timers.Timer?
Press and hold the key using keybd_event
's key down flag the release the key after the timer has elapsed?