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.
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.
The syntax is quite simple, but some remarks are still needed:
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]")
Um einen einzelnen Tastendruck oder eine Kombination von Tasten zu generieren und an das Fenster mit dem Fokus zu senden, können Sie die Methode SendKeys(..) einsetzen.
Syntax:
Static Sub SendKeys ( Keys As String )
Keys ist dabei eine Liste von Tasten, die nacheinander gesendet werden. Die (formalen) Namen der Tasten sind in der X11-Header-Datei /usr/include/X11/keysymdef.h aufgelistet. Ausgewählte Tastennamen auf dem Ziffernblock (Keypad) ermitteln Sie zum Beispiel mit diesem Befehl in einer Konsole:
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:~$
Um einen Tastennamen zu extrahieren, müssen Sie den XK_-Präfix aus der Konstanten in der Datei /usr/include/X11/keysymdef.h entfernen. Die Taste mit der Bezeichnung [1/End] im Ziffernblock hat den Tastennamen KP_End – aber nur dann, wenn der Ziffernblock mit der Taste NumLock de-aktiviert wurde – sonst wird die Ziffer 1 wirksam.
Leider erkennen Sie nicht, dass bei den Tastennamen zwischen Groß-und Kleinschreibung unterschieden wird und das nicht immer konsistent. Zum Beispiel beginnt 'Return' mit einem großen R, aber die Leertaste mit dem Tastennamen 'space' mit kleinem s. In der Datei /usr/include/X11/keysymdef.h finden Sie den Namen XK_KP_Space, während das Tastenanalyse-Programm xev die gedrückte Leertaste als 'space' beschreibt:
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
Das i-Tüpfelchen setzt eine Bemerkung von Minisini zu den Änderungen bei den Tastenkombinationen: “X11 fand es toll, vor kurzem die Bedeutung einzelner Tasten zu ändern. Bisher wurden Großbuchstaben benutzt, jetzt benutzen sie eine Mischung aus Groß-und Kleinbuchstaben. Sehr lustig.”
Aus diesem Grunde funktioniert zur Zeit nur Desktop.SendKeys(“{[Control_L]c}”) mit kleinem c korrekt!
Bedenken Sie die Unterschiede der Bezeichnungen für die einzelnen Tasten, wie sie
Um zum Beispiel auf einer deutschen Tastatur im alpha-numerischen Block das Fragezeichen oder den Backslash zu erreichen, müssen Sie in die 2. oder 3. Belegung der ß-Taste umschalten. Der Umschalter oder Modifizierer für die 2. Ebene ist die Shift-Taste (→ ?) und mit der Taste AltGr (alternativer Grafik-Zeichensatz; Tastenname: ISO_Level3_Shift) kommen Sie in die 3. Ebene und erreichen den Backslash (→ \). Die zweite Belegung der Druck-Taste erreichen Sie zum Beispiel im Normalfall mit dem Modifizierer Alt-Taste.
Die Syntax ist recht einfach, doch es sind noch einige Bemerkungen notwendig:
Beispiele:
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]}") ' Bildschirmkopie Desktop.SendKeys("{[Alt_L]}{[Print]}") ' Bildschirmkopie des aktiven Fensters Desktop.SendKeys("[NumLock]") Desktop.SendKeys("[F1]")