Thread: Getting the char from kbhit()

  1. #1
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937

    Getting the char from kbhit()

    I need to be able to have a function that I can use like kbhit() that I can get the char out of. No getch(), please. I'm not trying to pause the program. In fact, that's exactly what I want to avoid.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  2. #2
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    522
    using getch with kbhit wont pause your program if you only call getch if kbhit returns true.
    Code:
    while( ! kbhit() )
    {
         // do stuff
    }
    
    ch = getch();
    
    // do other stuff
    This won't pause your program because getch will read the key which ended the loop and move on to the other stuff.
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  3. #3
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Smells good. Let me try it.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  4. #4
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    522
    Code:
    #include <conio.h>
    #include <iostream>
    
    using namespace std;
    
    int main( void )
    {
       char ch;
    	
       // Display message until key is pressed. 
       while( ! kbhit() )
          cout << "Hit me!! ";
    
       // get the key pressed
       ch = getch();
    
       cout << endl << "Key struck was " << ch;
    
       return 0;
    }
    Try that, took it of msvc help.
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  5. #5
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Ok. Thanx.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    If you want to use kbhit() / getch() actively in a game loop:
    Code:
    char Key=0;
    //Will loop until ESC is pressed
    while(Key!=27)
    {
      Key=0;
      if(kbhit()) Key=getch();
    
      //Prints text when ENTER is pressed
      if(Key==13) cout << "You pressed Enter" << endl;
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 07:36 AM
  3. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM
  4. Wierd Segmentation Faults on Global Variable
    By cbranje in forum C Programming
    Replies: 6
    Last Post: 02-19-2005, 12:25 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM