我希望发送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);
SendKeys.Send("{NUM3}")
。然而,似乎无法发送alt代码,所以可能这就是为什么他们省略了它们的原因。您应该能够像传递字母一样传递数字。例如:
SendKeys.SendWait("{A}"); //sends the letter 'A'
SendKeys.SendWait("{5}"); //sends the number '5'