Better Keyboard Controls

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
ge0rg
Posts: 43
Joined: January 31st, 2014, 4:13 pm

Better Keyboard Controls

Post by ge0rg » February 5th, 2014, 7:21 pm

Hey guys,
in one of his tutorials, chili spoke about the fact that, at least up to the point where I've watched (around Lesson 15) the keybard controls are clumy in the sense that when you push two keys simultaneously or in quick succession, you don't get the response you expect.
I'm working on a snake clone and this is proving to be quite an issue ;).
How do I work around this?

MrGodin
Posts: 721
Joined: November 30th, 2013, 7:40 pm
Location: Merville, British Columbia Canada

Re: Better Keyboard Controls

Post by MrGodin » February 5th, 2014, 7:41 pm

heres a little something i came up with that i "borrowed" from DXUT camera controls, works well for me, although, it implements a lot different then Chili's keyboard routines


Header ..

Code: Select all


enum MoveKeys
{
    MOVE_LEFT = 0,
    MOVE_RIGHT,
    MOVE_UP,
    MOVE_DOWN,
    FIRE_SHOT,
	DROP_BOMB,
	START_GAME,
	PAUSE_GAME,
	CTRL_KEY,
	MAX_KEYS,
    MOVE_UNKNOWN     = 0xFF
};

#define KEY_WAS_DOWN_MASK 0x80
#define KEY_IS_DOWN_MASK  0x01

class kbd
{
private :
	BYTE _LastKeyPressed;
	BYTE _CurrentKeyPressed;

	int m_cKeysDown;            
	BYTE   m_aKeys[MAX_KEYS];
    
    bool IsKeyDown( BYTE key ) const { return( (key & KEY_IS_DOWN_MASK) == KEY_IS_DOWN_MASK ); }
    bool WasKeyDown( BYTE key ) const { return( (key & KEY_WAS_DOWN_MASK) == KEY_WAS_DOWN_MASK ); }
public:
	kbd()
	{
		ZeroMemory( m_aKeys, sizeof( BYTE ) * MAX_KEYS );
	}
	kbd( kbd& kb)
	:
	m_cKeysDown(0)
	{
		
		ZeroMemory( m_aKeys, sizeof( BYTE ) * MAX_KEYS );
	}
	virtual MoveKeys  MapKey( UINT nKey );
   
	bool KeyLeftDown()const{return IsKeyDown(m_aKeys[ MOVE_LEFT]);};		    
	bool KeyRightDown()const{ return IsKeyDown(m_aKeys[ MOVE_RIGHT]); };
	bool KeyUpDown()const{ return IsKeyDown(m_aKeys[ MOVE_UP]); };
	bool KeyDownDown()const{ return IsKeyDown(m_aKeys[ MOVE_DOWN]); };
	bool KeyShotDown()const{ return IsKeyDown(m_aKeys[ FIRE_SHOT]); };
	bool KeyStartDown()const{return IsKeyDown(m_aKeys[START_GAME]);};
	bool KeyPauseDown()const{return IsKeyDown(m_aKeys[PAUSE_GAME]);};
	bool KeyCtrlDown()const{return IsKeyDown(m_aKeys[CTRL_KEY]);};
	bool KeyDropBomb()const{return IsKeyDown(m_aKeys[DROP_BOMB]);};
	
    
	void GetMessages( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

	BYTE LastKey(){return _LastKeyPressed;};
	BYTE CurrentKey(){return _CurrentKeyPressed;};
	void FlushLastKey(){_LastKeyPressed = 0x00;};
	


};

CPP File

Code: Select all

#include "Keyboard1.h"

MoveKeys kbd::MapKey( UINT nKey )
{
   
    switch( nKey )
    {
        
        case VK_LEFT:
            return MOVE_LEFT;
        case VK_RIGHT:
            return MOVE_RIGHT;
        case VK_UP:
            return MOVE_UP;
        case VK_DOWN:
            return MOVE_DOWN;
        case 'S':
            return FIRE_SHOT;
		case 'O':
			return START_GAME;
		case 'P':
			return PAUSE_GAME;
		case VK_CONTROL:
			return CTRL_KEY;
		case '1':
			return DROP_BOMB;
    }

    return MOVE_UNKNOWN;
}

void kbd::GetMessages( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{

	switch(uMsg)
	{
	case WM_KEYDOWN:
		{
		
            MoveKeys mappedKey = MapKey( ( UINT )wParam );
            if( mappedKey != MOVE_UNKNOWN )
            {
                if( FALSE == IsKeyDown( m_aKeys[mappedKey] ) )
                {
                    m_aKeys[ mappedKey ] = KEY_WAS_DOWN_MASK | KEY_IS_DOWN_MASK;
                    ++m_cKeysDown;
                }
            }
            break;
		//_CurrentKeyPressed = wParam;
		
	}
	case WM_KEYUP:
		{
		 
            MoveKeys mappedKey = MapKey( ( UINT )wParam );
            if( mappedKey != MOVE_UNKNOWN && ( DWORD )mappedKey < 8 )
            {
                m_aKeys[ mappedKey ] &= ~KEY_IS_DOWN_MASK;
                --m_cKeysDown;
            }
            
		_LastKeyPressed = wParam;
		_CurrentKeyPressed = 0x00;
		break;
		}

	
	};
};
Curiosity killed the cat, satisfaction brought him back

User avatar
LuX
Posts: 1492
Joined: April 22nd, 2012, 12:33 pm
Location: Finland

Re: Better Keyboard Controls

Post by LuX » February 5th, 2014, 7:48 pm

The array method chili shows in the later tutorials should be one of the easiest and best to implement. Optionally DirectInput, but it's nearly identical...
ʕ •ᴥ•ʔ

User avatar
bshivam2001
Posts: 214
Joined: July 27th, 2013, 6:57 am
Location: India

Re: Better Keyboard Controls

Post by bshivam2001 » February 5th, 2014, 8:06 pm

Pretty much what LuX said, Chili shows an update on the Keyboard in Intermidiate Lesson 7, if I am not wrong.....
'If you can't make it good, at least make it look good'

Post Reply