Thread: kbhit() for enter key? not working

  1. #1
    Registered User
    Join Date
    Nov 2005

    kbhit() for enter key? not working

    I'm trying to make a loop to build a string while the program is running. When the enter key is hit I want to process the string. Basically I want gets() without blocking. So I'm trying to use kbhit() to do this.

    PHP Code:
                if (kbhit())
    str[stri] = getch();
    cout << str[stri];

                        if (
    str[stri] == '\n')
    //rakClientInterface->RPC("PrintMessage", str, (strlen(str)+1)*8, HIGH_PRIORITY, RELIABLE, 0, false);


    stri 0;
    str[0] = '\0';

  2. #2
    Banal internet user
    Join Date
    Aug 2002
    Is this a win32 console program? Because if it is, don't try to use kbhit().

    Here's an alternative:
    // Waits for keypress event and returns that key (virtual-key code)
    WORD GetKeypress()
    	HANDLE hStdIn = GetStdHandle(STD_INPUT_HANDLE);
    	INPUT_RECORD irEvent;
    	DWORD dwNumEventsRead;
    	WORD wReturn;
    	for (;;)
    		WaitForSingleObject(hStdIn, INFINITE);
    		PeekConsoleInput(hStdIn, &irEvent, 1, &dwNumEventsRead);
    		if ((irEvent.EventType == KEY_EVENT) && (irEvent.Event.KeyEvent.bKeyDown))
    			ReadConsoleInput(hStdIn, &irEvent, 1, &dwNumEventsRead);
    			wReturn = irEvent.Event.KeyEvent.wVirtualKeyCode;
    			return wReturn;
    			ReadConsoleInput(hStdIn, &irEvent, 1, &dwNumEventsRead);
    And if you're not implementing a Windows console program, then sorry.
    Last edited by BMJ; 04-02-2006 at 04:17 PM.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Its a console aplication, not a win32. I commented the raknet function and put in system("PAUSE") just to see if it was working, but its not executing system("PAUSE") I don't think. Its not outputing Press any key to continue . . . and when I hit enter it doesn't terminate the program.

    Can anyone tell me what I've done wrong and a solution to fix it?

    If not can anyone tell me a good place to learn windows threads?

  4. #4
    Been here, done that.
    Join Date
    May 2003
    Try outputting str[stri] in hex as you read it with getch(). You'll probably find, as I did, you are checking for the wrong character in your IF.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Which compiler are you using?
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  2. "press any key to continue" without the enter key
    By chickenandfries in forum C Programming
    Replies: 1
    Last Post: 03-29-2008, 09:56 PM
  3. Replies: 8
    Last Post: 01-18-2008, 04:06 AM
  4. key problem
    By madsmile in forum Game Programming
    Replies: 20
    Last Post: 06-05-2002, 10:57 PM
  5. Pausing for input, with kbhit() still detecting keys
    By Da-Spit in forum C++ Programming
    Replies: 5
    Last Post: 05-23-2002, 05:04 AM