4

I want to send the keystroke of NumPad keys (1-9).

I tried to use:

SendKeys.SendWait("{NUMPAD1}");

but it says

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

So i don't know the right keycode for the NumPad.

Regster Up
  • 113
  • 1
  • 2
  • 7
  • Reading [this](https://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.aspx), it looks like it is not possible... – Mischa Jul 06 '17 at 15:18

2 Answers2

4

Out of curiosity I looked at the source code for SendKeys. There's nothing explaining why the numpad codes were excluded. I wouldn't recommend this as a preferred option, but it's possible to add the missing codes to the class using reflection:

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);

Now I can use eg. SendKeys.Send("{NUM3}"). It doesn't seem to work for sending alt codes however, so maybe that's why they left them out.

Bloopy
  • 305
  • 1
  • 9
  • How do you add to the source code using reflection? – Bedir Nov 26 '19 at 21:01
  • @Bedir If you mean adding to/replacing an existing .NET method that can't be overridden with inheritance, I'm not sure. The answers to [this question](https://stackoverflow.com/questions/7299097/dynamically-replace-the-contents-of-a-c-sharp-method) may help you. – Bloopy Nov 28 '19 at 00:19
  • Can confirm, for using alt codes, this does unfortunately not work. – Philipp Sep 05 '22 at 12:16
0

You should be able to pass in a number in the same manner as passing in a letter. For example:

SendKeys.SendWait("{A}");  //sends the letter 'A'
SendKeys.SendWait("{5}");  //sends the number '5'
John Smith
  • 7,243
  • 6
  • 49
  • 61
  • I know, thats basic stuff. I wanted the NumPad Keys, not the normal numbers. – Regster Up Jul 10 '17 at 20:55
  • of course there is a diffrence, in nearly every game control configs you can use the numpad for control, and the numpad keyy have their own names (like NUM_4), displayed in the settings when pressed. This has no effect on the numbers on top of the keyboard. – Regster Up Jul 10 '17 at 21:46
  • 1
    They do appear to map to the same char code though. 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); if this is any help, it does look possible via the windows API http://www.vbforums.com/showthread.php?347527-Using-SendKeys-to-Send-Number-Pad-Numbers – John Smith Jul 10 '17 at 21:52
  • Great help, this helps me alot. – Regster Up Jul 11 '17 at 13:18