DIK_[ArrowKey] not work?

This is a discussion on DIK_[ArrowKey] not work? within the Game Programming forums, part of the General Programming Boards category; Maybe I'm doing something stupid, but when I use direct input and try to check it an arrow key is ...

  1. #1
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972

    DIK_[ArrowKey] not work?

    Maybe I'm doing something stupid, but when I use direct input and try to check it an arrow key is down (eg, DIK_RIGHT, DIK_LEFT) it never returns true. Other keys work fine(DIK_ESCAPE,DIK_ENTER,DIK_D,etc) Here's the code in question:
    Code:
      m_input->CheckInput();
      if (m_input->KeyDown(DIK_RIGHT))
        m_Player.MoveRel(DEF_PLAYERVEL,0);
      else if (m_input->KeyDown(DIK_LEFT))
        m_Player.MoveRel(-DEF_PLAYERVEL,0);
    //...In input.cpp
    int Input::CheckInput()
    {
    
      if (FAILED(m_keyboard->GetDeviceState(sizeof(keystate), (LPVOID)keystate)))
        return ERR_GETDEVSTATE;
      if (FAILED(m_mouse->GetDeviceState(sizeof(DIMOUSESTATE), (LPVOID)&mouse_state)))
        return ERR_GETDEVSTATE;
     
     return 0;
    }  
    
    bool Input::KeyDown(char DIK)
    {
      return keystate[DIK] & 0x80 ? true : false;
    }
    Any ideas?
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  2. #2
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Try this.
    Code:
    bool Input::KeyDown(char DIK)
    {
      return (keystate[DIK] & 0x80) ? true : false;
    }
    Perhaps operator precedence is the problem.

    I run into similar probs when doing this:

    Code:
    void SomeFunc(int mode)
    {
      if (mode & SET_VAR==SET_VAR) DoSomething();
      ...
    }
    That won't work. The bitwise and operation must have parentheses around it. Not sure it's your problem but it might be. Everything else looks ok.

  3. #3
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    I tried your suggestion but it makes no difference. The only thing I can think of now is that there's something wrong with the way I initialize Direct Input. So here's my Init function:
    Code:
    int Input::Init(HWND hwnd, HINSTANCE hInst)
    {
    
    if(FAILED(DirectInput8Create(hInst,DIRECTINPUT_VERSION,IID_IDirectInput8,(void**) &lpdi,NULL)))
      return ERR_DICREATE;  
    
    if (FAILED(lpdi->CreateDevice(GUID_SysKeyboard, &m_keyboard, NULL)))
      return ERR_CREATEDEVICE;    
    
    if (FAILED(m_keyboard->SetDataFormat(&c_dfDIKeyboard)))
      return ERR_SETDATAFMT;
    
    if (FAILED(m_keyboard->SetCooperativeLevel(hwnd, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE)))
      return ERR_SETCOOPLVL;  
    
    if (FAILED(m_keyboard->Acquire()))
      return ERR_ACQUIRE;
      
    /*snip: Initialize mouse*/
    
    return 0;   
    }
    I don't notice anything wrong here though
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  4. #4
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Here's mine and it looks the same:

    Code:
    bool CD3DApp::InitInput(void)
    {
      Input.Init(m_hAppInstance);
    
      Keyboard=new CKeyboard(Input.GetInterface());
      if (!Keyboard) return false;
    
      Keyboard->Init(m_hwndAppWindowHandle);
    
      Mouse=new CMouse(Input.GetInterface());
      if (!Mouse) {return false;};
      
      
      Mouse->Init(m_hwndAppWindowHandle);
    
      return true;
    }
    Code:
    #include "CDXinput.h"
    
    
    HRESULT CDXInput::Init(HINSTANCE hinst)
    {
        //Create IDirectInput8 interface pointer 
        if (FAILED(DirectInput8Create(hinst, 
                                  DIRECTINPUT_VERSION, 
                                  IID_IDirectInput8, 
                                  (void**)&_pDI8, 
                                  NULL)))
        {
            ::MessageBox(0,"Failed to create DirectInput8 interface",0,0);
        }
        return true;
    
    }

    Code:
    HRESULT CKeyboard::Init(HWND window)
    {
        
        //Create device
        if (FAILED(_pDI8->CreateDevice(GUID_SysKeyboard, 
                                       &_pDID8, 
                                       NULL)))
        {
            ::MessageBox(0,"Could not create keyboard device",0,0);
        }
        
        
        //Set cooperative level
        if (FAILED(_pDID8->SetCooperativeLevel(window,
                                               DISCL_BACKGROUND | DISCL_NONEXCLUSIVE)))
        {
            ::MessageBox(0,"Could not set keyboard cooperative level",0,0);
        }
    
        //Set data format
        if (FAILED(_pDID8->SetDataFormat(&c_dfDIKeyboard)))
        {
            ::MessageBox(0,"Could not set keyboard data format",0,0);
        }
    
        //Acquire the keyboard
        if (FAILED(_pDID8->Acquire()))
        {
            ::MessageBox(0,"Could not acquire keyboard device",0,0);
        }
    
        return true;
    } 
    
    HRESULT CKeyboard::Update(void)
    {
        HRESULT hr;
       
        //Make sure we still have the device
        //If so fill keystate with current state values representing current
        //keyboard state
        hr=(_pDID8->GetDeviceState(sizeof(keystate),(LPVOID)&keystate));
        if (hr==DIERR_INPUTLOST)
        {
            ::MessageBox(0,"Input lost",0,0);
        }
       
        return true;
    }
    I split it up so the engine does all of it for you once you init the engine. You never call these functions directly.

    But it looks the same. Not sure why you are having probs.

  5. #5
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Finally found the problem. Turns out my array was an array of unsigned chars.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  6. #6
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Roflmao.

    No worries bud. Shakti just found my oodles of memory leaks in my editor code and it was a really dumb mistake.

    Look at this:

    Code:
    Object *obj=new Object();
    Object *obj2=obj;
    obj2=new Object();
    That is essentially what I was doing. Doh!!! So we all make the same crazy errors.

    Glad you got it working.

  7. #7
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Yeah I also had a leak in this program (hopefully only one) where I forgot to release a D3DXSPRITE I created. After a while, my initialization function started failing and I couldn't figure out why since I hadn't touched it. Good times...
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  8. #8
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    How are you doing bounding boxes with ID3DXSPRITE? I couldn't get to the vertex data to create the bounding box.

  9. #9
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    I guess I haven't done bounding boxes yet (haven't even started on collision detection). I figured, to start with, I'd just use the rect of the bitmap and go from there
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 07:48 PM
  2. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  3. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21