[DirectX][DirectInput]Summarize:Using the Keyboard

Step 1: Creating the DirectInput Object

// HINSTANCE  g_hinst; // initialized earlier
HRESULT         hr;
LPDIRECTINPUT8  g_lpDI;

hr = DirectInput8Create(g_hinst, DIRECTINPUT_VERSION,
        IID_IDirectInput8, (void**)&g_lpDI, NULL);
if FAILED(hr)
{
    // DirectInput not available; take appropriate action
}

Step 2: Creating the DirectInput Keyboard Device

HRESULT               hr; 
LPDIRECTINPUTDEVICE8  g_lpDIDevice 

hr = g_lpDI->CreateDevice(GUID_SysKeyboard, &g_lpDIDevice, NULL); 
if FAILED(hr) { 
    DI_Term(); 
    return FALSE; 
} 

Step 3: Setting the Keyboard Data Format

 

hr = g_lpDIDevice->SetDataFormat(&c_dfDIKeyboard); if FAILED(hr) { DI_Term(); return FALSE; }

 

Step 4: Setting the Keyboard Behavior  

 

hr = g_lpDIDevice->SetCooperativeLevel(g_hwndMain, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE); if FAILED(hr) { DI_Term(); return FALSE; }

 

Step 5: Gaining Access to the Keyboard

 

if (g_lpDIDevice) g_lpDIDevice->Acquire();

 

Step 6: Retrieving Data from the Keyboard

void WINAPI ProcessKBInput() 
{ 
    #define KEYDOWN(name, key) (name[key] & 0x80) 

 	char     buffer[256]; 
	HRESULT  hr; 

 
    hr = g_lpDIDevice->GetDeviceState(sizeof(buffer),(LPVOID)&buffer); 
	if FAILED(hr) 
	{ 
		// If it failed, the device has probably been lost. 
		// Check for (hr == DIERR_INPUTLOST) 
		// and attempt to reacquire it here. 
		return; 
	} 

 	// Turn the spaceship right or left 
    if (KEYDOWN(buffer, DIK_RIGHT)); 
		// Turn right. 
	else if(KEYDOWN(buffer, DIK_LEFT)); 
		// Turn left. 

 	// Thrust or stop the spaceship 
	if (KEYDOWN(buffer, DIK_UP)) ; 
		// Move the spaceship forward. 
	else if (KEYDOWN(buffer, DIK_DOWN)); 
		// Stop the spaceship. 
} 
 

Step 7: Closing Down the DirectInput System

When an application is about to close, it should destroy all Microsoft® DirectInput® objects. This is a three-step process. 
  
  

 

Sample Function 1: DI_Init

/* The following variables are presumed to be initialized: HINSTANCE g_hinst; // application instance HWND g_hwndMain; // application window */ LPDIRECTINPUT8 g_lpDI; LPDIRECTINPUTDEVICE8 g_lpDIDevice; BOOL WINAPI DI_Init() { HRESULT hr; // Create the DirectInput object. hr = DirectInput8Create(g_hinst, DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&g_lpDI, NULL); if FAILED(hr) return FALSE; // Retrieve a pointer to an IDirectInputDevice8 interface hr = g_lpDI->CreateDevice(GUID_SysKeyboard, &g_lpDIDevice, NULL); if FAILED(hr) { DI_Term(); return FALSE; } // Now that you have an IDirectInputDevice8 interface, get // it ready to use. // Set the data format using the predefined keyboard data // format provided by the DirectInput object for keyboards. hr = g_lpDIDevice->SetDataFormat(&c_dfDIKeyboard); if FAILED(hr) { DI_Term(); return FALSE; } // Set the cooperative level hr = g_lpDIDevice->SetCooperativeLevel(g_hwndMain, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE); if FAILED(hr) { DI_Term(); return FALSE; } // Get access to the input device. hr = g_lpDIDevice->Acquire(); if FAILED(hr) { DI_Term(); return FALSE; } return TRUE; }

Sample Function 2: DI_Term

/* The following variables are presumed initialized: LPDIRECTINPUT8 g_lpDI; LPDIRECTINPUTDEVICE8 g_lpDIDevice; */ void WINAPI DI_Term() { if (g_lpDI) { if (g_lpDIDevice) { // Always unacquire device before calling Release(). g_lpDIDevice->Unacquire(); g_lpDIDevice->Release(); g_lpDIDevice = NULL; } g_lpDI->Release(); g_lpDI = NULL; } }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值