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. Is there any way to send keystrokes without bringing the window into focus?
I wish to keep the terminal in focus while my script does tasks on a window in the background. (mainly sending different keystrokes like ctrl+Tab
, pgDn/pgUp
, etc)
Edit - the suggested question does not answer my question because the solution over there brings the target window into focus. I want to keep the target window in the background itself.
Thank you to @Vasily Ryabov, the code now even works with PyWinAuto. :) This is the final code that works:
import time
import pywinauto
app = pywinauto.Application(backend="win32").start(r"C:\Program Files (x86)\Adobe\Acrobat DC\Acrobat\Acrobat.exe")
time.sleep(10)
print('sleeping over')
Wizard = app['testing.pdf - Adobe Acrobat Pro DC']
while True:
Wizard.send_keystrokes("{VK_NEXT}")
time.sleep(1)
I think the problem was that i was using type_keys
method instead of send_keystrokes
. Also, going back & forth between win32
& uia
helped me as well to find the one that works correctly. For Acrobat DC Pro, win32
works.
Thank you @Vasily Ryabov for your excellent help & your wonderful Python library PyWinAuto! :)