How do you send NumPad keys using SendKeys?

I want to send a NumPad key combination (1-9).

I tried using:

SendKeys.SendWait("{NUMPAD1}");

      

but he says

System.ArgumentException: NUMPAD1 keyword is invalid (translated)

So, I don't know the correct code for NumPad.

+3


source to share


2 answers


Out of curiosity, I looked at the source code for SendKeys. There is nothing to explain why the numpad codes were excluded. I would not recommend this as the preferred option, but it is 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 altcodes, so maybe that's why they left them behind.

+1


source


You should be able to transmit the number in the same way as in the letter. For example:



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

      

0


source







All Articles