I have been reading the documentation on curses in Python but I cannot figure it out how to detect the combinations like CTRL-S, or CTRL-A, or ALT-X, or CMD+A (in macOS), etc. Anyone knows how to do it?
Someone mentioned user ord(), but I cannot find the number that CTRL returns.
Thanks in advance
windows.getkey
returns ^A
for CTRL-A, wich is a special character with ord 1. Simlarly, CTRL-E will return ^E
which has ord(5).
Try this for seeing the strings that getkey
returns when you press a key. Note that some keys (Cursor keys for instance) will return a mutlti-byte string.
from curses import wrapper
def main(stdscr):
while True:
key = stdscr.getkey()
stdscr.clear()
stdscr.addstr(1,0, "Key pressed is '{}' (ord {})".format(key, ord(key[0])))
stdscr.refresh()
if ord(key) == 24: # CTRL-X
break
wrapper(main)