Reading arrow keys with C++

1.7k views Asked by At

I am writing an C++ Application and have to read if an arrow key is pressed or not. I only found some function that are only working on Windows.

2

There are 2 answers

0
Serge Ballesta On

As you say you only found material for Windows, I assume you are looking for a Linux-Unix way. Old dinosaurs like me remember the time when we only had true consoles (only a keyboard and a 80x25 display). And in these early times existed low-level libraries to interpret keypad transmitted keys and position cursor on screen on almost any terminal, and higher level ones to use the screen as a (text only) GUI.

You should look for curses or ncurses for high level libraries, and terminfo for the low-level capabilities.

1
JustAnotherCurious On

You have such problem because you just ask the wrong question. If you application is a command line tool and is accessible from a terminal, than it's just impossible to know which keys are pressed at the moment because the terminal can be far away from the machine where your application runs and which is more important, there is no reason for terminal to send you the arrow key presses because terminal can use them for text navigation.

So you may search how to make the terminal to send you key presses. Not every terminal will support it, but, I think, most of modern terminals in modern OS do.

If you has a gui application that is for running locally and assuming that you control it from the keyboard that is plugged in. Than you should search for the documentation for your gui toolkit. (Qt, wxWidgets, raw xorg, windows API, etc.)

So there are just no native C++ solution for this problem because you question just has no sense in many situations.

So you can use some console library like ncurses or gui toolkit like Qt or search for a native solution in your particular situation, but don't expect this last way will work without any additional code on other machines.

Or just search for other libraries that can allow you to do it.