[Python] Monitor keyboard while a game is running

Discussão em 'Python' iniciado por Stack, Maio 9, 2025.

  1. Stack

    Stack Membro Participativo

    How to monitor keyboard input while GTA5 is running using python script.

    I want to press a key while the game is running to directly terminate the game process.I’ve tried using the keyboard and pynput libraries.These methods cannot achieve the desired result.

    from pynput import keyboard
    import psutil

    def kill_gta5():
    for proc in psutil.process_iter(['pid', 'name']):
    if 'GTA5_Enhanced.exe' in proc.info['name']:
    proc.kill()

    def on_press(key):
    try:
    if key == keyboard.Key.f12:
    kill_gta5()
    return False
    except AttributeError:
    pass

    with keyboard.Listener(on_press=on_press) as listener:
    listener.join()

    Continue reading...

Compartilhe esta Página