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+.
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+.
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)
Send keystrokes not work for notepad and other programs
how do I use SendKeys? I am using pywinauto 0.6.5 and Python 3.5.1
winforms - How to send SendKeys to Windows form in python script? - Stack Overflow
python - pywinauto send key down to application - Stack Overflow
The code can be re-written simpler:
import pywinauto
app = pywinauto.application.Application().connect(title_re='Form1')
Form1 = app.Window_(title_re='Form1', class_name='WindowsForms10.Window.8.app.0.2bf8098_r13_ad1')
Form1.SetFocus()
Form1.TypeKeys("{PAUSE 2}")
Form1.TypeKeys("{TAB 2}{PAUSE 2}{ENTER}")
TypeKeys automatically sets a focus to the Form1 and types the keys. SendKeys doesn't set a focus because it's not aware about the window. That's probably why it doesn't work with SendKeys.
[EDIT] Of course you need to run the script as Administrator.
I got a fixed for this issue. The mistake I was doing is that, I was not running my script as Administrator. So that's why SendKeys event were not happening.
But when I ran my script as Administrator, SendKeys event successfully sent to Windows form.
Thanks Vasily for your help.
Hello,
I'm new to using pywinauto for some automation. I'm in the process of making a program that can automate on Pokemon Heartgold by using DesMuME. Initially, I was using pyautogui to automate the keystrokes and it was working perfectly. However, pyautogui needs the window to be in focus, which makes it so that I cannot use my computer to do other things in the meantime. This is when I started looking into pywinauto, to try and send keys to the program in the background while I do other things.
The issue is that none of the keystrokes are being sent. I'm still very new to pywinauto, and I might be doing things incorrectly, but I am not entirely sure. I've tried methods like type_keys(), send_chars() and send_keystrokes() but I was getting errors on each of them. I tried importing the send_keys() method from pywinauto.keyboard, but it doesn't work.
This is the code I have so far to initialize the application:
from pywinauto import Application
from pywinauto.keyboard import send_keys
import pywinauto
import time
app = Application(backend='uia').connect(title_re='DeSmuME')
desmume = app.window(title_re='DeSmuME')
# Even trying to send keystrokes while the window is focused, no luck
# desmume.set_focus()
# attempts at sending keys:
desmume.type_keys('{UP}')
desmume.send_chars('{UP}') # Error
desmume.send_keystrokes('{UP}') # Error
send_keys('{UP}')I don't really know what else there is to try. The pyautogui solution works as it should, but this would make it so much easier to run the program and leave it in the background while I do other things. If anyone knows how to fix, I would greatly appreciate it.
Tried this code:
import win32gui import win32con windowID = win32gui.FindWindow(None, "testing.pdf - Adobe Acrobat Pro DC") #win32gui.SetForegroundWindow(windowID) win32gui.SendMessage(windowID, win32con.WM_KEYDOWN, win32con.VK_NEXT, 0) #VK_NEXT is Page Down button
Above code works only if i de-comment the SetForegroundWindow line. Why won't it work without it? :(
I wish to keep the terminal in focus while my script does tasks on a window in the background. (mainly sending differenbt keystrokes like ctrl+Tab, pgDn/pgUp, etc)