问题
I have a problem with NCurses... i need to handle all keys like Esc, Alt+F etc. Problem is that the codes are similar... i.e:
Esc - 27
Alt+A - 27 65
As an example there is double code for Alt+[key] combination what similar to Esc key... Any ideas how handle that?
回答1:
Here is an example for python:
key = self.screen.getch()
if key == ord('q'): # quit
go = False
elif key == 27: # Esc or Alt
# Don't wait for another key
# If it was Alt then curses has already sent the other key
# otherwise -1 is sent (Escape)
self.screen.nodelay(True)
n = self.screen.getch()
if n == -1:
# Escape was pressed
go = False
# Return to delay
self.screen.nodelay(False)
回答2:
Resolved by:
- Use noecho or timeout mode
- Check for 27(ALT or ESC) code... if pass:
- try to read another code
- if another code is ERR then.. you have ESC key in other way you have ALT+another code
回答3:
You can use curses.ascii.ESC
- https://docs.python.org/3/library/curses.ascii.html#module-curses.ascii
- https://github.com/python/cpython/blob/master/Lib/curses/ascii.py
来源:https://stackoverflow.com/questions/5977395/ncurses-and-esc-alt-keys