Err....uh.....is this for DirectX or what?
If you are using the Windows GDI then its not a mystery why you are having this problem.
If you are using DirectX, look at the following code:
Code:
#include "dinput.h"
#include <objbase.h>
#define INITGUID
//Set objects to null
LPDIRECTINPUT lpdi=NULL;
IDIRECTINPUTDEVICE lpdikey=NULL;
//Create DirectInput object - get interface
if (FAILED(DirectInputCreate(main_instance,DIRECTINPUT_VERSION,&lpdi,NULL))
{ //error }
//Create DirectInput device
if (FAILED(lpdi->CreateDevice(GUID_SysKeyboard,&lpdikey,NULL))
{ //error }
//Set cooperative level
if (FAILED(lpdikey->SetCooperativeLevel(main_window_handle,DISCL_BACKGROUND | DISCL_NONEXCLUSIVE)) {//error}
//Keystate array
typedef _DIKEYSTATE UCHAR[256];
//Set data format to keyboard
if (FAILED(lpdikey->SetDataFormat(&c_dfDIKeyboard))) {//error}
//Acquire the keyboard
if (FAILED(lpdikey->Acquire())) {//error}
//Get info from the keyboard
if (FAILED(lpdikey->GetDeviceState(sizeof(_DIKEYSTATE),(LPVOID)keystate))) {//error}
//Test for key down
if (keystate[DIK_ESCAPE] & 0x80)
{
//down
}
else
{
//up
}
//Could also use this macro for keydown
#define DIKEYDOWN(data,n) (data[n] &0x80)
This sets up the DirectInput object, Keyboard object, and shows how to test keys. It comes directly from Andre Lamothe's book, Tricks of the Windows Game Programming Gurus.