2017-07-06 122 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

相關問題