Continuous keyboard input in C

后端 未结 3 1498
猫巷女王i
猫巷女王i 2021-02-07 05:18

I am creating a console application in C. This is a game in which characters are falling down and user has to press that specific key on the keyboard. I don\'t know how to detec

3条回答
  •  被撕碎了的回忆
    2021-02-07 06:02

    There is a function called kbhit() or _kbhit it is in the library it returns true or false depending whether a key was hit. So you can go with something like this:

    while (1){
        if ( _kbhit() )
            key_code = _getch();
            // do stuff depending on key_code
        else 
            continue;
    

    Also use getch() or _getch which reads a character directly from the console and not from the buffer. You can read more about conio.h functions here they might be very useful for what you want to do.

    Note: conio.h is not a standard library and implementations may vary from compiler to compiler.

提交回复
热议问题