How do you send NumPad keys using SendKeys?
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 to share