input without pause

This is a discussion on input without pause within the C Programming forums, part of the General Programming Boards category; I have a while loop that i want to keep going until the user presses enter, i have tried using ...

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    10

    Question input without pause

    I have a while loop that i want to keep going until the user presses enter, i have tried using
    while (getch()!=13)
    {
    commands
    }

    and other variations, but every command i know pauses the loop and waits for the input. Is there a way to keep the loop going while also waiting for the input?
    any help would be greatly appreciated.

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    the way I used to do it way back when I did games in the console was to use kbhit() which returns true if any key on the keyboard has been hit. Not sure if it's a standard function or not though, might have to look that up

    Code:
    int done=0;
     while(!done)
     {
     	if(kbhit())
     	{
     		int keypress=getch();
     		if(keypress==224)
     		{
     			int subkeypress=getch();
     // Process arrow keys, f1-f12 keys, etc
     		}
     	}
     // Process the rest of the program here
     }
    Last edited by jverkoey; 06-02-2004 at 06:31 AM.

  3. #3
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    oh, there's also asynchronous key getting using this function:

    GetAsyncKeyState

    It might be best to pair it with this macro, unless you're against macro use.
    Code:
       #define KEY_DOWN(vk_code)	((GetAsyncKeyState((vk_code))&0x8000 ? 1:0))
    and btw, you have to pass a VK_* code here, so you could do:
    if(KEY_DOWN(VK_RETURN))
    to test if the return/enter key has been pressed

    (defined in windows.h)

  4. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    10

    Thanx

    sweet. thanx for that, works like a dream.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. input redirection
    By sashaKap in forum C Programming
    Replies: 6
    Last Post: 06-25-2009, 01:59 AM
  2. For loop problems, input please.
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-22-2007, 03:54 AM
  3. I would love some input on my BST tree.
    By StevenGarcia in forum C++ Programming
    Replies: 4
    Last Post: 01-15-2007, 12:22 AM
  4. Simple Console Input for Beginners
    By jlou in forum C++ Programming
    Replies: 0
    Last Post: 06-21-2005, 01:50 PM
  5. Help with Input Checking
    By Derek in forum C Programming
    Replies: 7
    Last Post: 06-17-2003, 03:07 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21