Key Capture

4

I'm developing a game of C language snippet ( gcc 4.9.2 ) for the Ubuntu terminal 15.04 .

I need to capture a key typed by the user so he can change the direction in which the copper walks ( w up, s down, a left, d to the right).

For this, I'm using the following code snippet, inside a loop of repetition that moves the snake automatically while the user does not change the direction:

if(kbhit())
    seta = getchar();

seta is a variable of type char that stores the key you typed.

The program does read correctly, however, it also writes on the screen the captured character, which obviously can not happen in such a game.

Are there any C functions that catch the key without typing the character on the screen or any equivalent procedure?

Obs : Although kbhit() is a function originally from conio.h , I was able to set it for Linux and it is working perfectly.

    
asked by anonymous 30.06.2015 / 00:49

3 answers

2

Implement this function done by gdj :

#include <termios.h>  
#include <unistd.h>  
#include <errno.h>  
#define ECHOFLAGS (ECHO | ECHOE | ECHOK | ECHONL)  
int set_disp_mode(int fd,int option)  
{  
   int err;  
   struct termios term;  
   if(tcgetattr(fd,&term)==-1){  
     perror("Cannot get the attribution of the terminal");  
     return 1;  
   }  
   if(option)  
        term.c_lflag|=ECHOFLAGS;  
   else  
        term.c_lflag &=~ECHOFLAGS;  
   err=tcsetattr(fd,TCSAFLUSH,&term);  
   if(err==-1 && err==EINTR){  
        perror("Cannot set the attribution of the terminal");  
        return 1;  
   }  
   return 0;  
} 

And then time to call

while(1) {
    if (kbhit()) 
    {
        fflush(stdin);
        set_disp_mode(STDIN_FILENO,0); // 0 faz não exibir, se caso quiser voltar a exibir chame a função passando 1
        int c = getch();

        if (c == 65) // a
        { ...
    
30.06.2015 / 00:54
0

For Unix-based systems, you can use the header termios.h and fcntl.h to implement the kbhit and getch functions of Windows .

According to these pages 1 2 , this can be done as follows:

#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>

int kbhit(void)
{
  struct termios oldt, newt;
  int ch;
  int oldf;

  tcgetattr(STDIN_FILENO, &oldt);
  newt = oldt;
  newt.c_lflag &= ~(ICANON | ECHO);
  tcsetattr(STDIN_FILENO, TCSANOW, &newt);
  oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
  fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);

  ch = getchar();

  tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
  fcntl(STDIN_FILENO, F_SETFL, oldf);

  if(ch != EOF)
  {
    ungetc(ch, stdin);
    return 1;
  }

  return 0;
}

int getch(void)
{
    struct termios oldattr, newattr;
    int ch;
    tcgetattr( STDIN_FILENO, &oldattr );
    newattr = oldattr;
    newattr.c_lflag &= ~( ICANON | ECHO );
    tcsetattr( STDIN_FILENO, TCSANOW, &newattr );
    ch = getchar();
    tcsetattr( STDIN_FILENO, TCSANOW, &oldattr );
    return ch;
}

Alternatively, there is library ncurses , which provides function getch , still I do not know if the kbhit function is supported. To use it, you must include curses.h , if not installed, open a terminal and type:

sudo apt-get update && sudo apt-get install ncurses-dev
    
30.06.2015 / 03:22
0

The getch () function captures the user without displaying the response on the screen. Take a look at the github link:

link

Download the entire folder and run the 8-extras.c file and see how it works. The kbhitgetch.pdf file explains and has a good basis for understanding how it works from behind.

    
06.09.2017 / 21:04