Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

nws_mrman

taking keyboard input without waiting for it

This topic is 5960 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
Im assuming you know about ReadConsoleInput. Just above ReadConsoleInput type this:


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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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:



//hInput is your handle
int 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
//locals
int 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

Share this post


Link to post
Share on other sites
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




Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
if you dont want to use that massive win32 console api, use std-io/lib


  
#include <stdio.h>

bool keyPressed(char key)
{
char c = 0;
if ( kbhit() )
if (!(c = getch() ) c = getch();
return (c == key);
}

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!