如何使用 SendKeys 发送 NumPad 键?

How do you send NumPad keys using SendKeys?

我要发送 NumPad 键 (1-9) 的击键。

我尝试使用:

SendKeys.SendWait("{NUMPAD1}");

但是它说

System.ArgumentException: The keyword NUMPAD1 is invalid (translated)

所以我不知道 NumPad 的正确键码。

您应该能够像传递字母一样传递数字。例如:

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

出于好奇,我查看了 source code 的 SendKeys。没有任何内容可以解释为什么数字键盘代码被排除在外。我不建议将此作为首选选项,但可以使用反射将缺少的代码添加到 class:

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}")。然而,它似乎不适用于发送替代代码,所以也许这就是他们将它们排除在外的原因。