BOOL KeyboardInit (HWND hWindow, int iMin, int iMax, int iDeadZone) { if (FAILED(m_lpkDI->CreateDevice ( pdidInstance->guidInstance, m_lpkDKB, NULL)) ) { return DIENUM_CONTINUE; } else { return DIENUM_STOP; } if (FAILED(m_lpkDKB->SetDataFormat (&c_dfDIKeyboard))) { return FALSE; } /* Acquire joystick */ HRESULT hRet = m_lpkDKB->Poll (); if (FAILED (hRet)) { hRet = m_lpkDKB->Acquire (); while (hRet == DIERR_INPUTLOST) { hRet = m_lpkDKB->Acquire (); } } return TRUE; }
/* Update Keyboard status */ BOOL KeyboardUpdate (void) { /* Poll the Keyboard */ if (FAILED (m_lpkDKB->Poll ()) ) { /* Acquire Keyboard */ HRESULT hRet = m_lpkDKB->Acquire (); if ((FAILED (hRet)) && (hRet == DIERR_INPUTLOST)) { m_lpkDKB->Acquire (); } else { return FALSE; } } /* Get device data */ if (FAILED (m_lpkDIDevice->GetDeviceState (sizeof (DIJOYSTATE2), &m_kDeviceData)) ) { return FALSE; } return TRUE; }
bool initDIDevice(HINSTANCE hInstance, HWND hWnd) { // Initialisation de DirectInput if(FAILED(DirectInput8Create(hInstance, DIRECTINPUT_VERSION, IID_IDirectInput8, (void **)&g_pDI, NULL))) return false; // Initialisation du clavier if(FAILED(g_pDI->CreateDevice(GUID_SysKeyboard, &g_pKeyboardDevice, NULL))) return false; if(FAILED(g_pKeyboardDevice->SetDataFormat(&c_dfDIKeyboard))) return false; if(FAILED(g_pKeyboardDevice->SetCooperativeLevel(hWnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE))) return false; if(FAILED(g_pKeyboardDevice->Acquire())) return false; // Initialisation de la souris if(FAILED(g_pDI->CreateDevice(GUID_SysMouse, &g_pMouseDevice, NULL))) return false; if(FAILED(g_pMouseDevice->SetDataFormat(&c_dfDIMouse))) return false; if(FAILED(g_pMouseDevice->SetCooperativeLevel(hWnd, DISCL_FOREGROUND | DISCL_EXCLUSIVE))) return false; if(FAILED(g_pMouseDevice->Acquire())) return false; return true; }
bool DirectInput_Init(HWND hwnd) { //initialize DirectInput object DirectInput8Create( GetModuleHandle(NULL), DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&dinput, NULL); //initialize the keyboard dinput->CreateDevice(GUID_SysKeyboard, &dikeyboard, NULL); dikeyboard->SetDataFormat(&c_dfDIKeyboard); dikeyboard->SetCooperativeLevel(hwnd, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND); dikeyboard->Acquire(); //initialize the mouse dinput->CreateDevice(GUID_SysMouse, &dimouse, NULL); dimouse->SetDataFormat(&c_dfDIMouse); dimouse->SetCooperativeLevel(hwnd, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND); dimouse->Acquire(); d3ddev->ShowCursor(false); return true; }
void acquireDIDevice() { if(g_pKeyboardDevice) g_pKeyboardDevice->Acquire(); if(g_pMouseDevice) g_pMouseDevice->Acquire(); }
void DirectInput_Update() { //update mouse dimouse->Poll(); if (!SUCCEEDED(dimouse->GetDeviceState(sizeof(DIMOUSESTATE),&mouse_state))) { //mouse device lose, try to re-acquire dimouse->Acquire(); } //update keyboard dikeyboard->Poll(); if (!SUCCEEDED(dikeyboard->GetDeviceState(256,(LPVOID)&keys))) { //keyboard device lost, try to re-acquire dikeyboard->Acquire(); } //update controllers for (int i=0; i< 4; i++ ) { ZeroMemory( &controllers[i], sizeof(XINPUT_STATE) ); //get the state of the controller XINPUT_STATE state; DWORD result = XInputGetState( i, &state ); //store state in global controllers array if (result == 0) controllers[i] = state.Gamepad; } }
int GutInputRestore(void) { if ( g_pMouse ) g_pMouse->Acquire(); if ( g_pJoystick ) g_pJoystick->Acquire(); if ( g_pKeyboard ) g_pKeyboard->Acquire(); return 1; }
//----------------------------------------------------------------------------- // Name: ReadImmediateData() // Desc: Read the input device's state when in immediate mode and display it. //----------------------------------------------------------------------------- HRESULT ReadImmediateData( HWND hDlg ) { HRESULT hr; TCHAR strNewText[128] = TEXT(""); // Output string DIMOUSESTATE2 dims2; // DirectInput mouse state structure if( NULL == g_pMouse ) return S_OK; // Get the input's device state, and put the state in dims ZeroMemory( &dims2, sizeof(dims2) ); hr = g_pMouse->GetDeviceState( sizeof(DIMOUSESTATE2), &dims2 ); if( FAILED(hr) ) { // DirectInput may be telling us that the input stream has been // interrupted. We aren't tracking any state between polls, so // we don't have any special reset that needs to be done. // We just re-acquire and try again. // If input is lost then acquire and keep trying hr = g_pMouse->Acquire(); while( hr == DIERR_INPUTLOST ) hr = g_pMouse->Acquire(); // Update the dialog text if( hr == DIERR_OTHERAPPHASPRIO || hr == DIERR_NOTACQUIRED ) SetDlgItemText( hDlg, IDC_DATA, TEXT("Unacquired") ); // hr may be DIERR_OTHERAPPHASPRIO or other errors. This // may occur when the app is minimized or in the process of // switching, so just try again later return S_OK; } // The dims structure now has the state of the mouse, so // display mouse coordinates (x, y, z) and buttons. _stprintf( strNewText, TEXT("(X=% 3.3d, Y=% 3.3d, Z=% 3.3d) B0=%c B1=%c B2=%c B3=%c B4=%c B5=%c B6=%c B7=%c"), dims2.lX, dims2.lY, dims2.lZ, (dims2.rgbButtons[0] & 0x80) ? '1' : '0', (dims2.rgbButtons[1] & 0x80) ? '1' : '0', (dims2.rgbButtons[2] & 0x80) ? '1' : '0', (dims2.rgbButtons[3] & 0x80) ? '1' : '0', (dims2.rgbButtons[4] & 0x80) ? '1' : '0', (dims2.rgbButtons[5] & 0x80) ? '1' : '0', (dims2.rgbButtons[6] & 0x80) ? '1' : '0', (dims2.rgbButtons[7] & 0x80) ? '1' : '0'); // Get the old text in the text box TCHAR strOldText[128]; GetDlgItemText( hDlg, IDC_DATA, strOldText, 127 ); // If nothing changed then don't repaint - avoid flicker if( 0 != lstrcmp( strOldText, strNewText ) ) SetDlgItemText( hDlg, IDC_DATA, strNewText ); return S_OK; }
// Acquire our device. Our device might get unacquired for many many reasons, and we need to be able to reacquire it to get input again. // We use this a LOT. inline HRESULT AcquireDevice( LPDIRECTINPUTDEVICE8 lpDirectInputDevice ) { HRESULT hResult = lpDirectInputDevice->Acquire(); while( hResult == DIERR_INPUTLOST ) hResult = lpDirectInputDevice->Acquire(); if( SUCCEEDED( hResult )) lpDirectInputDevice->Poll(); return hResult; }
int GutInputInit(void) { memset(g_pKeyDownFuncs, 0, sizeof(g_pKeyDownFuncs)); memset(g_pKeyUpFuncs, 0, sizeof(g_pKeyUpFuncs)); memset(g_pKeyPressedFuncs, 0, sizeof(g_pKeyPressedFuncs)); int hr; HWND hwnd = GutGetWindowHandleWin32(); HINSTANCE hinst = GutGetWindowInstanceWin32(); hr = DirectInput8Create( hinst, DIRECTINPUT_VERSION, IID_IDirectInput8, (void **)&g_pDI, NULL); if ( FAILED(hr) ) return 0; // create keyboard device hr = g_pDI->CreateDevice( GUID_SysKeyboard, &g_pKeyboard, NULL ); if ( FAILED(hr) ) return 0; if ( g_pKeyboard ) { g_pKeyboard->SetCooperativeLevel(hwnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE ); g_pKeyboard->SetDataFormat(&c_dfDIKeyboard); g_pKeyboard->Acquire(); } // create mouse device hr = g_pDI->CreateDevice(GUID_SysMouse, &g_pMouse, NULL); if ( FAILED(hr) ) return 0; if ( g_pMouse ) { g_pMouse->SetCooperativeLevel(hwnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE ); g_pMouse->SetDataFormat(&c_dfDIMouse2); g_pMouse->Acquire(); } GetCursorPos(&g_op); ScreenToClient(hwnd, &g_op); g_mouse = g_op; // create joystick device hr = g_pDI->EnumDevices(DI8DEVCLASS_GAMECTRL, EnumJoysticksCallback, NULL, DIEDFL_ATTACHEDONLY); if ( FAILED(hr) ) return 0; if ( g_pJoystick ) { g_pJoystick->SetCooperativeLevel(hwnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE ); g_pJoystick->SetDataFormat(&c_dfDIJoystick); g_pJoystick->Acquire(); } return 1; }
void ENGINEAPI DIWindowActivate(BOOL bActive) { if(bDInputActive) { if(bActive) { if(diKeyboard) diKeyboard->Acquire(); if(diMouse) diMouse->Acquire(); } } }
void getState() { if(mouseDevice->GetDeviceState(sizeof(mouseState), (LPVOID)&mouseState ) == DIERR_INPUTLOST) { mouseDevice->Acquire(); } if(keyboardDevice->GetDeviceState(sizeof(keyState), (LPVOID)&keyState ) == DIERR_INPUTLOST) { mouseDevice->Acquire(); } mousePosX += mouseState.lX; mousePosY += mouseState.lY; }
bool OutDevice::Read(LPDIRECTINPUTDEVICE8 dev,void* pBuffer, long lSize) { HRESULT hr; while(true) { dev->Poll(); //轮询 dev->Acquire(); //获取设备控制权 if(SUCCEEDED(hr=dev->GetDeviceState(lSize,pBuffer))) break; if(hr != DIERR_INPUTLOST || hr != DIERR_NOTACQUIRED) return false; if(FAILED(dev->Acquire())) return false; } return true; }
int DInput_Init_Keyboard(void) { // this function initializes the keyboard device // create the keyboard device if (lpdi->CreateDevice(GUID_SysKeyboard, &lpdikey, NULL)!=DI_OK) return(0); // set cooperation level if (lpdikey->SetCooperativeLevel(main_window_handle, DISCL_NONEXCLUSIVE | DISCL_BACKGROUND)!=DI_OK) return(0); // set data format if (lpdikey->SetDataFormat(&c_dfDIKeyboard)!=DI_OK) return(0); // acquire the keyboard if (lpdikey->Acquire()!=DI_OK) return(0); // return success return(1); } // end DInput_Init_Keyboard
int DInput_Init_Mouse(void) { // this function intializes the mouse // create a mouse device if (lpdi->CreateDevice(GUID_SysMouse, &lpdimouse, NULL)!=DI_OK) return(0); // set cooperation level // change to EXCLUSIVE FORGROUND for better control if (lpdimouse->SetCooperativeLevel(main_window_handle, DISCL_NONEXCLUSIVE | DISCL_BACKGROUND)!=DI_OK) return(0); // set data format if (lpdimouse->SetDataFormat(&c_dfDIMouse)!=DI_OK) return(0); // acquire the mouse if (lpdimouse->Acquire()!=DI_OK) return(0); // return success return(1); } // end DInput_Init_Mouse
//============================================================================= // マウスの更新処理 //============================================================================= HRESULT UpdateMouse(void) { HRESULT hr; DIMOUSESTATE2 mouseState; // デバイスからデータを取得 hr = g_pDIDevMouse->GetDeviceState(sizeof(mouseState), &mouseState); if(SUCCEEDED(hr)) { g_mouseStateTrigger.lX = ((g_mouseState.lX ^ mouseState.lX) & mouseState.lX); g_mouseStateTrigger.lY = ((g_mouseState.lY ^ mouseState.lY) & mouseState.lY); g_mouseStateTrigger.lZ = ((g_mouseState.lZ ^ mouseState.lZ) & mouseState.lZ); for(int nCntKey = 0; nCntKey < NUM_MOUSE_BUTTON_MAX; nCntKey++) { g_mouseStateTrigger.rgbButtons[nCntKey] = ((g_mouseState.rgbButtons[nCntKey] ^ mouseState.rgbButtons[nCntKey]) & mouseState.rgbButtons[nCntKey]); } g_mouseState = mouseState; } else { // マウスへのアクセス権を獲得(入力制御開始) g_pDIDevMouse->Acquire(); } return S_OK; }
///////////////////////////////////// // Name: INPMouseInit // Purpose: initialize mouse // if bExclusive, this means // we don't get actual windows // mouse location. Use it on // fullscreen only. // Output: mouse initialized // Return: success if so ///////////////////////////////////// s32 F_API INPMouseInit(void *hMain, u8 bExclusive, iPoint *pBound) { INPMouseDestroy(); HRESULT hr; //create the mouse device hr = g_pDInput->CreateDevice(GUID_SysMouse, &g_pDMouse, NULL); if(FAILED(hr)) { DInputError(hr, L"INPMouseInit"); return RETCODE_FAILURE; } //set mouse device as a REAL mouse hr = g_pDMouse->SetDataFormat(&c_dfDIMouse2); if(FAILED(hr)) { DInputError(hr, L"INPMouseInit"); return RETCODE_FAILURE; } //set up mouse coop level u32 coop = g_coopFlag & ~(INP_EXCLUSIVE | INP_NONEXCLUSIVE); coop |= bExclusive ? INP_EXCLUSIVE : INP_NONEXCLUSIVE; hr = g_pDMouse->SetCooperativeLevel((HWND)hMain, coop); if(FAILED(hr)) { DInputError(hr, L"INPMouseInit"); return RETCODE_FAILURE; } //acquire mouse device g_pDMouse->Acquire(); g_bExclusive = bExclusive; memset(&g_mouseLoc, 0, sizeof(g_mouseLoc)); INPMouseSetBound(pBound); return RETCODE_SUCCESS; }
//------------------------------------------------------ // コントローラー初期化 //------------------------------------------------------ LPDIRECTINPUTDEVICE8 iexInputManager::GetDevice( int n ) { HRESULT hr; LPDIRECTINPUTDEVICE8 lpDevice; // デバイス生成 hr = pDI->CreateDevice( didi[n].guidInstance, &lpDevice, NULL); if( FAILED(hr) ) return NULL; if( lpDevice->SetDataFormat( &c_dfDIJoystick2 ) != DI_OK ) return FALSE; if( lpDevice->SetCooperativeLevel( iexSystem::Window, DISCL_EXCLUSIVE | DISCL_FOREGROUND ) != DI_OK ) return FALSE; // 自動センタリング無効 DIPROPDWORD dipdw; dipdw.diph.dwSize = sizeof(DIPROPDWORD); dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER); dipdw.diph.dwObj = 0; dipdw.diph.dwHow = DIPH_DEVICE; dipdw.dwData = DIPROPAUTOCENTER_OFF; lpDevice->SetProperty( DIPROP_AUTOCENTER, &dipdw.diph ); // 各軸設定 lpDevice->EnumObjects(EnumAxes, lpDevice, DIDFT_AXIS); // 入力制御開始 lpDevice->Acquire(); return lpDevice; }
/// a function that Initializes all direct input stuff void TSRInputSubSystem::Init() { #if defined( WIN32 ) || defined( WIN64 ) if ( FAILED( DirectInput8Create( GetModuleHandle( 0 ), DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&m_lpdi, NULL) ) ) { TSRFatalError( "Error Creating main direct input object" ); } if ( FAILED( m_lpdi->CreateDevice( GUID_SysMouse, &m_lpdimouse, NULL ) ) ) { TSRFatalError( "Error Creating mouse device" ); } if ( FAILED( m_lpdimouse->SetCooperativeLevel( NULL, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE ) ) ) { TSRFatalError( "Error Setting mouse cooperative level" ); } if ( FAILED( m_lpdimouse->SetDataFormat( &c_dfDIMouse ) ) ) { TSRFatalError( "Error setting mouse data fromat" ); } if ( FAILED ( m_lpdimouse->Acquire() ) ) { TSRFatalError( "error aquiring mouse" ); } #endif }
C_RESULT open_dx_keyboard(void) { HRESULT hr; HWND hDlg = GetConsoleHwnd(); // Register with the DirectInput subsystem and get a pointer // to a IDirectInput interface we can use. // Create a DInput object if (g_pDI==NULL) if( VP_FAILED( hr = DirectInput8Create( GetModuleHandle( NULL ), DIRECTINPUT_VERSION, IID_IDirectInput8, ( VOID** )&g_pDI, NULL ) ) ) return hr; // Create the connection to the keyboard device g_pDI->CreateDevice(GUID_SysKeyboard, &fDIKeyboard, NULL); if (fDIKeyboard) { fDIKeyboard->SetDataFormat(&c_dfDIKeyboard); fDIKeyboard->SetCooperativeLevel(hDlg,DISCL_FOREGROUND | DISCL_EXCLUSIVE); fDIKeyboard->Acquire(); } return C_OK; }
static void Mouse_Win32_Poll(void) { if(!mouseTrapped) { // We are not supposed to be reading the mouse right now. return; } // Try to get the mouse state. int tries = 1; BOOL acquired = false; while(!acquired && tries > 0) { HRESULT hr = didMouse->GetDeviceState(sizeof(mstate), &mstate); if(SUCCEEDED(hr)) { acquired = true; } else if(tries > 0) { // Try to reacquire. didMouse->Acquire(); tries--; } } if(!acquired) { // The operation is a failure. memset(&mstate, 0, sizeof(mstate)); } }
int GutReadMouse(GutMouseInfo *info) { if( NULL == g_pMouse ) return 0; // DirectInput mouse state structure DIMOUSESTATE2 dims2; // Get the input's device state, and put the state in dims ZeroMemory( &dims2, sizeof(dims2) ); int hr = g_pMouse->GetDeviceState( sizeof(DIMOUSESTATE2), &dims2 ); if ( FAILED(hr) ) { g_pMouse->Acquire(); g_pMouse->GetDeviceState( sizeof(DIMOUSESTATE2), &dims2 ); } info->x = dims2.lX; info->y = dims2.lY; info->z = dims2.lZ; g_mouse.x += info->x; g_mouse.y += info->y; info->button[0] = dims2.rgbButtons[0] & 0x80 ? 1 : 0; info->button[1] = dims2.rgbButtons[1] & 0x80 ? 1 : 0; info->button[2] = dims2.rgbButtons[2] & 0x80 ? 1 : 0; return 1; }
void getKeyStates( const unsigned char numKeys, const unsigned short* keys, unsigned char* states, unsigned char* modifierMask ) { static BYTE keystate[MAX_KEYS]; // create a static storage for the key-states dinKeyboard->Acquire(); // get access if we don't have it already dinKeyboard->GetDeviceState( MAX_KEYS, (LPVOID)keystate ); // fill keystate with values *modifierMask = 0; if ( keystate[DIK_LSHIFT] & 0x80 ) *modifierMask |= MODIFIER_MASK_SHIFT; if ( keystate[DIK_RSHIFT] & 0x80 ) *modifierMask |= MODIFIER_MASK_SHIFT; if ( keystate[DIK_LCONTROL] & 0x80 ) *modifierMask |= MODIFIER_MASK_CTRL; if ( keystate[DIK_RCONTROL] & 0x80 ) *modifierMask |= MODIFIER_MASK_CTRL; if ( keystate[DIK_LMENU] & 0x80 ) *modifierMask |= MODIFIER_MASK_LALT; if ( keystate[DIK_RMENU] & 0x80 ) *modifierMask |= MODIFIER_MASK_RALT; if ( keystate[DIK_LMENU] & 0x80 ) *modifierMask |= MODIFIER_MASK_LMETA; if ( keystate[DIK_RMENU] & 0x80 ) *modifierMask |= MODIFIER_MASK_RMETA; for ( unsigned short i = 0; i < numKeys; i++ ) { states[i] = ( keystate[keys[i]] & 0x80 ) ? 1 : 0; } }
//DirectInputデバイスの初期化 bool Application::InitDinputDevice() { //DirectInputデバイスを作成 if (FAILED(directInput->CreateDevice(GUID_SysMouse, &dinputDevice, nullptr))) return false; //データフォーマットを設定 if (FAILED(dinputDevice->SetDataFormat(&c_dfDIMouse2))) return false; //協調モードを設定 if (FAILED(dinputDevice->SetCooperativeLevel(hWnd, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND))) return false; // 軸モードを設定 DIPROPDWORD diprop; diprop.diph.dwSize = sizeof(diprop); diprop.diph.dwHeaderSize = sizeof(diprop.diph); diprop.diph.dwObj = 0; diprop.diph.dwHow = DIPH_DEVICE; diprop.dwData = DIPROPAXISMODE_REL; if (FAILED(dinputDevice->SetProperty(DIPROP_AXISMODE, &diprop.diph))) { MessageBox(NULL, "設定に失敗", "Direct Input Error", MB_OK); return false; } // 入力制御開始 dinputDevice->Acquire(); return true; }
//----------------------------------------------------------- HRESULT UpdateMouse() { HRESULT result; // 前回の値保存 DIMOUSESTATE2 lastMouseState = mouseState; // データ取得 result = pMouse->GetDeviceState(sizeof(mouseState),&mouseState); if(SUCCEEDED(result)) { mouseTrigger.lX = mouseState.lX; mouseTrigger.lY = mouseState.lY; mouseTrigger.lZ = mouseState.lZ; // マウスのボタン状態 for(int i=0;i<8;i++) { mouseTrigger.rgbButtons[i] = ((lastMouseState.rgbButtons[i] ^ mouseState.rgbButtons[i]) & mouseState.rgbButtons[i]); } } else // 取得失敗 { // アクセス権を得てみる result = pMouse->Acquire(); } return result; }
//============================================================================= // キーボードの初期化 //============================================================================= HRESULT InitKeyboard(HINSTANCE hInst, HWND hWnd) { HRESULT hr; // デバイスオブジェクトを作成 hr = g_pDInput->CreateDevice(GUID_SysKeyboard, &g_pDIDevKeyboard, NULL); if(FAILED(hr) || g_pDIDevKeyboard == NULL) { MessageBox(hWnd, "キーボードがねぇ!", "警告!", MB_ICONWARNING); return hr; } // データフォーマットを設定 hr = g_pDIDevKeyboard->SetDataFormat(&c_dfDIKeyboard); if(FAILED(hr)) { MessageBox(hWnd, "キーボードのデータフォーマットを設定できませんでした。", "警告!", MB_ICONWARNING); return hr; } // 協調モードを設定(フォアグラウンド&非排他モード) hr = g_pDIDevKeyboard->SetCooperativeLevel(hWnd, (DISCL_FOREGROUND | DISCL_NONEXCLUSIVE)); if(FAILED(hr)) { MessageBox(hWnd, "キーボードの協調モードを設定できませんでした。", "警告!", MB_ICONWARNING); return hr; } // キーボードへのアクセス権を獲得(入力制御開始) g_pDIDevKeyboard->Acquire(); return S_OK; }
//----------------------------------------------------------------------------- // Name: UpdateInputState() // Desc: Get the input device's state and display it. //----------------------------------------------------------------------------- HRESULT UpdateInputState() { HRESULT hr; DIJOYSTATE2 js; // DInput joystick state state.is_valid = false; if (!g_pJoystick) { state.message = "No device at index"; return S_OK; } // Poll the device to read the current state hr = g_pJoystick->Poll(); if (FAILED(hr)) { state.message = "device stream interrupted"; // DInput is telling us that the input stream has been // interrupted. We aren't tracking any state between polls, so // we don't have any special reset that needs to be done. We // just re-acquire and try again. hr = g_pJoystick->Acquire(); //while (hr == DIERR_INPUTLOST) // hr = g_pJoystick->Acquire(); // hr may be DIERR_OTHERAPPHASPRIO or other errors. This // may occur when the app is minimized or in the process of // switching, so just try again later return S_OK; } // Get the input's device state if (FAILED(hr = g_pJoystick->GetDeviceState(sizeof(DIJOYSTATE2), &js))) { state.message = "GetDeviceState failed"; return hr; // The device should have been acquired during the Poll() } state.is_valid = true; state.message = ""; // Axes state.x = js.lX; state.y = js.lY; state.z = js.lZ; state.rx = js.lRx; state.ry = js.lRy; state.rz = js.lRz; state.slider0 = js.rglSlider[0]; state.slider1 = js.rglSlider[1]; // Slider controls state.slider0 = js.rglSlider[0]; state.slider1 = js.rglSlider[1]; // Points of view state.pov0 = js.rgdwPOV[0]; state.pov1 = js.rgdwPOV[1]; state.pov2 = js.rgdwPOV[2]; state.pov3 = js.rgdwPOV[3]; // Buttons for (int i = 0; i < 128; i++) { state.buttons[i] = js.rgbButtons[i]; } return S_OK; }
s32 F_API INPKBUpdate() { if(!g_pDKeyboard) return RETCODE_SUCCESS; u32 i; ////////////////////// //reset buffer for(i=0; i < g_prevKNum; i++) { if(KEYRELEASED(g_buffState[i].ofs)) { g_buffState[i].state = INPUT_UP; g_keyState[g_buffState[i].ofs] = INPUT_UP; } } g_buffStateSize = 0; ////////////////////// HRESULT hr; g_prevKNum = SAMPLE_BUFFER_SIZE; //First, check to see if the keyboard is still working/functioning hr= g_pDKeyboard->GetDeviceData(sizeof(DIDEVICEOBJECTDATA),g_pKbBuff,(LPDWORD)&g_prevKNum,0); if(hr != DI_OK) { g_prevKNum = 0; //did we lose the keyboard? if(hr==DIERR_INPUTLOST || hr==DIERR_NOTACQUIRED) { //we gotta have it back! g_pDKeyboard->Acquire(); /*hr = g_pDKeyboard->Acquire(); if(FAILED(hr)) { DInputError(hr, "_INPKBUpdate"); return RETCODE_FAILURE; } */ return RETCODE_SUCCESS; } else return RETCODE_FAILURE; } //update the keyboard states for(i=0; i < g_prevKNum; i++) { g_buffState[i].ofs = g_pKbBuff[i].dwOfs; g_buffState[i].state = (g_pKbBuff[i].dwData & INPUT_DOWN) ? INPUT_DOWN : INPUT_RELEASED; g_keyState[g_buffState[i].ofs] = g_buffState[i].state; } g_buffStateSize=g_prevKNum; return RETCODE_SUCCESS; }
//============================================================================= // キーボードの更新処理 //============================================================================= HRESULT UpdateKeyboard(void) { HRESULT hr; BYTE aKeyState[NUM_KEY_MAX]; // デバイスからデータを取得 hr = g_pDIDevKeyboard->GetDeviceState(sizeof(aKeyState), aKeyState); if(SUCCEEDED(hr)) { for(int nCntKey = 0; nCntKey < NUM_KEY_MAX; nCntKey++) { g_aKeyStateTrigger[nCntKey] = (g_aKeyState[nCntKey] ^ aKeyState[nCntKey]) & aKeyState[nCntKey]; g_aKeyStateRelease[nCntKey] = (g_aKeyState[nCntKey] ^ aKeyState[nCntKey]) & ~aKeyState[nCntKey]; if(aKeyState[nCntKey]) { g_aKeyStateRepeatCnt[nCntKey]++; if(g_aKeyStateRepeatCnt[nCntKey] < g_nCountWaitRepeat) { if(g_aKeyStateRepeatCnt[nCntKey] == 1 || g_aKeyStateRepeatCnt[nCntKey] >= g_nCountWaitRepeat) { g_aKeyStateRepeat[nCntKey] = aKeyState[nCntKey]; } else { g_aKeyStateRepeat[nCntKey] = 0; } } else { if(((g_aKeyStateRepeatCnt[nCntKey] - g_nCountWaitRepeat) % g_nIntervalRepeat) == 0) { g_aKeyStateRepeat[nCntKey] = aKeyState[nCntKey]; } else { g_aKeyStateRepeat[nCntKey] = 0; } } } else { g_aKeyStateRepeatCnt[nCntKey] = 0; g_aKeyStateRepeat[nCntKey] = 0; } g_aKeyState[nCntKey] = aKeyState[nCntKey]; } } else { // キーボードへのアクセス権を取得 g_pDIDevKeyboard->Acquire(); } return S_OK; }
//----------------------------------------------------------------------------- // Name: MainDialogProc // Desc: Handles dialog messages //----------------------------------------------------------------------------- INT_PTR CALLBACK MainDlgProc( HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam ) { UNREFERENCED_PARAMETER( lParam ); switch( msg ) { case WM_INITDIALOG: if( FAILED( InitDirectInput( hDlg ) ) ) { MessageBox( NULL, TEXT( "Error Initializing DirectInput" ), TEXT( "DirectInput Sample" ), MB_ICONERROR | MB_OK ); EndDialog( hDlg, 0 ); } // Set a timer to go off 30 times a second. At every timer message // the input device will be read SetTimer( hDlg, 0, 1000 / 30, NULL ); return TRUE; case WM_ACTIVATE: if( WA_INACTIVE != wParam && g_pJoystick ) { // Make sure the device is acquired, if we are gaining focus. g_pJoystick->Acquire(); } return TRUE; case WM_TIMER: // Update the input device every timer message if( FAILED( UpdateInputState( hDlg ) ) ) { KillTimer( hDlg, 0 ); MessageBox( NULL, TEXT( "Error Reading Input State. " ) \ TEXT( "The sample will now exit." ), TEXT( "DirectInput Sample" ), MB_ICONERROR | MB_OK ); EndDialog( hDlg, TRUE ); } return TRUE; case WM_COMMAND: switch( LOWORD( wParam ) ) { case IDCANCEL: EndDialog( hDlg, 0 ); return TRUE; } case WM_DESTROY: // Cleanup everything KillTimer( hDlg, 0 ); FreeDirectInput(); return TRUE; } return FALSE; // Message not handled }