Keydown 事件 - 冷却
我有一个带有 keydown 事件的项目,但是每次按键时,我都会单击该键,如果我停止单击它,它将等待半秒并快速启动垃圾邮件该键。我需要它在没有冷却的情况下发送垃圾邮件,我该怎么办?
I have a project with keydown event, but as every keypress, i click on the key and if i kip clicking it, it will wait a half second and start spam quickly the key. I need it to spam with no cool down, what can i do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这称为键盘重复延迟,它是一个系统范围的属性,可以在控制面板的键盘部分中设置。或者,您可以使用
SystemParametersInfo
Win32 API 函数,设置SPI_SETKEYBOARDDELAY
标志。要从 C# 调用它,您可能需要定义 P/Invoke 签名,但幸运的是 PInvoke.net 上有人有 已经为我们做到了这一点。
不要忘记您正在设置系统范围的设置!这可能需要管理员权限,无论如何,您应该好好发挥并在完成后将其恢复到原始设置。
This is called the Keyboard Repeat Delay, and it's a system-wide property that can be set in the Keyboard section in the Control Panel. Alternately, you can set it via code, using the
SystemParametersInfo
Win32 API function, setting theSPI_SETKEYBOARDDELAY
flag.To call it from C#, you probably need to define a P/Invoke signature, but luckily someone on PInvoke.net has done this for us already.
Don't forget that you are setting a system-wide setting! This might require admin privileges, and in any case, you should play nice and return it to the original setting once you're done.
您可以监视同一键的 keydown 和 keyup 事件(不要忘记用户可以一次按下多个键并以不同的顺序释放它们),而不是更改系统范围的设置并仍然有 250ms 的延迟。在 keydown 时以所需的频率启动计时器,并在 keyup 时停止计时器,并将之前的 keydown 处理程序设置为计时器处理程序。
Instead of changing the system-wide settings and still have a delay of 250ms, you can watch keydown and keyup events for the same key (don't forget that a user can press multiple keys at once and release them in different order). Start a timer with required frequency on keydown, and stop it on the keyup, and set your previous keydown handler as a timer handler.
尝试使用响应式扩展并使用与时间相关的运算符之一(例如 Sample 或 Interval)来实现此处所需的功能。
http://msdn.microsoft.com/en-us/data/gg577609
作为示例(仅作为指南,无需 VS 键入)
Try using Reactive Extensions and use one of the time-related operator such as Sample or Interval to achieve what you need here.
http://msdn.microsoft.com/en-us/data/gg577609
As an example (just as a guide, typed without VS)