如何在侦听C中的用户输入的同时运行程序? [英] How to run program whilst listening for user input in C?

查看:182
本文介绍了如何在侦听C中的用户输入的同时运行程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个可以继续运行直到按下某个键的游戏,然后它应该将其插入并对其进行操作,然后继续正常运行.我该怎么做?

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?

我正在使用MAC,因此即使遇到了一个名为conio.h的Windows库,该库可以使用kbhit()和getch()来处理此问题,但我无法使其正常工作...

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");
}

推荐答案

在MAC上,您需要弄弄终端设置以关闭行缓冲. (您也可以关闭echo.)正确设置终端后,可以使用read从键盘获取单个字符.

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.

在下面的示例代码中,kbsetup功能负责终端设置. getkey功能检查是否有按键按下,如果有按键,则返回按键;如果没有按键,则返回'\0'. main函数具有一个循环,该循环每秒打印一次时间,并打印用户按下的任何键.按'q'退出程序.

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" );
        }
    }
}

这篇关于如何在侦听C中的用户输入的同时运行程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆