3

I'm trying to make a game that continues running until a key is pressed and then it should take that key in and do something with it then continue running as per normal. How do I do this?

I'm on MAC so even though I've come across a windows library called conio.h which can handle this using kbhit() and getch(), I can't get it working for me...

//
//  main.c
//  conioTesting
//
//

#include <stdio.h>
#include "myconio_mac.h"

int main(int argc, const char * argv[]) {

    int counter = 0;

    while (counter < 2) {
        if (kbhit()) {
            char key = getch();
            printf("\n Key is %c \n", key);
            printf("Keyboard hit detected \n");
        } else {
            printf("Nothing. \n");
        }
    }
    printf("Passed!!!!!!!!!! \n");
}
Ikki
  • 33
  • 1
  • 5

2 Answers2

2

On the MAC, you need to fiddle with the terminal settings to turn off line buffering. (You can also turn off echo.) Once the terminal is setup correctly, you can use read to get single characters from the keyboard.

In the sample code below, the kbsetup function takes care of the terminal settings. The getkey function checks for a key press, and returns the key if any, or '\0' if no key was read. The main function has a loop that prints the time once per second, and prints any key that the user presses. Press 'q' to exit the program.

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>
#include <termios.h>
#include <unistd.h>

static struct termios oldSettings;

void kbcleanup( void )
{
    tcsetattr( 0, TCSAFLUSH, &oldSettings );     /* restore old settings */
}

void kbsetup( void )
{
    tcgetattr( 0, &oldSettings );

    struct termios newSettings = oldSettings;

    newSettings.c_lflag &= ~ICANON;   /* disable line-at-a-time input */
    newSettings.c_lflag &= ~ECHO;     /* disable echo */
    newSettings.c_cc[VMIN]  = 0;      /* don't wait for characters */
    newSettings.c_cc[VTIME] = 0;      /* no minimum wait time */

    if ( tcsetattr( 0, TCSAFLUSH, &newSettings ) == 0 ){
        atexit( kbcleanup );    /* restore the terminal settings when the program exits */
    } else {
        fprintf( stderr, "Unable to set terminal mode\n" );
        exit( 1 );
    }
}

int getkey( void )
{
    char c;

    if ( read( STDIN_FILENO, &c, 1 ) == 0 )
        return '\0';
    else
        return c;
}

int main( void )
{
    int c;

    kbsetup();

    time_t start = time( NULL );
    time_t previous = start;
    for (;;)
    {
        usleep( 1000 );
        time_t current = time( NULL );

        if ( current != previous )
        {
            fprintf( stderr, "tick %3ld\r", current - start );
            previous = current;
        }
        else if ( (c = getkey()) != '\0' )
        {
            if ( c == 'q' || c == 'Q' )
                break;
            printf( "\ngot char: 0x%02x", c );
            if ( isprint( c ) )
                printf( " '%c'", c );
            printf( "\n" );
        }
    }
}
user3386109
  • 34,287
  • 7
  • 49
  • 68
  • Great!! I used this code and it seems to work :) What exactly are the disadvantages for this, I imagine turning off line buffering / one at a time would result in some form of loss in functionality? – Ikki Apr 19 '15 at 00:05
  • @Ikki: It tends to be slower because you end up making more system calls, etc. You can find out about [Canonical vs non-canonical terminal input](http://stackoverflow.com/questions/358342/canonical-vs-non-canonical-terminal-input/) on SO, of course. – Jonathan Leffler Apr 19 '15 at 01:05
  • @ikki I'm glad you asked since it forced me to think about the disadvantages. The link from Jonathan discusses one disadvantage, which is that the line editing features are disabled. Another disadvantage is that constantly polling the keyboard puts one of the cpu cores at 100% (as shown by the Activity Monitor). Adding a `usleep(1000)` to the code ([see my edit](http://stackoverflow.com/posts/29723614/revisions)) fixes that. The millisecond delay is not noticeable to the user, but brings the cpu usage down to 1.5%. If your game already has some built-in delays, the usleep may not be needed. – user3386109 Apr 19 '15 at 05:18
2

Sounds like you want to wait for a key to be pressed and then continue execution:

//test.c

#include <pthread.h>
#include <stdio.h>

void *input_listener(void *threadarg)
{
  getchar();
  printf("A key was pressed.\n");
}

int main()
{
  printf("Start\n");
  pthread_t thread;
  pthread_create(&thread, NULL, input_listener, NULL);
  pthread_join(thread, NULL);

  // Continue main
}

Should be very simple to do with pthreads (need to compile: gcc test.c -lpthread).

Vinay Dandekar
  • 366
  • 3
  • 9