繁体   English   中英

如何使用SendKeys发送NumPad密钥?

[英]How do you send NumPad keys using SendKeys?

我想发送NumPad键的按键(1-9)。

我试着用:

SendKeys.SendWait("{NUMPAD1}");

但它说

System.ArgumentException:关键字NUMPAD1无效(已翻译)

所以我不知道NumPad的正确密钥代码。

出于好奇,我查看了SendKeys的源代码 没有什么可以解释为什么小键盘代码被排除在外。 我不建议将此作为首选选项,但可以使用反射将缺少的代码添加到类中:

FieldInfo info = typeof(SendKeys).GetField("keywords",
    BindingFlags.Static | BindingFlags.NonPublic);
Array oldKeys = (Array)info.GetValue(null);
Type elementType = oldKeys.GetType().GetElementType();
Array newKeys = Array.CreateInstance(elementType, oldKeys.Length + 10);
Array.Copy(oldKeys, newKeys, oldKeys.Length);
for (int i = 0; i < 10; i++) {
    var newItem = Activator.CreateInstance(elementType, "NUM" + i, (int)Keys.NumPad0 + i);
    newKeys.SetValue(newItem, oldKeys.Length + i);
}
info.SetValue(null, newKeys);

现在我可以用eg。 SendKeys.Send("{NUM3}") 但它似乎不适用于发送alt代码,所以也许这就是为什么他们把它们排除在外。

您应该能够以与传递信件相同的方式传递号码。 例如:

SendKeys.SendWait("{A}");  //sends the letter 'A'
SendKeys.SendWait("{5}");  //sends the number '5'

暂无
暂无

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

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