spyoungtech / ahk

Python wrapper for AutoHotkey with full type support. Harness the automation power of AutoHotkey with the beauty of Python.
MIT License
887 stars 66 forks source link

To send key press or release event to a ahk window? #326

Closed vfptr closed 3 months ago

vfptr commented 4 months ago

Checked the documentation

describe your feature request

I want to simulate long press event by sending keys release or press event to a not activated window. I did not find any way to do this in AHK.Window class.

spyoungtech commented 4 months ago

The way you do this is through the send method of the Window class. Doing a long press works the same way as described in the docs describing Repeating or Holding Down a Key. (these features also work with ControlSend, which is the underlying functionality of Window.send).

For example, the AutoHotkey documentation provides:

Send {Up down}  ; Presses down the up-arrow key.
Sleep 1000  ; Keeps it down for one second.
Send {Up up}  ; Releases the up-arrow key.

Using the wrapper, the equivalent doing what you want to a window that is not active:

win = ahk.win_get(title='my window')
win.send('{Up down}')
time.sleep(1)
win.send('{Up up}')

Keep in mind, some windows (especially games) may simply not accept input at all while the window is not active.

vfptr commented 3 months ago

Thank you very much for your answer