How do I do this?I want a terminal menu where shift+up or shift+down selects multiple lines.ncurses seems to have problems.I don't necessarily need an advanced terminal library.I just want to handle this and other than that my IO should be pretty straightforward.Any advice?
Detecting shift + keyup/down in the terminal
terminal;tty;ncurses
Terminals transmit characters, not keys. Most characters are printable, which doesn't leave much room to encode function keys and keychords. Function keys are encoded as escape sequences, i.e. sequences of bytes starting with the escape character (byte value 27, which can be written out as \e in many programming languages). For more details, see How do keyboard input and text output work?There's no universal standard on what escape sequence each key or keychord sends. The curses library provides an abstraction layer that decodes function keys. It uses the termcap (old-style) or terminfo (modern) library under the hood; you can use termcap/terminfo directly if you don't want to link with curses.Alternatively, you can hard-code the usual control sequences for Up and Down. While there is no standard, almost every terminal sends either \eOA or \e[A for Up and either \eOB or \e[B for Down, and I've never seen a terminal that sent these sequences for a different key.The Shift keychords are another matter: the escape sequences they send are more diverse, and many terminals don't distinguish e.g. Up from Shift+Up. You can't rely on these being distinct unless you only support a restricted set of configurations. There are emerging standards but they're still not supported by many popular terminal emulators.Terminals don't send key up events. (Your use case doesn't require them, though, according to your description.)Unless you can afford to require a specific terminal emulator (e.g. recent-ish xterm), don't rely on the user being able to type Shift+Up. Support an alternate method, such as a start multiple selection key followed by plain Up/Down.