taking keyboard input without waiting for it
how can i take input from the keyboard i.e the arrow keys and ''space'' in console mode , without pausing until the user presses a button?
thanks
Im assuming you know about ReadConsoleInput. Just above ReadConsoleInput type this:
m_hInput is your input handle.
and 200 is the amount of miliseconds you want it to wait for.
Jeff D
if(WAIT_TIMEOUT == WaitForSingleObject(m_hInput, 200))//Do what you want it to do here
m_hInput is your input handle.
and 200 is the amount of miliseconds you want it to wait for.
Jeff D
Suffered seven plagues, but refused to let the slaves go free. ~ Ross Atherton
It depends on what platform sometimes...I remember a whileback
there was like a KeyDown function, and what you could do was
do otherstuff, check to see if KeyDown was true, if so, call
getch();
Or something like that
-=Lohrno
there was like a KeyDown function, and what you could do was
do otherstuff, check to see if KeyDown was true, if so, call
getch();
Or something like that
-=Lohrno
Thanks Jeff D, i tried the code you gave me, (ReadConsoleInput is what i am using) but it did not work the way i want it to, although i may have put it in the wrong place.
It still wont perform the game loop continuosly without waiting to take the input.
The game i am making is a tetris clone and the problem is that the user has to press a keyboard button every single loop of the game to make the blocks fall.
Sorry if im just stupid and the code you gave me can work the way i want it to, if so can you post something to help me use it correctly. Otherwise, what ways are there of taking input in this way?
Thanks
It still wont perform the game loop continuosly without waiting to take the input.
The game i am making is a tetris clone and the problem is that the user has to press a keyboard button every single loop of the game to make the blocks fall.
Sorry if im just stupid and the code you gave me can work the way i want it to, if so can you post something to help me use it correctly. Otherwise, what ways are there of taking input in this way?
Thanks
This is what I would do.
First I would make the input part its own function and have it return an int 0 for nothing, 1 playermoved item left, 2 player moved item right,3 player forced item down, 4 player rotated item right, 5 player rotated item left.
ok heres the code:
I hope that helps.
Jeff D
Edited by - Jeff D on February 26, 2002 5:49:40 PM
First I would make the input part its own function and have it return an int 0 for nothing, 1 playermoved item left, 2 player moved item right,3 player forced item down, 4 player rotated item right, 5 player rotated item left.
ok heres the code:
//hInput is your handleint GetUserInput(){ //locals INPUT_RECORD InputRecord; DWORD Events = 0; //function body FlushConsoleInputBuffer(hInput); if(WAIT_TIMEOUT == WaitForSingleObject(hInput, 200)) return 0; //Player did nothing ReadConsoleInput(hInput, &InputRecord, 1, &Events); if(InputRecord.EventType == KEY_EVENT) { if(InputRecord.Event.KeyEvent.bKeyDown) { switch(InputRecord.Event.KeyEvent.wVirtualKeyCode) { case VK_DOWN: //Moves piece down return 3; break; case VK_LEFT: //moves piece left return 1; break; case VK_RIGHT: //moves piece right return 2; break; case VK_SHIFT: //rotates piece left return 4; break; case VK_RETURN://rotates piece right return 5; break; } } } return 7; //Reason is that if user pushes anything thats not //above it willdo the loop fast.}//This is your game loop done is a bool that starts false//localsint UserInput;while(!Done){ UserInput = GetUserInput(); if(UserInput != 7) { //This will be where you find out what the user inputed and do //what you need to the piece. }}
I hope that helps.
Jeff D
Edited by - Jeff D on February 26, 2002 5:49:40 PM
Suffered seven plagues, but refused to let the slaves go free. ~ Ross Atherton
Yeah I dont know if hes writing it in Linux, or Windows or what
but if its windows, I''d just use GetAsyncKeyState() or
DirectInput.(its an API)
you''d use it something like this:
if (GetAynscKeyState(VK_LEFT)
paddle.moveleft(); //Or whatever
For a list of VK codes, look at MSDN for VK and you should find
a list somewhere. Also you can use GetAsyncKeyState for Mouse
Buttons.
If it''s something else, IE DOS, or linux, or mac or something
it might be something else but I believe that KeyDown, and
getch() will work too. (or however they''re spelled) It''d work
something like
if (KeyDown())
getch(c);
-=Lohrno
but if its windows, I''d just use GetAsyncKeyState() or
DirectInput.(its an API)
you''d use it something like this:
if (GetAynscKeyState(VK_LEFT)
paddle.moveleft(); //Or whatever
For a list of VK codes, look at MSDN for VK and you should find
a list somewhere. Also you can use GetAsyncKeyState for Mouse
Buttons.
If it''s something else, IE DOS, or linux, or mac or something
it might be something else but I believe that KeyDown, and
getch() will work too. (or however they''re spelled) It''d work
something like
if (KeyDown())
getch(c);
-=Lohrno
Jeff D''s solution is kinda good because it''s not so specific.
You could use it for a menu as well as the game.
-=Lohrno
You could use it for a menu as well as the game.
-=Lohrno
He states hes in Console mode. No Win32 API at all.
Jeff D
Edited by - Jeff D on February 26, 2002 6:38:27 PM
Jeff D
Edited by - Jeff D on February 26, 2002 6:38:27 PM
Suffered seven plagues, but refused to let the slaves go free. ~ Ross Atherton
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement