简体   繁体   English

是否可以向Python解释器添加任意键绑定?

[英]Is it possible to add arbitrary keybindings to the Python interpreter?

In the python interpreter we can use 在python解释器中,我们可以使用

readline.parse_and_bind('tab: complete')

to enable tab completion 启用制表符完成

Is it possible to bind arbitrary keys to our own functions? 是否可以将任意键绑定到我们自己的功能?
I'd like to bind CTRL+E and CTRL+SHIFT+E to edit_history() and edit_history(True) respectively, where edit_history() is my own function defined in .pythonrc 我想分别将CTRL + E和CTRL + SHIFT + E绑定到edit_history()和edit_history(True),其中edit_history()是我在.pythonrc中定义的函数

def edit_history(fork=False):
    import readline
    timeStamp = time.strftime("%Y%m%d-%H%M%S")
    tmpFile = '/tmp/pyHistory.%s' % timeStamp
    readline.write_history_file(tmpFile)
    if not fork:
        os.system('gvim -f %s' % tmpFile)
        readline.clear_history()
        readline.read_history_file(tmpFile)
        os.unlink(tmpFile)
    else:
        os.system('gvim %s' % tmpFile)

Any pointers would be greatly appreciated 任何指针将不胜感激

Thanks, 谢谢,
Dado 墙裙

Yes... kind of. 是的,有点。 After defining your edit_history function, you can map a key to the sequence "edit_history()\\n". 定义edit_history函数后,可以将键映射到序列“ edit_history()\\ n”。 Pressing that key would simulate typing "edit_history" followed by the Enter key. 按下该键将模拟键入“ edit_history”,然后按Enter键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM