Tastenklicks simulieren

Programmierung unter AOO/LO (StarBasic, Python, Java, ...)

Moderator: Moderatoren

delta9
****
Beiträge: 100
Registriert: Fr, 19.03.2010 15:28

Tastenklicks simulieren

Beitrag von delta9 » Mi, 09.01.2019 12:20

Hallo,
ich möchte gerne von einem Calc-Makro aus Tastaturklicks in anderen geöffneten Anwendungen simulieren.
Mit Mausklicks habe ich es geschafft (s.u.) aber geht das auch mit dem Keyboard?
Stefan
PS: Ich gebe zu, das ist Murks, aber ich könnte es gut gebrauchen ;-)

Code: Alles auswählen

  ' simuliere einen linken Mausklick 
  Private Declare Sub mouse_event Lib "user32" _
  Alias "mouse_event" ( _
  ByVal dwFlags As Long, _
  ByVal dx As Long, _
  ByVal dy As Long, _
  ByVal cButtons As Long, _
  ByVal dwExtraInfo As Long)
  Public Sub SendMausklick()
  
  Const MOUSEEVENTF_LEFTDOWN = &H2 
  Const MOUSEEVENTF_LEFTUP = &H4 
  Call mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
  Call mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
end sub
 
 

marcel_at_work
****
Beiträge: 185
Registriert: Sa, 24.04.2010 15:51
Wohnort: Basel [CH]

Re: Tastenklicks simulieren

Beitrag von marcel_at_work » Sa, 18.01.2020 23:11

Hi,

der Beitrag ist schon etwas älter, aber da ich mich gerade etwas in die WinAPI einarbeite, hier, für alle, die es interessiert:

Code: Alles auswählen

Private Declare Sub keybd_event Lib "user32.dll" ( _
  ByVal bVk As Byte, _
  ByVal bScan As Byte, _
  ByVal dwFlags As Long, _
  ByVal dwExtraInfo As Long)
 
' keybd_event dwFlags-Konstanten
Private Const KEYEVENTF_EXTENDEDKEY = &H1 ' Erweiterte Tastatureingabe
Private Const KEYEVENTF_KEYUP = &H2 ' Die angegebene Taste wird losgelassen
 
' GetAsyncKeyState vKey-Konstanten
Private Const VK_LBUTTON = &H1  ' Linker Mausbutton
Private Const VK_RBUTTON = &H2  ' Rechter Mausbutton
Private Const VK_MBUTTON = &H4  ' Mittlerer Masubutton
Private Const VK_BACK = &H8  ' Backspace Taste
Private Const VK_TAB = &H9  ' Tab Taste
Private Const VK_CLEAR = &HC  ' Numpad 5 ohne Numlock
Private Const VK_RETURN = &HD  ' Enter Taste
Private Const VK_SHIFT = &H10  ' Shift Taste
Private Const VK_CONTROL = &H11  ' STRG Taste
Private Const VK_MENU = &H12  ' Alt Taste
Private Const VK_PAUSE = &H13  ' Pause/Untbr
Private Const VK_CAPITAL = &H14  ' Caps Lock/Feststelltaste
Private Const VK_ESCAPE = &H1B  ' Escape
Private Const VK_SPACE = &H20  ' Space/Leertaste
Private Const VK_PRIOR = &H21  ' PageUp/Bild hoch
Private Const VK_NEXT = &H22  ' PageDown/Bild runter
Private Const VK_END = &H23  ' Ende
Private Const VK_HOME = &H24  ' Home/Pos1
Private Const VK_LEFT = &H25  ' Linke Pfeiltaste
Private Const VK_UP = &H26  ' Obere Pfeilaste
Private Const VK_RIGHT = &H27  ' Rechte Pfeiltaste
Private Const VK_DOWN = &H28  ' Untee Pfeiltaste
Private Const VK_PRINT = &H2A  ' Drucken (Nokia Tastaturen)
Private Const VK_SNAPSHOT = &H2C  ' Drucken/S-Abf
Private Const VK_INSERT = &H2D  ' Einfügen
Private Const VK_DELETE = &H2E  ' Entfernen
Private Const VK_HELP = &H2F  ' Hilfe
Private Const VK_0 = &H30  ' Taste 0
Private Const VK_1 = &H31  ' Taste 1
Private Const VK_2 = &H32  ' Taste 2
Private Const VK_3 = &H33  ' Taste 3
Private Const VK_4 = &H34  ' Taste 4
Private Const VK_5 = &H35  ' Taste 5
Private Const VK_6 = &H36  ' Taste 6
Private Const VK_7 = &H37  ' Taste 7
Private Const VK_8 = &H38  ' Taste 8
Private Const VK_9 = &H39  ' Taste 9
Private Const VK_A = &H41  ' Taste A
Private Const VK_B = &H42  ' Taste B
Private Const VK_C = &H43  ' Taste C
Private Const VK_D = &H44  ' Taste D
Private Const VK_E = &H45  ' Taste E
Private Const VK_F = &H46  ' Taste F
Private Const VK_G = &H47  ' Taste G
Private Const VK_H = &H48  ' Taste H
Private Const VK_I = &H49  ' Taste I
Private Const VK_J = &H4A  ' Taste J
Private Const VK_K = &H4B  ' Taste K
Private Const VK_L = &H4C  ' Taste L
Private Const VK_M = &H4D  ' Taste M
Private Const VK_N = &H4E  ' Taste N
Private Const VK_O = &H4F  ' Taste O
Private Const VK_P = &H50  ' Taste P
Private Const VK_Q = &H51  ' Taste Q
Private Const VK_R = &H52  ' Taste R
Private Const VK_S = &H53  ' Taste S
Private Const VK_T = &H54  ' Taste T
Private Const VK_U = &H55  ' Taste U
Private Const VK_V = &H56  ' Taste V
Private Const VK_W = &H57  ' Taste W
Private Const VK_X = &H58  ' Taste X
Private Const VK_Y = &H59  ' Taste Y
Private Const VK_Z = &H5A  ' Taste Z
Private Const VK_STARTKEY = &H5B  ' Startmenütaste
Private Const VK_CONTEXTKEY = &H5D  ' Kentextmenü
Private Const VK_NUMPAD0 = &H60  ' Numpad Taste 0
Private Const VK_NUMPAD1 = &H61  ' Numpad Taste 1
Private Const VK_NUMPAD2 = &H62  ' Numpad Taste 2
Private Const VK_NUMPAD3 = &H63  ' Numpad Taste 3
Private Const VK_NUMPAD4 = &H64  ' Numpad Taste 4
Private Const VK_NUMPAD5 = &H65  ' Numpad Taste 5
Private Const VK_NUMPAD6 = &H66  ' Numpad Taste 6
Private Const VK_NUMPAD7 = &H67  ' Numpad Taste 7
Private Const VK_NUMPAD8 = &H68  ' Numpad Taste 8
Private Const VK_NUMPAD9 = &H69  ' Numpad Taste 9
Private Const VK_MULTIPLY = &H6A  ' Numpad Multiplikations Taste (*)
Private Const VK_ADD = &H6B  ' Numpad Additions Taste (+)
Private Const VK_SUBTRACT = &H6D  ' Numpad Subtrations Taste (-)
Private Const VK_DECIMAL = &H6E  ' Numpad Komma Taste (,)
Private Const VK_DIVIDE = &H6F  ' Numpad Devidierungs Taste (/)
Private Const VK_F1 = &H70  ' F1 Taste
Private Const VK_F2 = &H71  ' F2 Taste
Private Const VK_F3 = &H72  ' F3 Taste
Private Const VK_F4 = &H73  ' F4 Taste
Private Const VK_F5 = &H74  ' F5 Taste
Private Const VK_F6 = &H75  ' F6 Taste
Private Const VK_F7 = &H76  ' F7 Taste
Private Const VK_F8 = &H77  ' F8 Taste
Private Const VK_F9 = &H78  ' F9 Taste
Private Const VK_F10 = &H79  ' F10 Taste
Private Const VK_F11 = &H7A  ' F11 Taste
Private Const VK_F12 = &H7B  ' F12 Taste
Private Const VK_F13 = &H7C  ' F13 Taste
Private Const VK_F14 = &H7D  ' F14 Taste
Private Const VK_F15 = &H7E  ' F15 Taste
Private Const VK_F16 = &H7F  ' F16 Taste
Private Const VK_F17 = &H80  ' F17 Taste
Private Const VK_F18 = &H81  ' F18 Taste
Private Const VK_F19 = &H82  ' F19 Taste
Private Const VK_F20 = &H83  ' F20 Taste
Private Const VK_F21 = &H84  ' F21 Taste
Private Const VK_F22 = &H85  ' F22 Taste
Private Const VK_F23 = &H86  ' F23 Taste
Private Const VK_F24 = &H87  ' F24 Taste
Private Const VK_NUMLOCK = &H90  ' Numlock Taste
Private Const VK_OEM_SCROLL = &H91  ' Scroll Lock
Private Const VK_LSHIFT = &HA0 ' Linke Shift-Taste
Private Const VK_RSHIFT = &HA1 ' Rechte Shift-Taste
Private Const VK_LCONTROL = &HA2 ' Linke STRG-Taste
Private Const VK_RCONTROL = &HA3 ' Rechte STRG-Taste
Private Const VK_LMENU = &HA4 ' Linke ALT-Taste
Private Const VK_RMENU = &HA5 ' Rechte ALT-Taste
Private Const VK_OEM_1 = &HBA  ' ";"-Taste
Private Const VK_OEM_PLUS = &HBB  ' "
Private Const VK_OEM_COMMA = &HBC  ' ","-Taste
Private Const VK_OEM_MINUS = &HBD  ' "-"-Taste
Private Const VK_OEM_PERIOD = &HBE  ' "."-taste
Private Const VK_OEM_2 = &HBF  ' "/"-Taste
Private Const VK_OEM_3 = &HC0 ' "`"-Taste
Private Const VK_OEM_4 = &HDB  ' "["-Taste
Private Const VK_OEM_5 = &HDC  ' "\"-Taste
Private Const VK_OEM_6 = &HDD  ' "]"-Taste
Private Const VK_OEM_7 = &HDE  ' "
Private Const VK_ICO_F17 = &HE0  ' F17 einer Olivetti Tastatur (Intern)
Private Const VK_ICO_F18 = &HE1  ' F18 einer Olivetti Tastatur (Intern)
Private Const VK_OEM102 = &HE2  ' "<"-Taste oder "|"-Taste einer  _
' IBM-Kompatiblen 102 Tastatur (Nicht US)
Private Const VK_ICO_HELP = &HE3  ' Hilfetaste einer Olivetti Tastatur ' (Intern) 
Private Const VK_ICO_00 = &HE4  ' 00-Taste einer Olivetti Tastatur (Intern)
Private Const VK_ICO_CLEAR = &HE6  ' Löschen Taste einer Olivetti Tastatur ' (Intern) 
Private Const VK_OEM_RESET = &HE9  ' Reset Taste (Nokia)
Private Const VK_OEM_JUMP = &HEA  ' Springen Taste (Nokia)
Private Const VK_OEM_PA1 = &HEB  ' PA1 Taste (Nokia)
Private Const VK_OEM_PA2 = &HEC  ' PA2 Taste (Nokia)
Private Const VK_OEM_PA3 = &HED  ' PA3 Taste (Nokia)
Private Const VK_OEM_WSCTRL = &HEE  ' WSCTRL Taste (Nokia)
Private Const VK_OEM_CUSEL = &HEF  ' WSCTRL Taste (Nokia)
Private Const VK_OEM_ATTN = &HF0  ' ATTN Taste (Nokia)
Private Const VK_OEM_FINNISH = &HF1  ' Fertig Taste (Nokia)
Private Const VK_OEM_COPY = &HF2  ' Kopieren Taste (Nokia)
Private Const VK_OEM_AUTO = &HF3  ' Auto Taste (Nokia)
Private Const VK_OEM_ENLW = &HF4  ' ENLW Taste (Nokia)
Private Const VK_OEM_BACKTAB = &HF5  ' BackTab Taste (Nokia)
Private Const VK_ATTN = &HF6  ' ATTN-Taste
Private Const VK_CRSEL = &HF7  ' CRSEL-Taste
Private Const VK_EXSEL = &HF8  ' EXSEL-Taste
Private Const VK_EREOF = &HF9  ' EREOF-Taste
Private Const VK_PLAY = &HFA  ' PLAY-Taste
Private Const VK_ZOOM = &HFB  ' ZOOM-Taste
Private Const VK_NONAME = &HFC  ' NONAME-Taste
Private Const VK_PA1 = &HFD  ' PA1-Taste
Private Const VK_OEM_CLEAR = &HFE  ' OEM_CLEAR-Taste

' Programm beenden per ALT+F4
Private Sub Command1_Click()
 
  ' Alt+F4 drücken
  Call keybd_event(VK_LMENU, 0&, 0&, 0&)
  Call keybd_event(VK_F4, 0&, 0&, 0&)
 
  ' Und wieder loslassen
  Call keybd_event(VK_LMENU, 0&, KEYEVENTF_KEYUP, 0&)
  Call keybd_event(VK_F4, KEYEVENTF_KEYUP, 0&, 0&)
End Sub
Dies ist das WinAPI Keyboard-Event aus der Bibliothek von Dieter Otter/vb@rchiv.
Der Code ist in VBA und erfordert noch die Adaption nach OOo (soweit ich das gerade sehe, aber nur die Typdeklaration 0&/Long...)

Viele Grüße,

Marcel

Antworten