Python Forum
Typing into CMD Prompt while its behind another window
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Typing into CMD Prompt while its behind another window
#1
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?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Is there a way to call and focus any popup window outside of the main window app? Valjean 6 1,737 Oct-02-2023, 04:11 PM
Last Post: deanhystad
  Pywinauto typing in the wrong field EGameiro 0 676 Jun-07-2023, 10:01 PM
Last Post: EGameiro
  Pyspark Window: perform sum over a window with specific conditions Shena76 0 1,165 Jun-13-2022, 08:59 AM
Last Post: Shena76
  Mypy typing error WannaBePythonDev 2 1,922 Sep-05-2021, 10:18 AM
Last Post: WannaBePythonDev
  Typing and variable initializers jgossage 0 1,622 May-29-2020, 03:18 PM
Last Post: jgossage
  Need Help Typing Text into Tough Form [xpath / selenium] digitalmatic7 0 1,745 Jun-05-2019, 06:46 AM
Last Post: digitalmatic7
  Is there any way to simulate clicks/typing to an inactive window using Python? Nwb 1 5,912 Jun-11-2018, 08:56 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020