Keydown event - cool down

房东的猫 提交于 2019-12-13 02:15:56

问题


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


来源:https://stackoverflow.com/questions/9248143/keydown-event-cool-down

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!