views:

1093

answers:

2
while(ch != 'q') 
{
  printf("looping\n");
  sleep(1);
  if(kbhit()) 
   {
    ch = readch();
    printf("you hit %c\n",ch);
   }
}

This code gives me a blocking getch() like functionality. I am trying to use this code to capture up down arrow keys.

Added: Trying to capture key codes of up arrow gives me 3 chars 27, 91 and 65. Using if/else I am trying pattern matching but I only get 2 chars. Next one is captured when next key is pressed.

I want to capture full words using getchar() while always looking for certain keys all the time(esc, del etc.).

A: 

This example could help: raw mode demonstration (backspace to stop the demo)

RC
I have seen this code and My code gives me similar codes. But how do i do the pattern matching with three codes for UP key. My waits for the next key to be pressed.
Alex Xander
modifying the example to: char pp = 0; char p = 0; while( (i = read(0, if (c == 0177) /* ASCII DELETE */ break; printf( "%o, %o, %o\t%s\n\r", pp, p, c, pp = p; p = c; }works on my linux box, but I would advise curse if you can use it (see http://www.linuxselfhelp.com/HOWTO/NCURSES-Programming-HOWTO/keys.html)
RC
This makes use of read(), after which I can not use getchar(). I am using getchar() to capture full word. Also looking for certain keys all the time(esc, del etc.)
Alex Xander
+2  A: 

I can't reproduce Your problem:

#include <unistd.h> 
#include <stdio.h> 
#include <ctype.h> 

#include "kbhit.h" /* http://linux-sxs.org/programming/kbhit.html */

int main(){
  init_keyboard();
  char ch='x';
  while( ch != 'q' ){
    printf("looping\n");
    sleep(1);
    if( kbhit() ){
      printf("you hit");
      do{
        ch = readch();
        printf(" '%c'(%i)", isprint(ch)?ch:'?', (int)ch );
      }while( kbhit() );
      puts("");
    }
  }
  close_keyboard();
}
sambowry