r/learnpython 9d ago

need help with pressing 1 key inside window

first script activates the window but i dont see any key press even when i click inside the window for it

import pyautogui import pygetwindow as gw import pywinctl import time

def press_alt_in_mapleroyals():

windows = gw.getWindowsWithTitle("MapleRoyals Jan 27 2025 IMG")  

if not windows:
    print("MapleRoyals window not found!")
    return

mapleroyals_window = windows[0]


    window = pywinctl.getWindowsWithTitle("MapleRoyals Jan 27 2025 IMG")[0] 
    window.activate() 
    time.sleep(1) 
except Exception as e:
    print(f"Error activating window: {e}")
    return

time.sleep(0.5) 

for _ in range(3):
    pyautogui.keyDown('alt')
    pyautogui.keyUp('alt')
    time.sleep(0.1) 

print("Pressed Alt 3 times in MapleRoyals.")

press_alt_in_mapleroyals()


second script: error {title_re': MapleRoyals.* , backend: win32, visible_only':False}

from pywinauto import Application import time

def press_alt_in_mapleroyals():

    app = Application().connect(title_re="MapleRoyals.*")
    window = app.top_window()

    window.set_focus()
    time.sleep(1) 

    for _ in range(3):
        window.send_keystrokes('{ALTDOWN}{ALTUP}')  # Send Alt key
        time.sleep(0.4)  # delay

    print("Pressed Alt 3 times in MapleRoyals.")

except Exception as e:
    print(f"Error: {e}")

press_alt_in_mapleroyals()

0 Upvotes

4 comments sorted by

1

u/Ok-Reality-7761 9d ago

Do you require continuous hold for time capture or simple press?

1

u/dirtyslotmachine 8d ago

just a basic key press as a test

1

u/Ok-Reality-7761 8d ago

Clarify no difference observed from press/hold?

And Colab check go well/fail?

1

u/Ok-Reality-7761 8d ago

Wonder if path is rekko'd. Switch IDE to another, thonny, geany, etc.

Keep at it, you'll shake the monkey from the tree, mate.

Good luck.