简体   繁体   English

如何使用C#中的keybd_event按住键

[英]How can I hold down a Key using the keybd_event in C#

I'm currently trying to develop an App that enables the user to send Keystrokes to a specific program. 我目前正在尝试开发一个应用程序,使用户能够将击键发送到特定程序。 I already can send keys and hold down specific keys like that: 我已经可以发送密钥并按住这样的特定密钥:

[DllImport("user32.dll", SetLastError = true)]        
static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

keybd_event(key, 0, 0, 0); //Start holding the key down
keybd_event(key, 0, KEY_UP_EVENT, 0); //Stop holding the key down

Holding Shift down while sending letters capitalizes them already. 发送信件时按住Shift键已经将它们资本化。 But if I'm holding down a letter (and send it to notepad for example), then just writes that letter once. 但是,如果我按住一封信(例如将其发送到记事本),那么就写一次这封信。 If I do the same with my physical keyboard, it starts writing the letter until I let go again. 如果我用我的物理键盘做同样的事情,它会开始写信,直到我再次放手。 Does anyone have a tip or clue how I can manage that? 有没有人有提示或线索如何管理?

I already experimented with the stopwatch, but that wasn't working either. 我已经尝试过秒表,但那也没有用。

Thanks to @RaymondChen , this question has been answered. 感谢@RaymondChen,这个问题已得到解答。

Physical keyboards have a feature called "typematic" which autorepeats characters which are held down. 物理键盘具有称为“打字”的功能,可自动重复按下的字符。 You'll have to emulate that too. 你也必须效仿。

If you want to simulate the repetition of a keystroke, just send multiple KeyDown and KeyUP-Events. 如果要模拟击键的重复,只需发送多个KeyDown和KeyUP-Events。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM