KeyBoard Input under Windows

Started by
2 comments, last by LonelyStar 20 years, 5 months ago
Hey Everybody, I am looking for a methode to get keyboard input under Windows. GetAsyncKeyState does not suite me, because I need Key EVENTs and not states!!! There are 2 Problems with the WM_KEYDOWN message (and the WM_KEYUP message): 1. If a key is pressed and hold down, a lot of WM_KEYDOWN messages are send (I do not want that). 2. If I understand MSDN correctly, if the ALT key is pressed, no WM_KEYDOWN message is sent. Is there any way to get game-suitable input under windows (without using DirectInput)??? Thanks! Nathan
Advertisement
1. You can check the prior state of the key to make sure you only handle the message once per press

2. WM_KEYDOWN can handle the right ALT key. It will set bit 24 of the LPARAM.

Colin Jeanne | Invader''s Realm
You could store 2 full key states (GetKeyboardState), and check if the state this frame is different than the one last frame. If it is, then the key was toggled to the new state. You could do something like
for(int ii = 0; ii < 256; ++ii){     if(old_keystates[ii] != new_keystates[ii])          KeyHandler(ii, new_keystates[ii]);}
every frame and then you have your own key event system.
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk
  /\             /  /____\   O  O  O 

what he said

#include "stdafx.h"#include "config.h"#include "log.h"#include "input.h"CInput::CInput(){}bool CInput::KeyInput(){	bool r = false;	for (int i = 0; i < 256; i++) r = r | KeyDown[i];	return r;}bool CInput::MouseInput(){	bool r = false;	for (int i = 0; i < 4; i++) r = r | MouseBut[i];	return r;}bool CInput::CheckKeyPress(int code){	bool is = KeyPress[code];	KeyPress[code] = false;	return is;}bool CInput::CheckKey(int code){	return KeyDown[code];}bool CInput::CheckBut(int but){	return MouseBut[but];}bool CInput::CheckClick(int but){	bool r = MouseClick[but];	MouseClick[but] = false;	return r;}	int CInput::GetMouseX(){	return PosX;}int CInput::GetMouseY(){	return PosY;}void CInput::CenterMouse(){	PosX = curConfig.scrWidth >> 1;	PosY = curConfig.scrHeight >> 1;}void CInput::DoInput(){	int i;	unsigned char buf[256];	int hr;	hr = Keyboard->GetDeviceState(256, buf);	if (hr == DIERR_INPUTLOST) 	{		Keyboard->Acquire();		memset(KeyDown, 0, 256);		memset(KeyPress, 0, 256);		return;	}	Mouse->GetDeviceState(sizeof(MouseState), &MouseState);	if (hr == DIERR_INPUTLOST) 	{				Mouse->Acquire();		memset(MouseBut, 0, 4);		return;	}	for (i = 0; i < 256; i++)	{		if (buf[i] & 0x80)			{			KeyDown[i] = true;		}		else		{			if (KeyDown[i]) 				KeyPress[i] = true;			else 				KeyPress[i] = false;			KeyDown[i] = false;		}	}	PosX += MouseState.lX * 2;	PosY += MouseState.lY * 2;	for (i = 0; i < 4; i++)	{		if (MouseState.rgbButtons[i] & 0x80)		{			MouseBut[i] = true;		}		else		{			if (MouseBut[i])				MouseClick[i] = true;			else 				MouseClick[i] = false;			MouseBut[i] = false;		}	}}int CInput::Initialize(){	if (DirectInput8Create(g_hInstance, DIRECTINPUT_VERSION, IID_IDirectInput8A, (void**)&di, NULL))	{		lprintf("Cannointtialize DirectInput8\n");		return SYS_ERR;	}	if (di->CreateDevice(GUID_SysKeyboard, &Keyboard, NULL))	{		lprintf("Cannot create keyboard object\n");		return SYS_ERR;	}	if (di->CreateDevice(GUID_SysMouse, &Mouse, NULL))	{		lprintf("Cannot create mouse object\n");		return SYS_ERR;	}	if (Keyboard->SetDataFormat(&c_dfDIKeyboard))	{		lprintf("Cannot set keyboard data format\n");		return SYS_ERR;	}	if (Mouse->SetDataFormat(&c_dfDIMouse))	{		lprintf("Cannot set mouse    data format\n");		return SYS_ERR;	}	if (Keyboard->SetCooperativeLevel(g_hWnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE))	{		lprintf("Cannot set cooperative level\n");		return SYS_ERR;	}	int mode;#ifdef _DEBUG 	mode = DISCL_NONEXCLUSIVE;#else 	mode = DISCL_EXCLUSIVE;#endif	if (Mouse->SetCooperativeLevel(g_hWnd, mode | DISCL_FOREGROUND))	{		lprintf("Cannot set cooperative level\n");		return SYS_ERR;	}	if (Keyboard->Acquire())	{		lprintf("Cannot acquire keyboard\n");		return SYS_ERR;	}	if (Mouse->Acquire())	{		lprintf("Cannot acquire mouse\n");		return SYS_ERR;	}	return SYS_OK;}int CInput::Terminate(){	if (Keyboard)	{		Keyboard->Unacquire();		Keyboard->Release();	}	if (Mouse)	{		Mouse->Unacquire();		Mouse->Release();	}	if (di)	{		di->Release();	}	return SYS_OK;}char GetKeyChar(int key){	switch (key)	{		case DIK_1:			return  '1';				case DIK_2:         return  '2';				case DIK_3:         return  '3';				case DIK_4:         return  '4';				case DIK_5:			return  '5';				case DIK_6:         return  '6';				case DIK_7:         return  '7';				case DIK_8:         return  '8';				case DIK_9:			return  '9';				case DIK_0:         return  '0';				case DIK_MINUS:		return  '-';				case DIK_EQUALS:	return  '=';				case DIK_Q:			return  'Q';		case DIK_W:			return  'W';				case DIK_E:			return  'E';			case DIK_R:			return  'R';				case DIK_T:			return  'T';			case DIK_Y:			return  'Y';				case DIK_U:			return  'U';			case DIK_I:			return  'I';				case DIK_O:			return  'O';			case DIK_P:			return  'P';				case DIK_LBRACKET:	return  '[';			case DIK_RBRACKET:	return  ']';				case DIK_A:			return  'A';			case DIK_S:			return  'S';				case DIK_D:			return  'D';			case DIK_F:			return  'F';				case DIK_G:			return  'G';			case DIK_H:			return  'H';				case DIK_J:			return  'J';			case DIK_K:			return  'K';				case DIK_L:			return  'L';			case DIK_SEMICOLON:	return  ';';				case DIK_APOSTROPHE:return  '\';			case DIK_BACKSLASH:	return  '\\';			case DIK_Z:			return  'Z';				case DIK_X:			return  'X';			case DIK_C:			return  'C';				case DIK_V:			return  'V';			case DIK_B:			return  'B';				case DIK_N:			return  'N';			case DIK_M:			return  'M';				case DIK_COMMA:		return  ',';			case DIK_PERIOD:	return  '.';				case DIK_SLASH:		return  '/';				case DIK_MULTIPLY:	return  '*';			case DIK_SPACE:		return  ' ';		case DIK_NUMPAD7:	return  '7';			case DIK_NUMPAD8:	return  '8';			case DIK_NUMPAD9:	return  '9';			case DIK_SUBTRACT:	return  '-';				case DIK_NUMPAD4:	return  '4';			case DIK_NUMPAD5:	return  '5';			case DIK_NUMPAD6:	return  '6';			case DIK_ADD:		return  '+';			case DIK_NUMPAD1:	return  '1';			case DIK_NUMPAD2:	return  '2';			case DIK_NUMPAD3:	return  '3';			case DIK_NUMPAD0:	return  '0';			case DIK_DECIMAL:	return  '.';		}	return 0;}void GetKeyName(int key, char *keyn){		switch (key)	{		case DIK_ESCAPE:	strcpy(keyn, "ESCAPE");		return;		case DIK_1:			strcpy(keyn, "1");			return;		case DIK_2:         strcpy(keyn, "2");			return;		case DIK_3:         strcpy(keyn, "3");			return;		case DIK_4:         strcpy(keyn, "4");			return;		case DIK_5:			strcpy(keyn, "5");			return;		case DIK_6:         strcpy(keyn, "6");			return;		case DIK_7:         strcpy(keyn, "7");			return;		case DIK_8:         strcpy(keyn, "8");			return;		case DIK_9:			strcpy(keyn, "9");			return;		case DIK_0:         strcpy(keyn, "0");			return;		case DIK_MINUS:		strcpy(keyn, "-");			return;			case DIK_EQUALS:	strcpy(keyn, "=");			return;		case DIK_BACK:		strcpy(keyn, "BKSPC");		return;		case DIK_TAB:		strcpy(keyn, "TAB");		return;		case DIK_Q:			strcpy(keyn, "Q");			return;		case DIK_W:			strcpy(keyn, "W");			return;		case DIK_E:			strcpy(keyn, "E");			return;		case DIK_R:			strcpy(keyn, "R");			return;		case DIK_T:			strcpy(keyn, "T");			return;		case DIK_Y:			strcpy(keyn, "Y");			return;		case DIK_U:			strcpy(keyn, "U");			return;		case DIK_I:			strcpy(keyn, "I");			return;		case DIK_O:			strcpy(keyn, "O");			return;		case DIK_P:			strcpy(keyn, "P");			return;		case DIK_LBRACKET:	strcpy(keyn, "[");			return;		case DIK_RBRACKET:	strcpy(keyn, "]");			return;		case DIK_RETURN:	strcpy(keyn, "ENTER");		return;		case DIK_LCONTROL:	strcpy(keyn, "L ");			return;		case DIK_A:			strcpy(keyn, "A");			return;		case DIK_S:			strcpy(keyn, "S");			return;		case DIK_D:			strcpy(keyn, "D");			return;		case DIK_F:			strcpy(keyn, "F");			return;		case DIK_G:			strcpy(keyn, "G");			return;		case DIK_H:			strcpy(keyn, "H");			return;		case DIK_J:			strcpy(keyn, "J");			return;		case DIK_K:			strcpy(keyn, "K");			return;		case DIK_L:			strcpy(keyn, "L");			return;		case DIK_SEMICOLON:	strcpy(keyn, ";");			return;		case DIK_APOSTROPHE:strcpy(keyn, "'");			return;		case DIK_LSHIFT:	strcpy(keyn, "L SHIFT");	return;		case DIK_BACKSLASH:	strcpy(keyn, "\\");			return;		case DIK_Z:			strcpy(keyn, "Z");			return;		case DIK_X:			strcpy(keyn, "X");			return;		case DIK_C:			strcpy(keyn, "C");			return;		case DIK_V:			strcpy(keyn, "V");			return;			case DIK_B:			strcpy(keyn, "B");			return;		case DIK_N:			strcpy(keyn, "N");			return;		case DIK_M:			strcpy(keyn, "M");			return;		case DIK_COMMA:		strcpy(keyn, ",");			return;		case DIK_PERIOD:	strcpy(keyn, ".");			return;			case DIK_SLASH:		strcpy(keyn, "/");			return;		case DIK_RSHIFT:	strcpy(keyn, "L SHIFT");	return;		case DIK_MULTIPLY:	strcpy(keyn, "*");			return;		case DIK_LMENU:		strcpy(keyn, "L ALT");		return;		case DIK_SPACE:		strcpy(keyn, "SPACE");		return;			case DIK_F1:		strcpy(keyn, "F1");			return;		case DIK_F2:		strcpy(keyn, "F2");			return;		case DIK_F3:		strcpy(keyn, "F3");			return;		case DIK_F4:		strcpy(keyn, "F4");			return;		case DIK_F5:		strcpy(keyn, "F5");			return;		case DIK_F6:		strcpy(keyn, "F6");			return;		case DIK_F7:		strcpy(keyn, "F7");			return;		case DIK_F8:		strcpy(keyn, "F8");			return;		case DIK_F9:		strcpy(keyn, "F9");			return;		case DIK_F10:		strcpy(keyn, "F10");		return;			case DIK_NUMLOCK:	strcpy(keyn, "NUMLOCK");	return;		case DIK_SCROLL:	strcpy(keyn, "SCROLL LOCK");return;		case DIK_NUMPAD7:	strcpy(keyn, "NUMPAD7");	return;		case DIK_NUMPAD8:	strcpy(keyn, "NUMPAD8");	return;		case DIK_NUMPAD9:	strcpy(keyn, "NUMPAD9");	return;		case DIK_SUBTRACT:	strcpy(keyn, "-");			return;		case DIK_NUMPAD4:	strcpy(keyn, "NUMPAD4");	return;		case DIK_NUMPAD5:	strcpy(keyn, "NUMPAD5");	return;		case DIK_NUMPAD6:	strcpy(keyn, "NUMPAD6");	return;		case DIK_ADD:		strcpy(keyn, "NUMPAD+");	return;			case DIK_NUMPAD1:	strcpy(keyn, "NUMPAD1");	return;		case DIK_NUMPAD2:	strcpy(keyn, "NUMPAD2");	return;		case DIK_NUMPAD3:	strcpy(keyn, "NUMPAD3");	return;		case DIK_NUMPAD0:	strcpy(keyn, "NUMPAD0");	return;		case DIK_DECIMAL:	strcpy(keyn, "NUMPAD.");	return;			case DIK_F11:		strcpy(keyn, "F11");		return;		case DIK_F12:		strcpy(keyn, "F12");		return;		case DIK_UP:		strcpy(keyn, "UP");			return;		case DIK_DOWN:		strcpy(keyn, "DOWN");		return;		case DIK_LEFT:		strcpy(keyn, "LEFT");		return;		case DIK_RIGHT:		strcpy(keyn, "RIGHT");		return;		case DIK_RMENU:		strcpy(keyn, "R ALT");		return;		case DIK_PRIOR:		strcpy(keyn, "PAGE UP");	return;		case DIK_NEXT:		strcpy(keyn, "PAGE DN");	return;		case DIK_END:		strcpy(keyn, "END");		return;		case DIK_HOME:		strcpy(keyn, "HOME");		return;	}		sprintf(keyn, "%d", key);}


[ My Site ]
All your source are belong to us
/*ilici*/

[edited by - Ilici on November 13, 2003 11:03:56 AM]

[edited by - Ilici on November 13, 2003 11:04:31 AM]

This topic is closed to new replies.

Advertisement