Thread: Is there an inkey() in C

  1. #1
    zach
    Guest

    Is there an inkey() in C

    Is it possible to write in c what I have paraphrased below?

    while inkey != char(32) continue with operation A, else stop and do operation B

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    It depends on your OS and Compiler.

    If you're looking for something to test if there has been a key press, not to wait if there hasn't, and not to wait for a newline.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    zach
    Guest
    Checking whether there has been a keypress is not the issue.

  4. #4
    zach
    Guest
    I am looking for a similar statement as inkey , the equivalent of which statement doesn't appear to be in C.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Since, you will not tell us what inkey means to you there is no way to help you!

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  6. #6
    Registered User
    Join Date
    Dec 2017
    Posts
    1,626
    It's a truly stupid question.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  7. #7
    Registered User
    Join Date
    May 2019
    Posts
    214
    The function of interest isn't in C because of the nature of C. It was originally purpose built to write the UNIX operating system while translating the assembler source into a language operating near the assembler level without no CPU dependencies. As such, C doesn't really address application level interests beyond files and i/o as a language or standard library.

    Instead, it relies upon third party libraries, and in your example case one that spans a diverse range of hardware. If the concept of inkey is to examine what current keyboard button is being held down, that is rather specific to a device like the PC. The original C viewed input as a specialized type of device opened as a file, which you know as stdin (or stdout for output), and doesn't function in a related way. Where, in the console, a keystroke is data sent through a serial connection, and is therefore a keystroke result, on a PC the notion is far more interesting, related to the fact that the keyboard hardware is more closely tied to the computer the software is running on. Most specifically, there are ways to detect combinations of keys being held, when they are pressed and when they are released. That can't happen using the stdin concept standardized for C.

    Students tend to think of this as a shortsighted design of a language, if it misses some feature out of the box they're interested in, but factually that's inverted. C offers much deeper access to the hardware, and there are likely many libraries which could provide what you want, or you could make one.

    A lot depends on the target operating system. The keyboard is a single resource controlled by the OS in combination with other concerns, like window focus and mouse I/O. Windows has much different support for that hardware than *Nix systems do.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by zach
    Checking whether there has been a keypress is not the issue.

    I am looking for a similar statement as inkey , the equivalent of which statement doesn't appear to be in C.
    I agree with stahta01: you need to state what exactly is this inkey thing, and not just assume that C programmers will know what you're talking about because you saw it from another programming language. Like Salem, I assumed that you had reading a keypress in mind, but you explicitly repudiated that, so what on earth is inkey? A quick search of the web for "inkey programming" brings up a QBasic "INKEY$ command, which checks to see if a user typed a key and provides the keypress to the program", but that's not what you're thinking of since you wrote that "Checking whether there has been a keypress is not the issue".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Nov 2018
    Location
    Amberg in upper palatinate, Bavaria
    Posts
    66

    usein kbhit() in c instead of inkey in qbasic

    Hallo zach!
    May be if you work with Linux, you can use kbhit() from this example:
    Code:
    #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 main(void)
    {
      int letter= 0;
    
     while(letter == 0)
      {
       letter = kbhit();
        printf("Press a key: %d\n", letter);
      }
      printf("You pressed '%c'!\n", getchar());
      return 0;
    }
    or this:
    Code:
    #ifndef GETCH_H_INCLUDED
    #define GETCH_H_INCLUDED
    
    #include <stdio.h>
    #include <termios.h>
    #include <unistd.h>
    #include <fcntl.h> //for new version of kbhit
    //#include <sys/time.h> old version
    
    /// reads from keypress, doesn't echo
    int getch(){
        struct termios oldt, newt;
        int ch;
        tcgetattr( STDIN_FILENO, &oldt );
        newt = oldt;
        newt.c_lflag &= ~( ICANON | ECHO );
        tcsetattr( STDIN_FILENO, TCSANOW, &newt );
        ch = getchar();
        tcsetattr( STDIN_FILENO, TCSANOW, &oldt );
        return ch;
    }
    
    void clpuf(void)
     {
     while (getc(stdin) != '\n')
        ;
     }
    
       /// reads from keypress, echoes
    int getche(void)
        {
        struct termios oldattr, newattr;
        int ch;
        tcgetattr( STDIN_FILENO, &oldattr );
        newattr = oldattr;
        newattr.c_lflag &= ~( ICANON );
        tcsetattr( STDIN_FILENO, TCSANOW, &newattr );
        ch = getchar();
        tcsetattr( STDIN_FILENO, TCSANOW, &oldattr );
        return ch;
        }
    /*old: 
    
    long kbhit(void)
    {
    struct timeval tv;
    fd_set read_fd;
    
    tv.tv_sec=0;
    tv.tv_usec=0;
    FD_ZERO(&read_fd);
    FD_SET(0,&read_fd);
    
    if(select(1, &read_fd, NULL, NULL, &tv) == -1)
    return 0;
    
    if(FD_ISSET(0,&read_fd))
     return 1;
    
    return 0;
    } 
    */
    
    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;
    }
    #endif // GETCH_H_INCLUDED
    Sorry here is a source not a real header.
    But i think as an example it is okay

  10. #10
    zach
    Guest

    inkey

    this is the idea
    Code:
     do {something while inkey != space bar}

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Registered User
    Join Date
    Nov 2018
    Location
    Amberg in upper palatinate, Bavaria
    Posts
    66
    With two little changes
    of the last examples:
    Code:
    #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 ch;
      }
     
      return 0;
    }
     
    void clpuf(void)
     {
     while (getc(stdin) != '\n')
        ;
     } 
     
    
    int main(void)
    {
      int letter= 0;
    
    while((letter = kbhit()) != 32)
      {
       //if (letter != 0) clpuf();
        printf("Press a key: %d\n", letter);
       }
       printf("You pressed '%c'= %d decimal\n", letter, letter);
     
     return 0;
    }
    you have a similar effect.
    But i think on modern fast Systems it can be a risk to use a substitute for kbhit() like here.
    For terminals like in Linux such a code is almost outdated.

  13. #13
    zach
    Guest
    Quote Originally Posted by Niccolo View Post
    The function of interest isn't in C because of the nature of C. It was originally purpose built to write the UNIX operating system while translating the assembler source into a language operating near the assembler level without no CPU dependencies. As such, C doesn't really address application level interests beyond files and i/o as a language or standard library.

    Instead, it relies upon third party libraries, and in your example case one that spans a diverse range of hardware. If the concept of inkey is to examine what current keyboard button is being held down, that is rather specific to a device like the PC. The original C viewed input as a specialized type of device opened as a file, which you know as stdin (or stdout for output), and doesn't function in a related way. Where, in the console, a keystroke is data sent through a serial connection, and is therefore a keystroke result, on a PC the notion is far more interesting, related to the fact that the keyboard hardware is more closely tied to the computer the software is running on. Most specifically, there are ways to detect combinations of keys being held, when they are pressed and when they are released. That can't happen using the stdin concept standardized for C.

    Students tend to think of this as a shortsighted design of a language, if it misses some feature out of the box they're interested in, but factually that's inverted. C offers much deeper access to the hardware, and there are likely many libraries which could provide what you want, or you could make one.

    A lot depends on the target operating system. The keyboard is a single resource controlled by the OS in combination with other concerns, like window focus and mouse I/O. Windows has much different support for that hardware than *Nix systems do.
    A student of C (even the very old student that I am) cannot get enough of the sort of texts that you have written above. Thank you! Zach Kleinstein.

  14. #14
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    338
    what inkey means to you
    Inkey is a quickbasic function.
    "without goto we would be wtf'd"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inkey$ in C++?
    By reefa in forum C++ Programming
    Replies: 5
    Last Post: 10-03-2005, 10:01 AM
  2. For those who know Qbasic:inkey$ in c++
    By Ivan! in forum C++ Programming
    Replies: 4
    Last Post: 01-27-2003, 09:25 AM

Tags for this Thread