-->

Keydown event - cool down

2019-07-23 09:11发布

问题:

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?

回答1:

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.



回答2:

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.



回答3:

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


标签: c# keydown