3

I want to create a program which alt tabs into for example discord, and enters a message to a user.

import time
from pynput.keyboard import Key, Controller
keyboard=Controller()
keyboard.press(Key.alt_l)
keyboard.press(Key.tab)
keyboard.release(Key.tab)
keyboard.release(Key.alt_l)
time.sleep(1)
keyboard.type('this is a test message.')
keyboard.press(Key.enter)

That's what I have currently to simulate alt tabbing, however is it possible to alt tab into a specific program?

martineau
  • 119,623
  • 25
  • 170
  • 301
sanguine
  • 107
  • 1
  • 8
  • 3
    I'd not alt-tab, as that depends on what windows are open and what you used before. Most likely you can find a window by its title or process anyway. See https://stackoverflow.com/questions/2090464/python-window-activation – Robert Aug 21 '20 at 16:54
  • 1
    Does this answer your question? [Python Window Activation](https://stackoverflow.com/questions/2090464/python-window-activation) – Robert Aug 21 '20 at 16:54
  • @Robert No that did not help, thank you for trying though! – sanguine Aug 21 '20 at 17:41

1 Answers1

1

You can achieve something similar using pywinauto, which is a module that allows you to interact with a programs GUIs in a pythonic way. Example:

from pywinauto.application import Application
app = Application().start("notepad.exe")

app.UntitledNotepad.menu_select("File->New")

You can read more about pywinauto and what it can do in its documentation here

nordmanden
  • 340
  • 1
  • 10
  • Hey! Thank you, does this only work with windows apps? Also, how would I use this to set focus onto the discord app? – sanguine Aug 21 '20 at 17:39
  • Hey, I wont have time today but if you still need help monday I'll set up an example :-) – nordmanden Aug 22 '20 at 17:59
  • 1
    Hey @nordmanden I figured it out, you can import a module called pygetwindow and use win=gw.getWindowsWithTitle('application name')[0] win.activate() and it works fine! – sanguine Aug 22 '20 at 19:59