0

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.

rckoenes
  • 69,092
  • 8
  • 134
  • 166
torhoehn
  • 95
  • 17
  • You want catch the arrow keys in Xcode or in your app? – rckoenes Nov 18 '14 at 13:49
  • I have to use the input of an arrow key in a method. – torhoehn Nov 18 '14 at 13:50
  • Then it has nothing to do with Xcode. Is it a command line app or are you using a UI via Cocoa? – rckoenes Nov 18 '14 at 13:51
  • A C++ command line app. I thought it have something to do with Xcode, because the stuff I found only works on Windows. – torhoehn Nov 18 '14 at 13:52
  • Xcode is just an IDE, so most time it will have nothing to with it. You should look at some UNIX example on how to read out the keypresses and catch the arrow keys. – rckoenes Nov 18 '14 at 13:58
  • There is no portable way. (In fact, there's not even a notion of a keyabord with arrow keys in C++). What OS do you need this for? – MSalters Nov 18 '14 at 14:14
  • You can hook the keyboard and directly screen the keys pressed which is basically the same behavior as a keylogger. – deW1 Nov 18 '14 at 14:16

2 Answers2

1

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.

JustAnotherCurious
  • 2,208
  • 15
  • 32
  • 1
    I would never have imagined that programs like `vi` and the good old `vt-100` terminal could be described as *modern* in 21th century :-) – Serge Ballesta Nov 18 '14 at 14:51
1

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.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252