Hangfish
Hangfish

Reputation: 354

Press key with pywinauto

Extremely straightforward question.

Just want to press a keyboard key. Like enter, using pywin auto. I don't want to press it in the context of any application window.

Just a raw keypress of a keyboard key, like a or enter or backspace.

Upvotes: 8

Views: 33187

Answers (3)

DagicCross
DagicCross

Reputation: 401

I got this running for my game, and it works on notepad.exe too

from pywinauto import Application

app = Application(backend="win32").connect(path="Mario")
# app.MarioClass.minimize() # if you want to minimized
app.MarioClass.send_keystrokes('Hi {ENTER}')

Upvotes: 0

Olc
Olc

Reputation: 69

I had to change the include to get the code working:

from pywinauto.keyboard import send_keys, KeySequenceError

send_keys('some text{ENTER 2}some more textt{BACKSPACE}', with_spaces=True)

Upvotes: 2

Vasily Ryabov
Vasily Ryabov

Reputation: 10000

Just use

# from pywinauto.SendKeysCtypes import SendKeys # old for pywinauto==0.5.x
from pywinauto.keyboard import send_keys

send_keys('some text{ENTER 2}some more textt{BACKSPACE}', with_spaces=True)

Docs: https://pywinauto.readthedocs.io/en/latest/code/pywinauto.keyboard.html

P.S. SendKeysCtypes was renamed to keyboard in pywinauto 0.6.0+.

Upvotes: 6

Related Questions