• ### Announcements

#### Archived

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

# DirectInput :: Which key was pressed?

## Recommended Posts

Hi, I''m trying to implement a menu where the user can change controls. To do this I need a way to know if a key has been pressed and if so, which key (or mouse/joystick button)? Do you know of a slick way of achieving that with DirectInput? Do I have to use buffered input? Thanks.

##### Share on other sites
DirectInput was really made for fast controls. It can handle menu selection, but that is not what it was meant for. It will be much easier if you just use standard Win32 messages for your menus.

DirectInput immediate mode is so fast that you really can''t use it for menus at all. If you want to use DirectInput, you''ll need to use buffered mode. But in general, Win32 messages are the easiest as well as the cleanest solution to this.

neneboricua

##### Share on other sites
I have no problem with the speed, my menu is working fine, I just want to know how to retrieve which key/button was pressed. Does Win32 messages handle joystick input?

##### Share on other sites
quote:

Does Win32 messages handle joystick input?

I''m 99.9% sure it does not. You need to interface with the joystick drivers to get joystick data, something that DirectInput does for you ( I think that''s how it works ).

Stay Clausal,

Red Sodium

##### Share on other sites
When DirectX_Event is triggered by a button press, you have to scan the array of buttons and determine which one was pressed. If multiple buttons are pressed at once, I guess you would automatically stop at the first one. How you handle that is up to you.

As far as I know, DirectInput cannot and will not tell you specifically which button was pressed. It will tell you if a button was pressed, but you have to figure out which one it was.

##### Share on other sites
DirectInput is probably what you want to use. It isn''t too difficult to use. Take a look at the DirectInput samples. Here is some code that handles mouse input to get you started.
static void InitializeMouse( HINSTANCE hInstance, HWND hWnd ){	OutputDebugString( "***InitializeMouse\n" );    HRESULT hr;    hr = DirectInput8Create( hInstance, DIRECTINPUT_VERSION, IID_IDirectInput8, (VOID**)&s_pDI, NULL );	assert_succeeded( hr );    	hr = s_pDI->CreateDevice( GUID_SysMouse, &s_pMouseDevice, NULL );	assert_succeeded( hr );        hr = s_pMouseDevice->SetDataFormat( &c_dfDIMouse2 );	assert_succeeded( hr );        hr = s_pMouseDevice->SetCooperativeLevel( hWnd, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND );	assert_succeeded( hr );    s_pMouseDevice->Acquire();}static void ShutDownMouse(){	OutputDebugString( "***ShutDownMouse\n" );	if( s_pMouseDevice )	{        s_pMouseDevice->Unacquire();	}        SAFE_RELEASE( s_pMouseDevice );    SAFE_RELEASE( s_pDI );}static void UpdateMouse( __int64 time ){    HRESULT       hr;    DIMOUSESTATE2 dims2;      // DirectInput mouse state structure    if ( s_pMouseDevice == NULL )	{        return;	}        memset( &dims2, 0, sizeof(dims2) );    hr = s_pMouseDevice->GetDeviceState( sizeof(DIMOUSESTATE2), &dims2 );    if ( FAILED( hr ) )     {        hr = s_pMouseDevice->Acquire();        while ( hr == DIERR_INPUTLOST )		{            hr = s_pMouseDevice->Acquire();		}        return;     }	s_pCamera->Yaw( s_MouseSensitivity * dims2.lX );	s_pCamera->Pitch( s_MouseSensitivity * dims2.lY );	s_pCamera->Roll( s_MouseSensitivity * dims2.lZ );}

##### Share on other sites
You should use DirectInput action mapping. What you want to achieve is what action mapping was designed for.

##### Share on other sites
Thanks all for your tips, though I think I came up with a good method last night which I have yet to implement. If that turns out as a failure I will try your suggestions. Thanks again.

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627657
• Total Posts
2978472

• 10
• 12
• 22
• 13
• 33