Python Forum

Full Version: Typing into CMD Prompt while its behind another window
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I made a Auto Hotkey script to type into a command prompt while the command prompt window was in the back and not selected. With one button it would type 3 things after waiting a few seconds in between and it looked a little like this.
{
*$F1::
ConsoleSend("Hello World")
Sleep, 200
ConsoleSend("`r", "ahk_class ConsoleWindowClass")
Sleep, 1000
ConsoleSend("Random Text", "ahk_class ConsoleWindowClass")
Sleep, 200
ConsoleSend("`r", "ahk_class ConsoleWindowClass")
Sleep, 15000
ConsoleSend("More Random Text", "ahk_class ConsoleWindowClass")
Sleep, 200
ConsoleSend("`r", "ahk_class ConsoleWindowClass")
Sleep, 1000
SetKeyDelay 30,50
Send, {ALT DOWN}{TAB}{ALT UP}
Return
I'm trying to train myself to use python and run the same script and I know its messy but I got this so far:
from pynput import keyboard
from pynput.keyboard import Controller

COMBINATIONS = [
    {keyboard.Key.f7},
    {keyboard.Key.f7, keyboard.KeyCode(char='A')}
]


keyboard = Controller()
current = set()



def start_cmd():
    keyboard.type("Hello World!")

from pynput import keyboard

def on_press(key):
    if any([key in COMBO for COMBO in COMBINATIONS]):
        current.add(key)
        if any(all(k in current for k in COMBO) for COMBO in COMBINATIONS):
            start_cmd()

def on_release(key):
    if any([key in COMBO for COMBO in COMBINATIONS]):
        current.remove(key)


with keyboard.Listener(on_press=on_press, on_release=on_release) as listener:
    listener.join()
I broke it and it errors now after trying to add "from pynput import keyboard" but so far I got it to see what keys I'm pressing no matter what window I am focused on and I got it to write text (with a key binding) in the window I am currently focused on. But I can't find a way to type something into the window or command prompt that is running in the back. Anyone know how to fix this?