1

I am trying to access keystrokes in C. I can access alphanumeric keys. How can I access Control, Shift and Alt key?
Plus I read somewhere that sometimes while entering text in console, OS masks backspace key. I would like to know where user pressed backspace key. It's not same as knowing when '\n' was pressed. GNU C. Ubuntu 11.

rsjaffe
  • 5,600
  • 7
  • 27
  • 39
  • 5
    Use the ncurses library. – Dietrich Epp Nov 26 '11 at 05:03
  • Possible duplicate of [Detect meta (special) keys (Ctrl, Shift, Alt, Tab, Esc, Backspace) from Shell Input](http://stackoverflow.com/questions/8242404/detect-meta-special-keys-ctrl-shift-alt-tab-esc-backspace-from-shell-in) – Thomas Dickey Apr 02 '17 at 00:40

2 Answers2

2

Dietrich Epp answered in a comment: use ncurses library.

See also this question

And you might make an X11 client graphical application; in that case use a graphical toolkit library like GTK or Qt

If you want to make a console application, use ncurses or perhaps readline

And your question, when taken literally, has no sense: the strict C standard don't know what a key or a keystroke is (the only I/O operations mentioned in the standard are related to <stdio.h> thru FILE). This is why most people uses additional libraries and standards (in addition of those required by ISO C), eg. Posix...

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
1

The simple answer is "you can't", at least not easily or without downloading third party libraries.

Most C programs shouldn't have to know anything about the keyboard or the screen. Standard C is only concerned with reading from and writing to files (the keyboard and screen being special-case files).

Assuming you have a good reason for wanting to access the keyboard directly, you should be looking at the ncurses library (http://www.gnu.org/software/ncurses/ncurses.html). Ncurses knows how many different (virtual) terminals and keyboards work, and it presents a uniform interface to them. It lets you paint the screen and create a substitute graphical interface using only blocks of text.

Since you use Ubuntu, try running the "aptitude" command to see a good example of what ncurses can do.

jforberg
  • 6,537
  • 3
  • 29
  • 47