Is there a way to simulate Windows alt-tabbing?

Viewed 637

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?

1 Answers

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

Related