首页 > 解决方案 > 是否可以在没有任何导入模块/库的情况下在 Python 中模拟按键

问题描述

我正在使用 Python(2.4)并尝试模拟按键(CTRL,SHIFT)。但是我的 python 内置在一个媒体应用程序/框架中(来自 Peavey),它允许我在 Python 中进行非常基本的编码,但没有安装/导入任何模块、库的能力。

所以我不能使用任何库/模块进行按键操作,例如:键盘、pyinput、ctype、Win32、wintype 等。

有什么方法可以在不使用脚本顶部的“导入”的情况下模拟按键?

先感谢您...

我尝试了以下方法:

import keyboard  # using module keyboard
while True:  # making a loop
    try:  # used try so that if user pressed other than the given key error will not be shown
        if keyboard.is_pressed('q'):  # if key 'q' is pressed 
            print('You Pressed A Key!')
            break  # finishing the loop
        else:
            pass
    except:
        break  # if user pressed a key other than the given key the loop will break

但是在执行时,它给了我错误说明:

Traceback (most recent call last):
  File "key.py", line 1, in ?
    import keyboard  # using module keyboard
ImportError: No module named keyboard

当我在生产中使用代码时,我无法导入库。

标签: pythonkeyboardkeypress

解决方案


推荐阅读