-1

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?

Roberto Pegoraro
  • 1,313
  • 2
  • 16
  • 31
conquistador
  • 673
  • 3
  • 11
  • 35
  • Wouldnt that be part of the parameter of the keypress event you're going to change to? – BugFinder Jun 25 '19 at 12:08
  • 2
    What's the point of `ToUnixTimeMilliseconds` or `ctime - startTime` ? If you want to measure time use `Stopwatch` which works with nanosecond ticks. `DateTime` [also stores ticks internally](https://referencesource.microsoft.com/#mscorlib/system/datetime.cs,132) which means `ToUnixTimeMilliseconds()` loses precision at best. Simply subtracting one DateTime from another would return a `Timespan` with far greater precision, without paying the cost for the operations behind `ToUnixTimeMilliseconds` – Panagiotis Kanavos Jun 25 '19 at 12:21
  • 2
    As for the question itself, what are you trying to do? There's no code that tries to press keys. *All* of this code looks like an attempt to create a timer. You could probably replace *all* this code with a timer, perhaps a [System.Threading.Timer](https://learn.microsoft.com/en-us/dotnet/api/system.threading.timer?view=netframework-4.8) – Panagiotis Kanavos Jun 25 '19 at 12:22

1 Answers1

0

This is what I'm trying to do. I just need to simulate how long the key is pressed. I used a System.Timers.Timer. Send the press event the send the release event after the timer has elapsed.

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)
                    {
                        var tmr = new System.Timers.Timer(Convert.ToDouble(rpk.eventTime));
                        tmr.AutoReset = false;
                        tmr.Elapsed += delegate
                        {
                            ReleaseKey(rpk.key);
                        };
                        PressKey(rpk.key);
                        tmr.Enabled = true;
                        currentIndex++;
                    }
                    if(currentIndex > recordedPressedKeys.Count -1)
                    {
                        flag = false;
                    }
                }
            });
            td.SetApartmentState(ApartmentState.STA);
            startTime = DateTimeOffset.Now.ToUnixTimeMilliseconds();
            td.Start();
        }

private void PressKey(Keys key)
        {
            keybd_event((byte)key, 0, KEYEVENTF_EXTENDEDKEY, 0);
        }

private void ReleaseKey(Keys key)
        {
            keybd_event((byte)key, 0, KEYEVENTF_KEYUP, 0);
        }

[DllImport("user32.dll", SetLastError = true)]
static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

conquistador
  • 673
  • 3
  • 11
  • 35