Archived

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

DInput checking just a few of the keys

This topic is 5653 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

Hi, I''ve written a simple "game" and I want DI for the input. It''s been the second time I ever used DI (the first time, it was working though, but the code is gone). I wrote it (as I remembered it) as if I was writing it under DX7, all the set-up things proceed ok (using FAILED macro) and then, when I retrieve the key buffer, I check for the keys (again, using the macro that was there in the docs). It works fine for all the keys (as defined in dinput.h (DIK_xxxx) up to F12, all the upper don''t (like DIK_LEFT, DIK_RIGHT, DIK_PRIOR etc). I''m using DX8.0 SDK. Anybody know''s what''s the problem? Thanks

Share this post


Link to post
Share on other sites
Only the high order bit should be verified, are you comparing with (keys[DIK_xxx]&0x80)!=0 or keys[DIK_xxx]!=0 directly?

If it's not your prob, post some code.

[edited by - Coincoin on June 23, 2002 10:35:42 AM]

Share this post


Link to post
Share on other sites
Ok here comes the code:


      
class CDirectInput{
protected:
LPDIRECTINPUT7 direct_input_object;
LPDIRECTINPUTDEVICE7 keyboard_object;
char buffer[256];
public:
CDirectInput();
~CDirectInput();

void Initialize();
void Poll(); char KeyDown(char key);
};

#pragma comment(lib, "dxguid.lib")
#pragma comment(lib, "dinput.lib")

CDirectInput::CDirectInput()
{
}

CDirectInput::~CDirectInput()
{
if(direct_input_object)
{
if(keyboard_object)
{
keyboard_object->Unacquire();
keyboard_object->Release();
}

direct_input_object->Release();
}
}

void CDirectInput::Initialize()
{
if(FAILED(DirectInputCreate(GetModuleHandle(NULL),DIRECTINPUT_VERSION,(LPDIRECTINPUT *)&direct_input_object,NULL)))
exit(-11);

if(FAILED(direct_input_object->CreateDeviceEx(GUID_SysKeyboard,IID_IDirectInputDevice7,(void **)&keyboard_object,NULL)))
exit(-12);

if(FAILED(keyboard_object->SetDataFormat(&c_dfDIKeyboard)))
exit(-13);

if(FAILED(keyboard_object->SetCooperativeLevel(game_window,DISCL_NONEXCLUSIVE | DISCL_BACKGROUND)))
exit(-14);
if(FAILED(keyboard_object->Acquire()))
exit(-15);
}

void CDirectInput::Poll()
{
if(FAILED(keyboard_object->GetDeviceState(256,&buffer)))
exit(-22);
}

char CDirectInput::KeyDown(char key)
{
if(buffer[key] & 0x80)
return 1;
else
return 0;
}


I then check whether the key is pressed like

  
input->Initialize();
input->Poll();
if(input->KeyDown(DIK_LEFT))
exit(-1);


it works fine if I pass DIK_A, DIK_0, DIK_F12 etc, up to DIK_F12 it works fine... Now I've finally linked it with the DX8 libs so there should be no version collision. Anybody knows? Thanks

[edited by - Caesar on June 23, 2002 1:18:08 PM]

[edited by - Caesar on June 23, 2002 1:19:44 PM]

Share this post


Link to post
Share on other sites