#### Archived

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

# Jim Adam's RPG book and DirectInput

## Recommended Posts

I can''t get the directional keys to work for some reason ( DIK_LEFT, DIK_RIGHT, etc ). Here''s the code, edited a little bit, but it still didn''t work with his version off the cd. I''ve also searched around for the answer before posting, and couldn''t find anything. I was wondering if anyone was having the same problem as I was.
HRESULT CInputDevice::Read( )
{
HRESULT	hr;
int		BufferSizes[ 3 ];
int		i;

BufferSizes[ 0 ] = 256;
BufferSizes[ 1 ] = sizeof( DIMOUSESTATE );
BufferSizes[ 2 ] = sizeof( DIJOYSTATE );

// Error checking

if( m_pDIDevice == NULL )								return E_FAIL;
if( m_Type < 1 || m_Type > 3 )							return E_FAIL;

while( 1 )
{
// Poll input device

m_pDIDevice->Poll( );

// Read in the state

if( SUCCEEDED( hr = m_pDIDevice->GetDeviceState( BufferSizes[ m_Type - 1 ], ( LPVOID )&m_State ) ) )
break;

// Return a failure on an unknown error

if( hr != DIERR_INPUTLOST && hr != DIERR_NOTACQUIRED )
return E_FAIL;

// Reaquire and the loop will go again

if( FAILED( m_pDIDevice->Acquire( ) ) )
return E_FAIL;
}

// Set the new mouse coordinates

if( m_Type == MOUSE )
{
// If it is windowed mode, get the mouse coordinates from windows

if( m_fWindowed == TRUE )
{
POINT pt;
GetCursorPos( &pt );
ScreenToClient( m_pInput->GethWnd( ), &pt );
m_nXPos = pt.x;
m_nYPos = pt.y;
}
else
{
m_nXPos = m_MouseState->lX;
m_nYPos = m_MouseState->lY;
}
}

switch( m_Type )
{
case KEYBOARD:
for( i = 0; i < 256; i++ )
{
if( !m_State[ i ] & 0x80 )
m_fLocks[ i ] = FALSE;
}
break;

case MOUSE:
for( i = 0; i < 4; i++ )
{
if( !( m_MouseState->rgbButtons[ i ] ) )
m_fLocks[ i ] = FALSE;
}
break;

case JOYSTICK:
for( i = 0; i < 32; i++ )
{
if( !( m_JoystickState->rgbButtons[ i ] ) )
m_fLocks[ i ] = FALSE;
}
break;
}

return S_OK;
}


##### Share on other sites
Change the definition of the GetKeyState (and related) functions to take an unsigned char instead of a char. The values for many of the DIK_* defines are 128 and above. A char (which is signed) will treat values greater than 127 as negative. This causes problems when indexing into the keyboard data buffer. By chaning the type of the key parameter to unsigned char, you get positive values and all is well.

##### Share on other sites
There''s a member in CInputDevice called:

char m_State[ 256 ];

that holds all the states of each key. I tried changing it to unsinged char and it still didn''t work. Also, Jim Adams'' core code didn''t contain an unsigned char but still worked with arrow keys. Your suggestion sounds very plausible to my probably (and it''s probably right), but why might it still not work? I''ll keep looking at it... Anyone else have any ideas?

##### Share on other sites
You need to change the parameter to the GetKeyState function. It is being used as an index into the char array. It has nothing to do with the type of the array.

If you compile Jim''s code in Release mode, I suspect that it will stop working. (I think someone mentioned that in another thread a while ago).

Trust me, changing the declaration of GetKeyState to:

BOOL GetKeyState(unsigned char Num);

will solve the problem. (You could use "int Num" as well.)

##### Share on other sites
Either that or add the /J compiler option as the Appendix suggests.

##### Share on other sites
Sheesh don't I feel like a dumbass. This is the first time I had an error relating to configuring the compiler... and I kept thinking how other people were so stupid for running into such errors Well thanks Jim... heheh hope this works.

EDIT: Woo it works. Thanks for all the help guys

[edited by - coufu on July 2, 2003 12:36:52 AM]

• ### Forum Statistics

• Total Topics
628383
• Total Posts
2982384

• 10
• 9
• 15
• 24
• 11