Key presses during other actions

This is a discussion on Key presses during other actions within the C++ Programming forums, part of the General Programming Boards category; Okay, is there a way to perform an action while another action is going on. In my case, I want ...

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    94

    Key presses during other actions

    Okay, is there a way to perform an action while another action is going on. In my case, I want something to animate at all times, but also allow the user to use the arrow keys. I know this is possible, I saw it done in a text-based Pong game. Would I use the function I noticed in the code, "kbhit()"? Thanks for any help!

    Brendan
    Draco dormiens nunquam titillandus

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,670
    The only way to do that is with more than one thread of execution. There is no other way to check for a key-press midstream in the flow of execution, if you know what i mean.



    ITSA
    Socket Library!

  3. #3
    Registered User fletch's Avatar
    Join Date
    Jul 2002
    Posts
    176
    You can kinda fake the multiple thread thing...
    Code:
    do
    {
    	Option = Keypress();	
    	
    	switch(Option)
    	{
    	case 'u': MovePaddle(Up); break;
    	case 'd': MovePaddle(Down); break;
    	}
    	
    	MoveBall();
    	DrawScreen();
    } while (Option != 'x');
    If you see what I mean. Just make sure that none of the functions in the loop take too long.
    "Logic is the art of going wrong with confidence."
    Morris Kline

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    94
    Ha thanks everyone, but I've found that "kbhit()" function works wonderfully (though I believe it is compiler specific)! Thanks again!

    Brendan
    Draco dormiens nunquam titillandus

  5. #5
    Registered User
    Join Date
    Jul 2002
    Posts
    94
    This is sorta what I'm doing (it's for "Mists of Avalon"). I'm making it so the villagers can walk around and the player can walk around at the same time. That was the point of it.

    Code:
    for(;;)
    {
         if (kbhit())
         {
              // movement code
         }
         else
         {
              // all other code
         }
    }
    See, that's what I'm sorta doing. Thanks for the help!

    Brendan
    Draco dormiens nunquam titillandus

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 11-23-2007, 12:48 PM
  2. Virtual keys
    By Arkanos in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2005, 09:00 AM
  3. Directional Keys - Useing in Console
    By RoD in forum C++ Programming
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM
  4. FAQ: Directional Keys - Useing in Console
    By RoD in forum FAQ Board
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM

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