在 python 中如何按特定键执行某些操作?

How do you press a specific key to do something in python?

我想知道在 python 中按特定键做某事,不,不是键盘库,它需要 root 权限。我可以做 sudo /bin/python3.7 "path of python file" 但这会很痛苦。顺便说一句,这是我的 python 代码:

import keyboard
import pyautogui as auto
import time

auto.hotkey('alt', 'F2')
auto.write('mousepad')
time.sleep(int(1))
auto.write('do you want to start game?')

keyboard.wait('y')
from pynput.keyboard import *
import pyautogui as auto
import time

auto.hotkey('alt', 'F2')
time.sleep(int(1))
auto.write('mousepad')
time.sleep(int(1))
auto.write('do you want to start game?')

if key == Key.y:
    # Stop listener
    return False

def on_press(key):
    doing_something_here()


with Listener(
        on_press=on_press,
        on_release=None) as listener:
    listener.join()