Access Keystrokes in C

前端 未结 2 777
傲寒
傲寒 2020-12-21 23:03

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 some

相关标签:
2条回答
  • 2020-12-21 23:08

    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.

    0 讨论(0)
  • 2020-12-21 23:30

    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...

    0 讨论(0)
提交回复
热议问题