是否可以在没有任何导入模块/库的情况下在Python中模拟按键

问题描述 投票:0回答:1

我正在使用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

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

python keyboard keypress
1个回答
0
投票

据我所知,如果不使用外部 python 库,就无法模拟按键。如果问题是您无法访问终端/shell,请使用

os.system(command)

其中命令是“pip install [library]”或“pip3 install [library]”

如果您遇到任何问题,也许可以尝试使用“git clone [库源]”

© www.soinside.com 2019 - 2024. All rights reserved.