Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Capturing keystrokes under most platforms requires you to access the console directly. Typically there are libraries available to help you with that. Low-level libraries are termcap (which is derived from terminal capabilities) libraries. There is a "portable" layer on top of termcap called curses. Linux uses the GNU version which is called ncurses and is actually available on a multitude of platforms.</p> <p>Curses is extensively documented, you can start a tutorial using </p> <pre><code>$ man ncurses </code></pre> <p>Your problem requires steps to initialize the console and set up keystroke reader. There are several ways to achieve your desired effect. </p> <p>I am posting a working example for you to play with. It shows some basic ideas in curses:</p> <pre><code>/* file: curses_ex1.c */ #include &lt;stdio.h&gt; #include &lt;curses.h&gt; const char rotary[4] = "/-\\|"; int main() { WINDOW *w; int i = 0; w = initscr(); if ( w == NULL ) { return -1; /* unable to initialize curses */ } timeout(500); /* do not block */ mvprintw(0, 0, "Press space bar to break out of the loop."); noecho(); for(i = 0; ; i++) { /* no condition so loops forever */ int c; mvaddch(0, 42, rotary[i%4]); /* display rotator */ c = getch(); /* get a character */ if ( c == ' ') break; if ( c != ERR ) { /* not a space but another valid key */ mvprintw(1, 0, "You need to press a space for me to stop (you pressed `%c')", c); } } endwin(); return 0; } </code></pre> <p>To compile it:</p> <pre><code>cc -o curses_ex1 curses_ex1.c -lcurses </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload