Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

How to get character from keyboard buffer without program pause

mhnCF

New Coder
I need a function that immediately returns either a character existing in the keyboard buffer or a -1
This so I can qualify input before it is echo-ed to the console display.
I found this code but it mysteriously echos characters to the console and I can't figure out why:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>

int main() {
    int input;
    int flags = fcntl(STDIN_FILENO, F_GETFL, 0); // Get the current file status flags

    if (flags == -1) {
        perror("fcntl");
        return 1;
    }

    // Set the non-blocking flag
   flags |= O_NONBLOCK;

    if (fcntl(STDIN_FILENO, F_SETFL, flags) == -1) {
        perror("fcntl");
        return 1;
    }

    while (1) {
        char c;
        int bytesRead = read(STDIN_FILENO, &c, 1); // Try to read one character

        if (bytesRead == 1) {
           printf("Character in buffer: %c\n", c);
        }

        // Your other code here (non-blocking)
         input = getchar();  // Read a single character and do nothing with it, however, It still gets "Printed"
        
    }

    return 0;
}
Thanks for your help
 
C:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>

// Returns: -1 if no character available, otherwise returns the character
int kbhit(void) {
    struct termios oldt, newt;
    int ch;
    int oldf;
    
    // Save old terminal settings
    tcgetattr(STDIN_FILENO, &oldt);
    
    // Set new terminal settings for non-blocking, non-echoing input
    newt = oldt;
    newt.c_lflag &= ~(ICANON | ECHO);
    tcsetattr(STDIN_FILENO, TCSANOW, &newt);
    
    // Set non-blocking mode
    oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
    fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);

    // Try to read a character
    ch = getchar();

    // Restore old terminal settings
    tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
    fcntl(STDIN_FILENO, F_SETFL, oldf);

    // Return the result
    if(ch != EOF) {
        return ch;
    }
    return -1;
}

// Example usage:
int main() {
    printf("Press keys (press 'q' to quit)...\n");
    
    while(1) {
        int c = kbhit();
        if(c != -1) {
            printf("Got character: %c\n", c);
            if(c == 'q') break;
        }
        // Your other code can go here
        usleep(10000);  // Small delay to prevent CPU hogging
    }
    
    return 0;
}
 

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom