How to intercept special (alt / ctrl) key press?

点点圈 提交于 2019-12-10 03:26:40

问题


How can I catch key combinations like ALT+K or CTRL+ALT+H in python curses?


回答1:


A terminal converts the control key in combination with a letter key to a normal ASCII code. This can be read from the getch() function like any other key press.

CTRL-A: getch() returns 1
CTRL-B: getch() returns 2
...
CTRL-Z: getch() returns 26

Also, the keypad() function must be called to enable other special function keys (e.g. left arrow, F1, home, etc).

I don't believe there is a portable way to receive meta-keys (e.g. ALT-H) through a terminal. Some send an escape (0x1B) followed by the modified key while others set the high-bit and only send one byte.



来源:https://stackoverflow.com/questions/406933/how-to-intercept-special-alt-ctrl-key-press

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!