3

I have a C++ application running on Ubuntu that waits for a key press using getchar() function. The input is not used, I just end my program with any keypress.

I noticed I could type basically any printable character, but also some unprintable characters such as the escape and backspace keys. Keys that did not trigger the ending of my program were keys such as shift and ctrl.

So my question is, which key presses get read by getchar()? How can I classify those, since apparantly not only printable characters trigger getchar()?

I could not find the answer in the man page or the C++ reference.

This is basically all the code I wrote:

std::cout << "Ready for input ..." << std::endl;
std::getchar();
std::cout << "A key was pressed, landing ..." << std::endl;

Mind that I do manipulate termios to not wait for a newline character and to not echo.

Marthe Veldhuis
  • 316
  • 2
  • 16

2 Answers2

1

So it turns out the keys that getchar() does not catch are Modifier keys. Maybe just because they are simply "modifiers" and always are used together with another key to create a specific charater. If anyone knows the particular reason, I would like to know.

Marthe Veldhuis
  • 316
  • 2
  • 16
  • That's a bit too simple. Is '\`' a modifier key? On my keyboard, the answer varies depending on the language I'm using. When I'm writing code or English, the answer is no. When I use languages that have the letter à, I use ` as a modifier. – MSalters Apr 26 '18 at 15:02
  • 1
    @MSalters In the references I saw, modifier keys are defined as not triggering an action by themselves, just entering a sort of [mode](https://en.wikipedia.org/wiki/Mode_(computer_interface)#Quasimodes). So I do not think '`' is a modifier key, you kinda proved it by typing it? Wikipedia calls those [dead keys](https://en.wikipedia.org/wiki/Dead_key) apparently, since you have to press and release them before they modify the next character typed. – Marthe Veldhuis Apr 27 '18 at 21:28
  • I type ` by typing a space afterwards. So yes, that's two keys for one character. – MSalters Apr 27 '18 at 22:58
  • 1
    So again that is a "dead key" then, and not a "modifier key" since you need to press and release them before they alter te next character instead of just the press. – Marthe Veldhuis Apr 28 '18 at 11:33
-1

getchar reads from stdin, not directly from the keyboard. The bit that you're missing is hinted at by your assumption of "keyboard characters". That's missing the translation from key presses to characters. For instance, the key presses Shift+a produces A. How you enter à depends on the xact keyboard configuration.

MSalters
  • 173,980
  • 10
  • 155
  • 350