DirectInput arrow keys

This is a discussion on DirectInput arrow keys within the Windows Programming forums, part of the Platform Specific Boards category; What are the possible reasons why my directinput code can recognise normal keys like A-Z but it just doesnt recognise ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    133

    DirectInput arrow keys

    What are the possible reasons why my directinput code can recognise normal keys like A-Z but it just doesnt recognise arrow keys like DIK_UP,DIK_LEFT,DIK_RIGHT,DIK_DOWN.

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    288
    change the keystate array to a 'char' instead of 'unsigned char' array.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    287
    I usually do it with:
    Code:
    BYTE tec[256];
    m_keyboarddev->GetDeviceState(256,&tec);
    if(tec[DIK_UP] & 0x80) {/*is the up arrow key*/}
    /*do similar with the other keys*/
    the 'keyboarddev' should be your LPDIRECTINPUTDEVICE
    Niara

  4. #4
    Registered User
    Join Date
    Sep 2003
    Posts
    133
    Code:
    class Keyboard :
    	public Controller
    {
    public:
    	Keyboard(LPDIRECTINPUT8 pDI, HWND hwnd);
    	~Keyboard(void);
    	bool shutdown();
    	bool read();
    	void clear();
    	bool buttonDown(char key){ return (m_keys[key] & 0x80) ? true : false; }
        bool buttonUp(char key){ return (m_keys[key] & 0x80) ? false : true; }
    private:
    	char m_keys[256];
    	LPDIRECTINPUTDEVICE8  m_pDIDev;
    
    };
    This is my code. It just can't recognize arrow keys, home,end, delete,page up and page down. Maybe there are more.

  5. #5
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    287
    A little observation, sometimes the easyest part is missed: you should call the button down function, hope that it won't execute for itself .
    I do the next: first process the keyboard and mouse operations, then render all; it is in the game's loop. something like:
    Code:
    while(msg.message!=WM_QUIT)
        {
        fMessage=PeekMessage(&msg,NULL,0U,0U,PM_REMOVE);
        if(fMessage)
            {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
            }
        else
            {
            yourkeyboardclasspointer->procKeyb();
            Render();
            }
        }
    and the procKey() should be (the same as before):
    Code:
    void yourkeyboardclass::procKeyb()
    {
    BYTE tec[256];
    m_keyboarddev->GetDeviceState(256,&tec);
    if(tec[DIK_UP] & 0x80) {/*is the up arrow key*/}
    /*do similar with the other keys*/
    }
    if you call that function from the WM_ messages I think that will get a little delay. it works for me.
    Niara

  6. #6
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    I had almost the exact same problem a couple days ago. You need an array of unsigned chars (and make sure the function also takes an unsigned char)
    "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

  7. #7
    Registered User
    Join Date
    Sep 2003
    Posts
    133
    Thanks guys.

    I forgot to mention that I did call the function in my game loop.

    And indeed, it is the unsigned char problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Movement with arrow keys
    By louis_mine in forum C Programming
    Replies: 3
    Last Post: 02-06-2005, 03:35 PM
  2. Interfacing with arrow keys...
    By adityakarnad in forum Game Programming
    Replies: 1
    Last Post: 08-30-2003, 10:25 PM
  3. Ascii code for arrow keys
    By beginner in forum C Programming
    Replies: 1
    Last Post: 11-07-2002, 12:29 PM
  4. msdos arrow keys?
    By seditee in forum Game Programming
    Replies: 3
    Last Post: 05-07-2002, 11:29 PM
  5. Arrow keys
    By Nutshell in forum C Programming
    Replies: 5
    Last Post: 03-27-2002, 10:49 AM

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