如何在Windows PC上实现键盘自动重复功能? [英] How is keyboard auto-repeat implemented on a Windows PC?
问题描述
我希望能够在Windows上拦截自动重复按键(并对其进行任意处理).我想知道如何实现键盘自动重复功能,以便知道自己有哪些选择.即我可以在以下位置拦截吗?
I want to be able to intercept (and do arbitrary processing on) auto-repeating key presses on Windows. I'd like to know how keyboard auto-repeat is implemented so that I know what options I have. i.e. can I intercept at the:
- 应用程序,
- 设备驱动程序和/或
- 硬件级别
?
更新:看起来(硬件性能差)自动重复,然后被设备驱动程序覆盖(
Update: It looks like auto-repeat is (poorly?) generated at the hardware level and then overridden by device drivers (see here).
推荐答案
要修改或过滤行为,可以使用钩子来拦截键:
To modify or filter behavior, you can intercept keys using a hook:
SetWindowsHookEx
using WH_KEYBOARD
挂钩过程在其中接收其他,重复计数(由于按住键)
The hook procedure receives, among others, the repeat count (due to holding down the key)
请注意,低级键盘挂钩(WH_KEYBOARD_LL
)不接收重复计数.
Note that the low level keyboard hook (WH_KEYBOARD_LL
) does not receive the repeat count.
如果所有窗口都在同一trhead中创建,则可以使用特定于线程的钩子,并避免将钩子过程移至DLL.
If all your windows are created in the same trhead, you can use a thread-specific hook, and avoid moving the hook procedure to a DLL.
我模糊地记得重复计数是由键盘本身产生的,并且LL挂钩会发送重复的击键事件-不过,我可能会误会.在DOS下,拔出并重新插入DIN或PS/2键盘后,在BIOS中或通过BIOS调用设置的键重复率和时间确实恢复为默认值.我不确定为什么您需要确切知道.
I dimly remember that repeat counts are generated by the keyboard itself and the LL hook sends repeated keydown events - I may be mistaken, though. Under DOS, the key repeat rate and time that was set in BIOS or through a BIOS call did return to default values when a DIN or PS/2 keyboard was unplugged and replugged. I am not sure WHY you need to know exactly.
这篇关于如何在Windows PC上实现键盘自动重复功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!