LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   read key from keybord in c (https://www.linuxquestions.org/questions/programming-9/read-key-from-keybord-in-c-150067/)

arthur_mueller 02-24-2004 03:57 PM

read key from keybord in c
 
Hallo,

is there any ansi-c-function, that just read one char from keyboard and returns this value? Functions I know are getc(), fgetc() and getchar(); but all this functions read from stdin until carry return and sends every char to stdout like scanf(). I need function, that do following:

- waits until one key is pressed,
- reads value of this key from stdin,
- does not output this char to stdout,
- returns ascii value,

So I need something like PASCAL's readkey();


Thanks for help :)

itsme86 02-24-2004 03:59 PM

You have to change the terminal to non-buffered I/O. You can do this with the termios() function. The man page might not give you enough details but there's a ton of sample code doing it if you do a google search search on "non-buffered termios".

bigearsbilly 03-11-2004 10:41 AM

there's something like it in
the ncurses library i think.


billy

ifdef 03-11-2004 04:33 PM

Hi,

this might work:

Code:

/* readchar.c
 * Written by René Schleicher <rs@bofh.name>
 */
#include <termios.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>

int
main (void)
{
  char c;
  struct termios new_kbd_mode;
  struct termios g_old_kbd_mode;

  /* put keyboard (stdin) in raw, unbuffered mode */
  tcgetattr (0, &g_old_kbd_mode);
  memcpy (&new_kbd_mode, &g_old_kbd_mode, sizeof (struct termios));

  new_kbd_mode.c_lflag &= ~(ICANON | ECHO);
  new_kbd_mode.c_cc[VTIME] = 0;
  new_kbd_mode.c_cc[VMIN] = 1;
  tcsetattr (0, TCSANOW, &new_kbd_mode);


  if (read (0, &c, 1) != 1)
    {
      fprintf (stderr, "read() failed\n");
      _exit (-1);
    }

  /* set back into old mode */
  tcsetattr (0, TCSANOW, &g_old_kbd_mode);

  printf ("-%c-\n", c);

  return 0;
}


cjp 03-12-2004 03:18 AM

How I did it
 
I used the following code in an application:
Code:

#include <stdio.h>
#include <unistd.h>
#include <termios.h>
#include <sys/ioctl.h>

struct termio original_termio;

void enterInputMode()
{
        //Settings for stdin (source: svgalib):
        int fd = fileno(stdin);
        struct termio zap;
        ioctl(fd, TCGETA, &original_termio);
        zap = original_termio;
        zap.c_cc[VMIN] = 0;
        zap.c_cc[VTIME] = 0;
        zap.c_lflag = 0;
        ioctl(fd, TCSETA, &zap);
}

void leaveInputMode()
{
        //Restore original stdin
        int fd = fileno(stdin);
        ioctl(fd, TCSETA, &original_termio);
}

char readKey()
{
        int fd = fileno(stdin);

        char c = '\0';
        int e = read(fd, &c, 1);
        if (e == 0) c = '\0';

        return c;
}

Call enterInputMode() before doing any readKey() calls, and leaveInputMode() after you're finished, to leave the terminal as bash etc. expect it to be. readKey returns '\0' if there's no key pressed. To wait for a key, do something like this:

Code:

char c = '\n'; //Just anything not equal to '\0'
while(c != '\0') c = readKey();

Special keys, like the arrow keys or the function keys, return "escape sequences". You can use the cat program without arguments to discover these. To scan them, you should do several readKey calls to check for escape sequences whenever the escape character is detected. Most terminal programs (xterm, konsole, PuTTY etc.) generate the same sequences, but to do things correctly you should read the termcap database. I never did this, but if you really want to do it, then it might be easier to use the ncurses library, which does everything for you.

gr33ndata 03-12-2004 06:57 PM

What about kbhit()
Is there something like this or i am wrong ??

Hko 03-13-2004 05:18 AM

That a DOS thing from the Borland compilers.
See these forum threads on how make a function like kbhit() for Linux:

http://www.linuxquestions.org/questi...archid=1682323


All times are GMT -5. The time now is 02:13 PM.