如何发送键Control + A + B? (保持控制修饰符为“按下”) [英] How to send keys Control + A + B? (keep Control modifier "pressed")

查看:298
本文介绍了如何发送键Control + A + B? (保持控制修饰符为“按下”)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我记录此序列时,它失败了。我知道我可以使用Keyboard.SendKeys(control, A,ModifierKeys.Control)发送Control + A,但是如何在按下下一个字母之前发送一个包含控件并释放字母的序列。

When I record this sequence it fails. I know I can send Control + A using Keyboard.SendKeys(control, "A",ModifierKeys.Control) but how do I send a sequence that holds control and releases the letter before pressing the next letter.

注意:我要查找的序列类似于默认的Visual Studio快捷方式,用于注释一行Control + K + C

Note: the sequence I am looking for is similar to the default Visual Studio shortcut for commenting out a line Control + K + C

是也许这只是我需要使用WinApi的东西?

Is this maybe something that I just need to use the WinApi for?

推荐答案

keybd_event 是对此非常方便(比替换 SendInput 更容易使用)。

keybd_event(Keys.Control, MapVirtualKey(Keys.Control, 0), 0, 0);
keybd_event(Keys.A, MapVirtualKey(Keys.A, 0), 0, 0);
keybd_event(Keys.A, MapVirtualKey(Keys.A, 0), KEYEVENTF_KEYUP, 0);
keybd_event(Keys.B, MapVirtualKey(Keys.B, 0), 0, 0);
keybd_event(Keys.B, MapVirtualKey(Keys.B, 0), KEYEVENTF_KEYUP, 0);
keybd_event(Keys.Control, MapVirtualKey(Keys.Control, 0), KEYEVENTF_KEYUP, 0);

如果您只需要按住控制键,alt键和/或移位键,请检查 TCS对 SendKeys.Send 的回答。 keybd_event 功能更强大,可以让您按住任意键并按任意顺序释放。

If you only ever need to hold down control, alt, and/or shift, check TCS's answer of SendKeys.Send. keybd_event is more powerful and will let you hold down any key, and release in any order.

这篇关于如何发送键Control + A + B? (保持控制修饰符为“按下”)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆