【发布时间】:2019-06-25 12:06:14
【问题描述】:
我编写了一个代码,它将根据按下的时间(毫秒)按下一个键。我正在使用question 上的答案来发送关键事件。
这是代码:
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;
}
现在,如何模拟键盘按下的时间?
我需要使用 Timers.Timer 吗?
使用keybd_event 的按键向下标记并按住按键,在计时器结束后释放按键?
【问题讨论】:
-
这不是您要更改的按键事件参数的一部分吗?
-
ToUnixTimeMilliseconds或ctime - startTime有什么意义?如果您想测量时间,请使用Stopwatch,它适用于纳秒刻度。DateTimealso stores ticks internally 这意味着ToUnixTimeMilliseconds()最多失去精度。简单地从另一个中减去一个 DateTime 将返回一个精度更高的Timespan,而无需支付ToUnixTimeMilliseconds背后的操作成本 -
至于问题本身,你想做什么?没有尝试按键的代码。 所有 代码看起来像是在尝试创建一个计时器。您可能可以用计时器替换 all 这段代码,也许是 System.Threading.Timer
标签: c#