'NCurses and ESC,ALT keys
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?
Solution 1:[1]
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
Solution 2:[2]
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)
Solution 3:[3]
You can use curses.ascii.ESC
Solution 4:[4]
if you don't care to support users who hit escape and then another key (a holdover from old terminals vt100 era terminals I think), and just want to respond to the physical keys on the pc 101 key-board, you can set this at the start of your (c) code:
ESCDELAY = 10;
the man page explains what's going on in more detail: https://man7.org/linux/man-pages/man3/curs_variables.3x.html
then use keyname() to get an easily strcmp'ed human readable name for what was pressed such as ^c for control+c. See How to get Ctrl, Shift or Alt with getch() ncurses?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | L Y E S - C H I O U K H |
| Solution 2 | sjohnson.pi |
| Solution 3 | Zren |
| Solution 4 | Al Ro |
