2 * Copyright (c) 2007-2010 SlimDX Group
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 #include "../stack_array.h"
28 #include "../ComObject.h"
29 #include "../Utilities.h"
31 #include "DirectInput.h"
32 #include "DirectInputException.h"
36 using namespace System;
37 using namespace System::Collections::Generic;
38 using namespace System::Windows::Forms;
39 using namespace System::Runtime::InteropServices;
45 Keyboard::Keyboard( DirectInput^ directInput ) : Device( directInput, Utilities::ConvertNativeGuid( GUID_SysKeyboard ) )
47 HRESULT hr = InternalPointer->SetDataFormat( &c_dfDIKeyboard );
48 if( RECORD_DINPUT( hr ).IsFailure )
49 throw gcnew DirectInputException( Result::Last );
52 IList<KeyboardState^>^ Keyboard::GetBufferedData()
54 DWORD size = INFINITE;
55 HRESULT hr = InternalPointer->GetDeviceData( sizeof( DIDEVICEOBJECTDATA ), NULL, &size, DIGDD_PEEK );
56 if( RecordError( hr ).IsFailure )
59 List<KeyboardState^>^ list = gcnew List<KeyboardState^>( size );
63 stack_array<DIDEVICEOBJECTDATA> data = stackalloc( DIDEVICEOBJECTDATA, size );
64 hr = InternalPointer->GetDeviceData( sizeof( DIDEVICEOBJECTDATA ), &data[0], &size, 0 );
65 if( RecordError( hr ).IsFailure )
71 for( unsigned int i = 0; i < size; i++ )
73 KeyboardState^ result = gcnew KeyboardState();
74 result->UpdateKey( data[i].dwOfs, data[i].dwData > 0 );
75 result->TimeStamp = data[i].dwTimeStamp;
83 Result Keyboard::GetCurrentState( KeyboardState^% data )
86 HRESULT hr = InternalPointer->GetDeviceState( 256, keys );
87 if( RecordError( hr ).IsFailure )
90 data->UpdateKeys( keys, 256 );
95 KeyboardState^ Keyboard::GetCurrentState()
97 KeyboardState^ result = gcnew KeyboardState();
98 GetCurrentState( result );
102 ObjectProperties^ Keyboard::GetObjectPropertiesByName( String^ name )
104 return gcnew ObjectProperties( InternalPointer, name, KeyboardState::typeid );
107 DeviceObjectInstance Keyboard::GetObjectInfoByName( String^ name )
109 DIDEVICEOBJECTINSTANCE di;
110 di.dwSize = sizeof( DIDEVICEOBJECTINSTANCE );
112 HRESULT hr = InternalPointer->GetObjectInfo( &di, Marshal::OffsetOf( KeyboardState::typeid, name ).ToInt32(), DIPH_BYUSAGE );
113 if( RECORD_DINPUT( hr ).IsFailure )
114 return DeviceObjectInstance();
116 return DeviceObjectInstance( di );