C# 按住键一毫秒

C# 按住键一毫秒,c#,C#,我已经写了一个代码,它会根据按下的时间(毫秒)来按键。我用这个答案发送关键事件 代码如下: public void DebugReplayKeys() { long startTime = 0; Thread td = new Thread(() => { int currentIndex = 0; bool flag = true; while (flag) { long ct

我已经写了一个代码,它会根据按下的时间(毫秒)来按键。我用这个答案发送关键事件

代码如下:

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();
}

这是用于存储关键事件的对象:

class RecordedPressedKeys
{
    public long eventTime;
    public Keys key;
    public long holdTime;
}
现在,如何模拟键盘被按下的时间?
我需要使用计时器吗。计时器?

使用
keybd_event
的键向下标记按住键,在计时器结束后释放键

这就是我要做的。我只需要模拟一下按键的按下时间。 我使用了一个
系统.定时器.定时器
。发送按下事件计时器运行后发送释放事件

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);


这不是要更改为的按键事件参数的一部分吗?
tounixtimemissions
ctime-startTime
有什么意义?如果您想测量时间,请使用秒表,它可以使用纳秒刻度
DateTime
这意味着
tounixtimemissions()
最多会失去精度。简单地从另一个日期时间中减去一个日期时间将返回一个更高精度的
Timespan
,而不必支付
tounixtimemilures
后面的操作成本。至于问题本身,您想做什么?没有试图按键的代码。所有这些代码看起来都像是试图创建计时器。您可能会用一个计时器,或者一个