Keydown event - cool down

2019-07-23 08:54发布

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?

标签: c# keydown
3条回答
我命由我不由天
2楼-- · 2019-07-23 09:46

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 the SPI_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.

查看更多
Bombasti
3楼-- · 2019-07-23 09:50

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.

查看更多
孤傲高冷的网名
4楼-- · 2019-07-23 09:54

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)

  Observable.FromEventPattern<KeyPressEventArgs>(this, "KeyPress").Sample(500).....
查看更多
登录 后发表回答