Skip to main content
gamedev.net
🔒 Locked

Keyboard Input

Started by lkw0x53 5 replies 900+ views
Original Post
lkw0x53
lkw0x53
How do I detect when someone presses two keys at once. I have a game where the up keys moves forward, the left/right keys move sideways when they are pressed individually. When they are both pressed I want to move forward and left at the same time, but I don't know how to detect this. Thanks!
Brocketino
Brocketino
If you are using dinput then you could do something like this:


if(g_Keyboard->GetInput(DIK_LEFT) && g_Keyboard->GetInput(DIK_UP))
{
// Go northwest
}


Mercury
Mercury
If you're using Win32, you can use GetAsyncKeyState.
hplus0603
hplus0603
  velocity.x = 0;  velocity.y = 0;  if( KeyIsDown( KEY_FORWARD ) ) velocity.y += 1;  if( KeyIsDown( KEY_LEFT ) ) velocity.x -= 1;  if( KeyIsDown( KEY_RIGHT ) ) velocity.x += 1;  velocity.normalize();

enum Bool { True, False, FileNotFound };
lkw0x53
lkw0x53
This is what I'm using for my other stuff. I should mention that I need to use GLUT with it and that it can't be anything Windows specific.
void special_keyboard_func(int key, int x, int y){	switch(key)	{		case GLUT_KEY_PAGE_UP:				// Tilt up			z          -= 0.2f;			lookupdown -= 0.2f;						if(pxangle >- 60) 				pxangle -= 5;		break;    		case GLUT_KEY_PAGE_DOWN:			// Tilt down			z          += 0.2f;			lookupdown += 0.2f;			if(pxangle < 60)				pxangle += 5;		break;				default:		break;	}	switch(key)	{		case GLUT_KEY_RIGHT	:				yrot -= 2.0f;				// If right key is pressed rotate world left.			break;			case GLUT_KEY_LEFT:				yrot += 2.0f;				// If left key is pressed rotate world right.			break;		default:		break;	}	switch(key)	{		case GLUT_KEY_UP:												// If up key is pressed go forward			xpos -= (float)sin(yrot*RAD_PER_DEG) * 0.1f;			zpos -= (float)cos(yrot*RAD_PER_DEG) * 0.1f;							if (walkbiasangle >= 359.0f)				walkbiasangle = 0.0f;				else 				walkbiasangle+= 10;			walkbias = (float)sin(walkbiasangle * RAD_PER_DEG)/20.0f;		break;		case GLUT_KEY_DOWN:												// If down key is pressed go backwards			xpos += (float)sin(yrot*RAD_PER_DEG) * 0.1f;			zpos += (float)cos(yrot*RAD_PER_DEG) * 0.1f;							if (walkbiasangle <= 1.0f)				walkbiasangle = 359.0f;				else 				walkbiasangle-= 10;						walkbias = (float)sin(walkbiasangle * RAD_PER_DEG)/20.0f;		break;				default:		break;	}	display_func();}
zedzeek
zedzeek
one method is

create a buffer of keystates
eg
#define KEY_PRESSED 1
#define KEY_NOT_PRESSED 2
int key_state[512];

and then in the keydown messgae set the relevant key_state to KEY_PRESSED
eg
key_pessed_message( int key_code )
{
key_state[ key_code ] = KEY_PRESSED;
}

finally during your movement loop do someit like

if ( key_state[ left_arrow ] == KEY_PRESSED )
move_left()

also read this
http://www.sjbaker.org/steve/omniv/keyboards_are_evil.html
lkw0x53
lkw0x53
Thank you for your suggestions, but I'm afraid none of them helped that much.

Brocketino: I am using OpenGL, not DirectX, so dinput is not available.

Mercury: I need something that will work on both Windows and Unix platforms.

hplus0603: I couldn't figure out what you're doing here, looks like you have some function KeyIsDown that tells you if a key is pressed. Would you mind explaining how this function gets this information?

zedzeek: I do not know anything about keydown messages. The only keyboard handlers in OpenGL that I know of are part of the GLUT toolkit (glutKeyboardFunc and glutSpecialFunc). Obviously I cant just create a function named key_passed_message() and expect it to work. Some sort of handler must be set up and this function would be specified as the handler. Please explain how the handler is set up to get the keyboard status.


Does anyone else know of any keyboard handling libraries that are available to both Unix and Windows?

I have tried to get SDL working but keep running into tons of linker errors. (for example: "pac error LNK2005: _exit already defined in LIBCD.lib", "pac error LNK2019: unresolved external symbol _SDL_Init referenced in function _main"). Any help would be appreciated.

Thanks!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.