2017-07-06 130 views
3

我想发送NumPad键(1-9)的击键。如何使用SendKeys发送NumPad密钥?

我试着使用:

SendKeys.SendWait("{NUMPAD1}"); 

但它说

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

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

+0

阅读[this](https://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.aspx),它看起来不可能... – Mischa

回答

1

出于好奇,我看了看source code进行的SendKeys。没有任何解释为什么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); 

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

0

你应该可以像传递一封信一样传递一个数字。例如:

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

我知道,那是基本的东西。我想要NumPad Keys,而不是普通的数字。 –

+0

当然是有差别的,在几乎所有游戏控制配置中,您都可以使用数字键盘进行控制,而数字键盘键具有自己的名称(如NUM_4),并在按下时显示在设置中。这对键盘上的数字没有影响。 –

+1

虽然它们似乎映射到相同的char代码。 ConsoleKeyInfo c1 = Console.ReadKey(true); Console.WriteLine(c1.Key); Console.WriteLine(c1.KeyChar); Console.WriteLine((int)c1.KeyChar); ConsoleKeyInfo c2 = Console.ReadKey(true); Console.WriteLine(c2.Key); Console.WriteLine(c2.KeyChar); Console.WriteLine((int)c2.KeyChar); 如果这是任何帮助,它看起来可能通过Windows API http://www.vbforums.com/showthread.php?347527-Using-SendKeys-to-Send-Number-Pad-Numbers – iliketocode

相关问题