user3310334
user3310334

Reputation:

What's the use of curses.KEY_ENTER, KEY_BACKSPACE?

curses.KEY_ENTER and curses.KEY_BACKSPACE have values that stdscr.getch never seems to return, so what's the point of them? Should my code even test for them?

Why do

c = stdscr.getch()
if c in (curses.KEY_BACKSPACE, curses.ascii.DEL):
    ...

When stdscr.getch() can never have the value of curses.KEY_BACKSPACE?

I've read that the curses.KEY_* codes are useful for special keys that may not have ASCII values, like KEY_UP etc., but is there any point to checking curses.KEY_* if you are testing for ascii keys?

Upvotes: 1

Views: 76

Answers (1)

Thomas Dickey
Thomas Dickey

Reputation: 54505

OP did not provide a complete code example. Where the python documentation is incomplete or vague, try the ncurses manpages. Generally speaking, the KEY_xxx symbols in curses are enabled by the keypad function (see Keypad Mode):

The NOTES section in the same manpage says

Some key strokes are indistinguishable from control characters; for example, KEY_ENTER may be the same as ^M, and KEY_BACKSPACE may be the same as ^H or ^?. Consult the terminfo entry for the terminal type to determine whether this is the case; see infocmp(1). Some curses implementations, including ncurses, honor the terminfo key definitions; others treat such control characters specially.

and

curses distinguishes the Enter keys in the alphabetic and numeric keypad sections of a keyboard because (most) terminals do. KEY_ENTER refers to the key on the numeric keypad and, like other function keys, is reliably recognized only if the window's keypad mode is enabled.

Upvotes: 0

Related Questions