Skip to main content
GameDev.net gamedev.net
🔒 Locked

Clearing the data

Started by sathenzar Jan 21, 2005 at 8:43 AM 0 replies 400+ views
Original Post
sathenzar
sathenzar
Well since no one for some reason wants to reply to my other post relating to how to fix my DirectInput problem. Maybe I can ask somthing more simplier that people will actually take time to answer (hopefully), how do you stop DirectX form thing you keep pressing the space bar. As a test I put

if(KEYDOWN(buffer, DIK_SPACE))
	{
		MessageBox(NULL, "DFDF", "DFGFDGFDGDFG", MB_OK);
	}

and it does nothing but give me the message box after I hit teh space bar. It never quits, and I can't figure out how to make it stop. Can someone help me please?
Namethatnobodyelsetook
Namethatnobodyelsetook
The messagebox will take focus from your app. You'll lose your DInput devices. You'll have to reacquire them. If you cannot acquire the input, you should clear your key array to avoid left over keydowns from when you had the device previously. Also, make sure you ask DInput to Poll the device before asking for new state (though I think this is only needed for Joysticks... I don't seem to call it during my keyboard code).

Here's an example of some code that works (as far as I know). You'll have to tweak it a bit to use the correct DInput object for your app.
#ifndef INC_KEYBOARD_H_#define INC_KEYBOARD_H_// Keyboard -- Defines an interface to a keyboard device.// (C)2003 David 'Redbeard' Eccleston//// Obtains input from a keyboard device.  Usually only one keyboard is present// on a system, and as such, only one instance of a keyboard object should be// created.//// Create a keyboard object with 'new', then call Init().  You can now request// keyboard data.  When a keyboard object is lost, it attempts to regain// itself anytime you request it to poll information.//// Usually, Poll() is called once per frame, before any keyboard data is// required.  After calling Poll(), the remaining functions can be used// to determine the state of the keys on the keyboard.//// IsPressed(key) - returns TRUE if the requested key (DirectInput keyboard// code) is currently pressed down.//// MakePressed(key) - can simulate a keypress that later code may act upon//// IsLatched(key) - returns TRUE if the key was pressed since the last time// the key's latch was cleared.  When a latch is cleared, the key is not// latched again until the key goes up, and is pressed down again.//// JustLatched(key) - returns TRUE is the key was latched on the last Poll()// call.  If you don't clear the latch, you can determine that the key wasn't // just pressed using this method.  Very odd, unsure when it would be useful,// but it was easy to support, so it's here.//// ReadAndClearLatch(key) - Reads the latch and clears it.  If no other parts// of your app will check the same latched key, this is useful in that you// won't have to make a second call to clear the latched key.//// ClearLatch() - Clears the latch of a key.  Allows you to determine the// next time the key is pressed, and to not think the key was pressed again// when it has been down for more than a single Poll() call.//// ClearAllLatches() - Clears the latches of all keys on the keyboard//// ClearAllLatchesAndKeysDown() - Stops any further keyboard activity until// the next poll.class Keyboard{public:  LPDIRECTINPUTDEVICE8  m_pKeyboard;  BYTE                  m_bKeyLatch[256];  BOOL Init();  void Poll();  BYTE IsPressed(int nKey) {return m_bKeyLatch[nKey] & 0x80;}  void MakePressed(BYTE nKey) {m_bKeyLatch[nKey] |= 0x80;}  BYTE IsLatched(int nKey) {return m_bKeyLatch[nKey] & 0x40;}  BYTE JustLatched(int nKey) {return m_bKeyLatch[nKey] & 0x20;}  BYTE ReadAndClearLatch(int nKey) {BYTE l = m_bKeyLatch[nKey]; m_bKeyLatch[nKey] &= ~0x40; return l&0x40;}  void ClearLatch(int nKey) {m_bKeyLatch[nKey] &= ~0x40;}  void ClearAllLatches();  void ClearAllLatchesAndKeysDown();  Keyboard();  ~Keyboard();};#endif

#include "stdafx.h"#include "engine3d.h"#include "keyboard.h"#include "main.h"Keyboard::Keyboard(){  m_pKeyboard = 0;}Keyboard::~Keyboard(){  if (m_pKeyboard)  {    m_pKeyboard->Unacquire();    m_pKeyboard->Release();  }}void Keyboard::ClearAllLatches(){  DWORD *l, n;  n = 256/4;  l = (DWORD *)m_bKeyLatch;  while (n--)  {    *l &= ~0x40404040;    l++;  }}void Keyboard::ClearAllLatchesAndKeysDown(){  memset(m_bKeyLatch, 0, sizeof(m_bKeyLatch));}BOOL Keyboard::Init(){  HRESULT hr;  ClearAllLatchesAndKeysDown();  if( FAILED( hr = g_Engine3D->m_pDInput->CreateDevice( GUID_SysKeyboard, &m_pKeyboard, NULL ) ) )  {    g_Errors.Add("Create keyboard");    g_Errors.Add((char *)DXGetErrorString9(hr));    return FALSE;  }  // This tells DirectInput that we will be passing an array  // of 256 bytes to IDirectInputDevice::GetDeviceState.  if( FAILED( hr = m_pKeyboard->SetDataFormat( &c_dfDIKeyboard ) ) )  {    g_Errors.Add("Set keyboard format");    g_Errors.Add((char *)DXGetErrorString9(hr));    return FALSE;  }  if (FAILED(hr = m_pKeyboard->SetCooperativeLevel( g_pDefaultEngineWindow->m_d3dpp.hDeviceWindow, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND )))  {    g_Errors.Add("Set keyboard cooperative level ");    g_Errors.Add((char *)DXGetErrorString9(hr));    return FALSE;  }  m_pKeyboard->Acquire();  return TRUE;}void Keyboard::Poll(){  HRESULT hr;  int n;  DWORD *down, *latch, d, l;  BYTE bKeys[256];  hr = m_pKeyboard->GetDeviceState(256, bKeys);  if (FAILED(hr))  {    // We'll get a 1 frame lag on re-acquiring the keyboard, but hey, not a big deal.    // Attempt to reacquire, and return so we don't use the uninitialized key states.    hr = m_pKeyboard->Acquire();    return;  }  // Now handle latched and just latched, and copy keydowns  n = 256/4;  down = (DWORD *)bKeys;  latch = (DWORD *)m_bKeyLatch;  while (n--)  {    d = *down & 0x80808080;   // Only look at the down flag    l = *latch & 0xC0C0C0C0;  // Keep previous down keys, and latches, but clear just latched    // Remember current set of down keys, keep previous uncleared latches,    // and add any keys that are newly down to both latch and just latched.    *latch = d | (l & 0x40404040) | ((d&(~l))>>1) | ((d&(~l))>>2);    down++;    latch++;  }}#if 0// Test sample app - all 3 methods of interpreting keysvoid Init(){  g_pKeyboard = new Keyboard();  if (!g_pKeyboard)    return E_FAIL;  if (!g_pKeyboard->Init())    return E_FAIL;}void Cleanup(){  if (g_pKeyboard)    delete g_pKeyboard;  g_pKeyboard = 0;}void ReadKeys(){  static float x=0, y=0;  static int mode=0;  g_Keyboard->Poll();  if (g_pKeyboard->IsLatched(DIK_T))  {    mode = (mode+1)%3;    g_pKeyboard->ClearAllLatches();  }  if (mode==0)  {    if (g_pKeyboard->IsPressed(DIK_UP))      y -= 1;    if (g_pKeyboard->IsPressed(DIK_DOWN))      y += 1;    if (g_pKeyboard->IsPressed(DIK_LEFT))      x -= 1;    if (g_pKeyboard->IsPressed(DIK_RIGHT))      x += 1;  }  else if (mode==1)  {    if (g_pKeyboard->IsLatched(DIK_UP))    {      y -= 5;      g_pKeyboard->ClearLatch(DIK_UP);    }    if (g_pKeyboard->IsLatched(DIK_DOWN))    {      y += 5;      g_pKeyboard->ClearLatch(DIK_DOWN);    }    if (g_pKeyboard->IsLatched(DIK_LEFT))    {      x -= 5;      g_pKeyboard->ClearLatch(DIK_LEFT);    }    if (g_pKeyboard->IsLatched(DIK_RIGHT))    {      x += 5;      g_pKeyboard->ClearLatch(DIK_RIGHT);    }  }  else if (mode==2)  {    if (g_pKeyboard->JustLatched(DIK_UP))      y -= 5;    if (g_pKeyboard->JustLatched(DIK_DOWN))      y += 5;    if (g_pKeyboard->JustLatched(DIK_LEFT))      x -= 5;    if (g_pKeyboard->JustLatched(DIK_RIGHT))      x += 5;  }}#endif

Topic Locked

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

Sign in to reply to this topic.