C# 如何使用SendKeys发送NumPad密钥?

C# 如何使用SendKeys发送NumPad密钥?,c#,winforms,sendkeys,keystroke,numpad,C#,Winforms,Sendkeys,Keystroke,Numpad,我想发送NumPad键(1-9)的击键。 我尝试使用: SendKeys.SendWait("{NUMPAD1}"); 但是它说 System.ArgumentException:关键字NUMPAD1无效(已翻译) 因此,我不知道NumPad的正确键码。您应该能够以与传递字母相同的方式传递数字。例如: SendKeys.SendWait("{A}"); //sends the letter 'A' SendKeys.SendWait("{5}"); //sends the number '

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

我尝试使用:

SendKeys.SendWait("{NUMPAD1}");
但是它说

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


因此,我不知道NumPad的正确键码。

您应该能够以与传递字母相同的方式传递数字。例如:

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

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

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);
FieldInfo=typeof(SendKeys).GetField(“关键字”,
BindingFlags.Static | BindingFlags.NonPublic);
数组oldKeys=(数组)info.GetValue(null);
Type elementType=oldKeys.GetType().GetElementType();
Array newKeys=Array.CreateInstance(elementType,oldKeys.Length+10);
Array.Copy(oldKeys、newKeys、oldKeys.Length);
对于(int i=0;i<10;i++){
var newItem=Activator.CreateInstance(elementType,“NUM”+i,(int)Keys.NumPad0+i);
设置值(newItem,oldKeys.Length+i);
}
info.SetValue(null,newkey);

现在我可以使用例如
SendKeys.Send(“{NUM3}”)
。但是,发送alt代码似乎不起作用,所以可能这就是他们忽略它们的原因。

阅读时,看起来不可能……我知道,这是基本的东西。我想要的是NumPad键,而不是普通的数字。当然有一个区别,在几乎所有的游戏控件配置中,您都可以使用NumPad进行控制,并且NumPad键有自己的名称(如NUM_4),按下时显示在设置中。这对键盘顶部的数字没有影响,但它们似乎映射到了相同的字符代码。ConsoleKeyInfo c1=Console.ReadKey(true);控制台写入线(c1键);控制台写入线(c1.KeyChar);Console.WriteLine((int)c1.KeyChar);ConsoleKeyInfo c2=Console.ReadKey(true);控制台写入线(c2键);控制台写入线(c2.KeyChar);Console.WriteLine((int)c2.KeyChar);如果这有什么帮助的话,通过windows API它看起来确实是可能的。这对我有很大帮助。你如何使用反射添加到源代码?@Bedir如果你是说添加/替换现有的.NET方法,而该方法不能被继承覆盖,我不确定。这个问题的答案可能会对你有所帮助。