What exactly must be specified for keycode and keychar when simulating keypresses (vb.net or c# with dotnetbrowser)

297 Views Asked by At

(I'm asking with vb examples, but the same applies for c# and the answer would be the same.)

I'm trying to fill in a form using dotnetbrowser and have problems understanding what exactly to put into the keychar and keycode/virtualkey parameters when I want to "press" special keys like "down arrow", "tab", "enter". The "KeyCode Enumeration" table lists, for example, "tab (9)", return (13)", "down arrow (40)", "A (65)". So, to type an A I would need to do the following:

SimulateKey(keyboard, 65, "A")

or, just written differently,

SimulateKey(keyboard, 65, chr(65))

whichs works fine for "A", and

SimulateKey(keyboard, 97, chr(97))

works fine for "a", although I don't understand why, because 97 is the keycode for "Numpad1".

But what do I specify for keychar for the special characters when I want to type tab-return-down-A?

SimulateKey(keyboard, 9, chr(9))
SimulateKey(keyboard, 13, chr(13))
SimulateKey(keyboard, 40, chr(40))   'where chr(40) is actually an opening parantheses "("
SimulateKey(keyboard, 65, chr(65))

or is keychar empty or space for the special characters like

SimulateKey(keyboard, 9, "") 'nothing between the qoutes
SimulateKey(keyboard, 13, " ") 'space-character (asc 32) between the quotes

What would I need to speciafy when I actually want to type an open parantheses "(", which is asc 40 - and thus has the same code as down? I would appreciate a thourough explanation on the keycode and keychar values.

For completeness, this is the code I use for SimulateKey (from the vb dotnetbrowser example or here for c#)

Private Shared Sub SimulateKey(keyboard As IKeyboard, key As KeyCode, keyChar As String, Optional ByVal modifiers As KeyModifiers = Nothing)
    modifiers = If(modifiers, New KeyModifiers())
    Dim keyPressedEventArgs = New KeyPressedEventArgs With {
            .KeyChar = keyChar,
            .VirtualKey = key,
            .Modifiers = modifiers
            }

    Dim keyTypedEventArgs = New KeyTypedEventArgs With {
            .KeyChar = keyChar,
            .VirtualKey = key,
            .Modifiers = modifiers
            }
    Dim keyReleasedEventArgs = New KeyReleasedEventArgs With {
            .VirtualKey = key,
            .Modifiers = modifiers
            }

    keyboard.KeyPressed.Raise(keyPressedEventArgs)
    keyboard.KeyTyped.Raise(keyTypedEventArgs)
    keyboard.KeyReleased.Raise(keyReleasedEventArgs)
End Sub
1

There are 1 best solutions below

5
JosephDaSilva On

The key codes are Windows virtual key codes, which are listed here.

These codes represent physical keys on the keyboard, not characters. To distinguish between two characters that use the same key (such as A and a, or ( and 9), you will need additional information, such as whether the shift key was down and/or caps lock was on at the time the key was pressed. In fact, this is how KeyChar is computed from the key code for a real keyboard event. On the other hand, KeyChar cannot distinguish a regular digit key from a numpad digit key, and some keys (such as function keys) do not have any associated character, so the only way to detect these keys is through the key code.