User Tools

Site Tools


k15:k15.1:k15.1.2:start

15.1.2 Method SendKeys - Class Desktop (gb. desktop)

To generate a single keystroke or a combination of keys and send them to the window with the focus, you can use the SendKeys(..) method.

Syntax:

Static Sub SendKeys ( Keys As String )

Keys is a list of keys that are sent one after the other. The (formal) names of the keys are listed in the X11 header file /usr/include/X11/keysymdef.h. For example, you can use this command in a console to determine selected key names on the keypad:

hans@linux:~$ cat /usr/include/X11/keysymdef.h | grep 'KP_E' 
#define XK_KP_Enter                      0xff8d  /* Enter */ 
#define XK_KP_End                        0xff9c 
#define XK_KP_Equal                      0xffbd  /* Equals */ 
hans@linux:~$ 

To extract a key name, remove the XK_ prefix from the constants in the /usr/include/X11/keysymdef.h file. The key labeled[1/End] in the numeric keypad has the key name KP_End - but only if the numeric keypad has been deactivated with the NumLock key - otherwise the digit 1 becomes effective.

Unfortunately, you do not see that the key names are case-sensitive and not always consistent. For example,' Return' starts with a large R, but the space bar with the key name' space' starts with a small s. In the file /usr/include/X11/keysymdef.h you will find the name XK_KP_Space, while the key analysis program xev describes the pressed space bar as 'space':

KeyRelease event, serial 33, synthetic NO, window 0x3400001, 
    root 0xbc, subw 0x3400002, time 15995041, (33,37), root:(149,566), 
    state 0x0, keycode 65 (keysym 0x20, space), same_screen YES, 
    XLookupString gives 1 bytes: (20) " " 
    XFilterEvent returns: False

The icing on the cake is a remark from Minisini about the changes in keyboard shortcuts:“X11 found it great to change the meaning of individual keys recently. Previously capital letters were used, now they use a mixture of upper and lower case letters. Very funny.”

For this reason only Desktop.SendKeys (“{[Control_L]c}”) with small c is working correctly!

Consider the differences in the names of the individual keys, as they differ from each other.

  • in the X11 header file /usr/include/X11/keysymdef.h,
  • from the key analysis program xev,
  • on the real keys on the keyboard and
  • using keyboard constants in gambas.

15.1.2.1 Modifier

For example, to reach the question mark or backslash on a German keyboard in the alpha-numeric block, you have to switch to the 2nd or 3rd assignment of the german ß-key. The switch or modifier for the 2nd level is the Shift key and with the AltGr key (alternative graphic character set; key name: ISO_Level3_Shift) you get to the 3rd level and reach the backslash (→ \). The second assignment of the print key can usually be reached with the modifier Alt key, for example.

15.1.2.2 Generate keystroke

The syntax is quite simple, but some remarks are still needed:

  • If several keys have to be pressed at the same time, they must be enclosed between '{' and '}'. To send the ASCII character '{', you must use “{{}”.
  • What is sent is not a string, but the internal key codes. The required modifiers are automatically sent before an (ASCII) character is sent - as in the case of Desktop, for example, Desktop.SendKeys(“M”).
  • Each key name must be enclosed between '[' and']'. But you can send any ASCII character as it is. For example, you can send the '\n' character for the return key or the '\t' character for the tab key.

Examples:

Desktop.SendKeys("A") «  Desktop.SendKeys("{[Shift_L]a}") « Desktop.SendKeys("{[Shift_R]a}")
Desktop.SendKeys("\n") «  Desktop.SendKeys("[Return]")
Desktop.SendKeys("{[Control_L]c}")
Desktop.SendKeys("Gambas Almost Means BASic")
Desktop.SendKeys(TextArea.Text)
Desktop.SendKeys("{[Control_L]}{[Alt_L]}{[Print]}") ' Screenshot
Desktop.SendKeys("{[Alt_L]}{[Print]}")  ' Screenshot of the active window
Desktop.SendKeys("[NumLock]")
Desktop.SendKeys("[F1]")

Download

The website uses a temporary session cookie. This technically necessary cookie is deleted when the browser is closed. You can find information on cookies in our privacy policy.
k15/k15.1/k15.1.2/start.txt · Last modified: 28.09.2023 by emma

Page Tools