Thread: Pausing for input, with kbhit() still detecting keys

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    99

    Unhappy Pausing for input, with kbhit() still detecting keys

    OK, I've been messing around with kbhit() and getch() and all that. But I've run into a problem. I got kbhit() to work, and the key detection works fine, thanks to you guys. But how would I let pause the program until the user presses a key and still let kbhit() detct it? Obviously your average pause wouldn't do, as kbhit() wouldn't detect it, and once the user presses a key it would continue, skipping through kbhit() before the user could press another key. I've tried Sleep() but it doesn't really work the way I want it to. Any help would be greatly appreciated.

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    99

    hmm

    Ok, I managed to solve the problem, but now I have another bug. Whenever I move up, and then move any other direction it will then move up again before I can move in the right direction, this happens every time. For example:

    Code:
    +---------------+
    |               |
    |               |
    |               |
    |       o >     |
    +---------------+
    
    o = player
    > = direction player moved last turn (loop, whatever)
    Say I had just moved right, if I went to move up it would continue to move right one more turn. I will post the code in the next post

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    99
    Code:
    #include <stdio.h>
    #include <windows.h>
    #include <stdlib.h>
    #include <process.h>
    #include <iostream.h>
    #include <conio_mingw.h>
    
    #define UP 72 
    #define DOWN 80 
    #define LEFT 75 
    #define RIGHT 77 
    #define ESCAPE 1
    
    int ShowBoard();
    char i[15][15] =
    {
     {'+', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '+'},
     {'|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|'},
     {'|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|'},
     {'|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|'},
     {'|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|'},
     {'|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|'},
     {'|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|'},
     {'|', ' ', ' ', ' ', ' ', ' ', ' ', 2, ' ', ' ', ' ', ' ', ' ', ' ', '|'},
     {'|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|'},
     {'|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|'},
     {'|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|'},
     {'|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|'},
     {'|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|'},
     {'|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|'},
     {'+', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '+'}
    };
    
    
    int main()
    {
    
    int update = 1;
    
    int x = 7, y = 7;
    
    
    
      system("CLS");
    
      Sleep(150);
      ShowBoard();
     // Sleep(100);
    
    
    
      do
      {
      i[x][y] = 2;
    
      if( kbhit() )
    		{
    			char keyCode = getch();
    			
    			// Left movement
    			if( keyCode == 75 )  //75 - 52
    			{
                    ShowBoard();
                    i[x][y] = ' ';
                    y--;
                    cout<<"Left!\n\n\n\n\n\n";
    			}
    			
    			// Down movement
    			else if( keyCode == 80 )  //80 - 50
    			{
                    ShowBoard();
                    i[x][y] = ' ';
                    x++;
                    cout<<"Down!\n\n\n\n\n\n";
    			}
    			
    			// Right movement
    			else if( keyCode == 77 )  //77 - 54
    			{
                    ShowBoard();
                    i[x][y] = ' ';
                    y++;
                    cout<<"Right!\n\n\n\n\n\n";
    			}
    			
    			// Up movement
    			else if( keyCode == 72 )   //72 - 56
    			{
    
                    ShowBoard();
                    i[x][y] = ' ';
                    x--;
                    cout<<"Up!\n\n\n\n\n\n";
    
    
                };
    
    
            };
    
    
    
       } while ( 1 );
    
    
       return 0;
    
    }
    
    int ShowBoard()
    {
    
      cout<<i[0][0]<<i[0][1]<<i[0][2]<<i[0][3]<<i[0][4]<<i[0][5]<<i[0][6]<<i[0][7]<<i[0][8]<<i[0][9]<<i[0][10]<<i[0][11]<<i[0][12]<<i[0][13]<<i[0][14]<<"\n";
      cout<<i[1][0]<<i[1][1]<<i[1][2]<<i[1][3]<<i[1][4]<<i[1][5]<<i[1][6]<<i[1][7]<<i[1][8]<<i[1][9]<<i[1][10]<<i[1][11]<<i[1][12]<<i[1][13]<<i[1][14]<<"\n";
      cout<<i[2][0]<<i[2][1]<<i[2][2]<<i[2][3]<<i[2][4]<<i[2][5]<<i[2][6]<<i[2][7]<<i[2][8]<<i[2][9]<<i[2][10]<<i[2][11]<<i[2][12]<<i[2][13]<<i[2][14]<<"\n";
      cout<<i[3][0]<<i[3][1]<<i[3][2]<<i[3][3]<<i[3][4]<<i[3][5]<<i[3][6]<<i[3][7]<<i[3][8]<<i[3][9]<<i[3][10]<<i[3][11]<<i[3][12]<<i[3][13]<<i[3][14]<<"\n";
      cout<<i[4][0]<<i[4][1]<<i[4][2]<<i[4][3]<<i[4][4]<<i[4][5]<<i[4][6]<<i[4][7]<<i[4][8]<<i[4][9]<<i[4][10]<<i[4][11]<<i[4][12]<<i[4][13]<<i[4][14]<<"\n";
      cout<<i[5][0]<<i[5][1]<<i[5][2]<<i[5][3]<<i[5][4]<<i[5][5]<<i[5][6]<<i[5][7]<<i[5][8]<<i[5][9]<<i[5][10]<<i[5][11]<<i[5][12]<<i[5][13]<<i[5][14]<<"\n";
      cout<<i[6][0]<<i[6][1]<<i[6][2]<<i[6][3]<<i[6][4]<<i[6][5]<<i[6][6]<<i[6][7]<<i[6][8]<<i[6][9]<<i[6][10]<<i[6][11]<<i[6][12]<<i[6][13]<<i[6][14]<<"\n";
      cout<<i[7][0]<<i[7][1]<<i[7][2]<<i[7][3]<<i[7][4]<<i[7][5]<<i[7][6]<<i[7][7]<<i[7][8]<<i[7][9]<<i[7][10]<<i[7][11]<<i[7][12]<<i[7][13]<<i[7][14]<<"\n";
      cout<<i[8][0]<<i[8][1]<<i[8][2]<<i[8][3]<<i[8][4]<<i[8][5]<<i[8][6]<<i[8][7]<<i[8][8]<<i[8][9]<<i[8][10]<<i[8][11]<<i[8][12]<<i[8][13]<<i[8][14]<<"\n";
      cout<<i[9][0]<<i[9][1]<<i[9][2]<<i[9][3]<<i[9][4]<<i[9][5]<<i[9][6]<<i[9][7]<<i[9][8]<<i[9][9]<<i[9][10]<<i[9][11]<<i[9][12]<<i[9][13]<<i[9][14]<<"\n";
      cout<<i[10][0]<<i[10][1]<<i[10][2]<<i[10][3]<<i[10][4]<<i[10][5]<<i[10][6]<<i[10][7]<<i[10][8]<<i[10][9]<<i[10][10]<<i[10][11]<<i[10][12]<<i[10][13]<<i[10][14]<<"\n";
      cout<<i[11][0]<<i[11][1]<<i[11][2]<<i[11][3]<<i[11][4]<<i[11][5]<<i[11][6]<<i[11][7]<<i[11][8]<<i[11][9]<<i[11][10]<<i[11][11]<<i[11][12]<<i[11][13]<<i[11][14]<<"\n";
      cout<<i[12][0]<<i[12][1]<<i[12][2]<<i[12][3]<<i[12][4]<<i[12][5]<<i[12][6]<<i[12][7]<<i[12][8]<<i[12][9]<<i[12][10]<<i[12][11]<<i[12][12]<<i[12][13]<<i[12][14]<<"\n";
      cout<<i[13][0]<<i[13][1]<<i[13][2]<<i[13][3]<<i[13][4]<<i[13][5]<<i[13][6]<<i[13][7]<<i[13][8]<<i[13][9]<<i[13][10]<<i[13][11]<<i[13][12]<<i[13][13]<<i[13][14]<<"\n";
      cout<<i[14][0]<<i[14][1]<<i[14][2]<<i[14][3]<<i[14][4]<<i[14][5]<<i[14][6]<<i[14][7]<<i[14][8]<<i[14][9]<<i[14][10]<<i[14][11]<<i[14][12]<<i[14][13]<<i[14][14]<<"\n";
    
      return 0;
    
    }
    Please don't flame for the Global Variables. It's the only way I could think of apart from putting it all in a class.

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    99

    :(

    Anyone?

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    30

    Well...

    Your code is a bit messy and some of the things look a bit weird. HereŽs
    what IŽd use to do what you want (more or less the same code, but not as
    messy):

    Code:
    #include <stdio.h>
    #include <windows.h>
    #include <stdlib.h>
    #include <process.h>
    #include <iostream.h>		//I don't really understand why you include
    #include <conio_mingw.h>//all of this, perhaps necessary with your compiler? 				
    void ShowBoard();
    
    char Board[15][15]; //you can initalize this the same way you did, or similar
    				//to what IŽll do with ShowBoard();
    void main()
    {
    	int x=7,y=7;
    	Sleep(100);
    	ShowBoard();
    	while(1)
    	{
    		if(kbhit())
    		{
    			char key=getch();
    			if(key==75)
    			{
    				Board[x][y]=' ';
    				x--;				//notice that the x-axis is the
    				Board[x][y]=2;	//horisontal axis, not the vertical
    			}
    			//etc...
    			ShowBoard();
    		}
    	}
    }
    
    void ShowBoard()
    {
    	for(int y=1;y<16;y++)
    	{
    		for(int x=1;x<16;x++)
    		{
    				cout<<Board[x][y];
    		}
    	}
    }
    I haven't really looked it through or tried it or anything, but it should work.

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    99
    Thanks heaps! Yes I realize my code was messy, thanks for cleaning it up!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simulate Keys with a Keyboard Hook
    By guitarist809 in forum Windows Programming
    Replies: 3
    Last Post: 11-14-2008, 08:14 PM
  2. Replies: 5
    Last Post: 11-30-2007, 09:46 AM
  3. detecting two keys at once using getch,switch,case
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 05-14-2002, 11:54 PM
  4. detecting two keys at once using getch,switch,case
    By Unregistered in forum Game Programming
    Replies: 1
    Last Post: 05-11-2002, 12:03 PM