天天看點

DirectInput使用示例

最近學習d3d學到DirectInput這塊出了少的問題,現在終于解決了

注意:1。環境 vc6.0+directx2009

2.在vc++工具----選項----目錄,裡面一定要把directx 的庫放到最上面去,因為在vc本身自己的庫裡面也有dinput.h這個頭檔案,剛開沒發現這個可讓 郁悶了好久。。

代碼好下:

 #include "stdafx.h"

#define DIRECTINPUT_VERSION 0x0700

#include <dinput.h>

#pragma comment(lib,"dxguid.lib")

#pragma comment(lib,"dinput.lib")

LPDIRECTINPUT dxInput;

LPDIRECTINPUTDEVICE dxInputDevice;

HINSTANCE m_hinstance;

HWND m_hwnd;

char keyBuffer[256];

BOOL InitKeyBoard()

{

HRESULT hr;

// hr=DirectInput8Create(m_hinstance,DIRECTINPUT_VERSION,IID_IDirectInput,(LPVOID*)&dxInput,0);

hr=DirectInputCreate(m_hinstance,DIRECTINPUT_VERSION,&dxInput,0);

if (FAILED(hr))

{

MessageBox(m_hwnd,"初始失敗1","alert",MB_OK);

return false;

}

hr=dxInput->CreateDevice(GUID_SysKeyboard,&dxInputDevice,0);

if (FAILED(hr))

{

MessageBox(m_hwnd,"初始失敗2","alert",MB_OK);

return false;

}

hr=dxInputDevice->SetDataFormat(&c_dfDIKeyboard);

if (FAILED(hr))

{

MessageBox(m_hwnd,"初始失敗3","alert",MB_OK);

return false;

}

hr=dxInputDevice->SetCooperativeLevel(m_hwnd,DISCL_BACKGROUND|DISCL_NONEXCLUSIVE);

if (FAILED(hr))

{

MessageBox(m_hwnd,"初始失敗4","alert",MB_OK);

return false;

}

hr=dxInputDevice->Acquire();

if (FAILED(hr))

{

MessageBox(m_hwnd,"初始失敗5","alert",MB_OK);

return false;

}

return true;

}

BOOL IsKeyPressed(int key)

{

HRESULT hr;

hr=dxInputDevice->GetDeviceState(sizeof(keyBuffer),(LPVOID)keyBuffer);

if (FAILED(hr))

{

MessageBox(m_hwnd,"初始失敗6","alert",MB_OK);

return false;

}

if (keyBuffer[key] & 0x80)

{

return true;

}

return false;

}

void Release()

{

dxInput->Release();

dxInputDevice->Release();

}

LRESULT CALLBACK Winproc(HWND hwnd,UINT msg,WPARAM wplaram,LPARAM lparam)

{

switch (msg)

{

case WM_CLOSE:

DestroyWindow(hwnd);

PostQuitMessage(0);

break;

default:

return DefWindowProc(hwnd,msg,wplaram,lparam);

}

return wplaram;

}

int APIENTRY WinMain(HINSTANCE hInstance,

HINSTANCE hPrevInstance,

LPSTR lpCmdLine,

int nCmdShow)

{

char classname[]="li";

// TODO: Place code here.

WNDCLASS wndcls;

MSG msg;

wndcls.cbClsExtra=NULL;

wndcls.cbWndExtra=NULL;

wndcls.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);

wndcls.hCursor=LoadCursor(NULL,IDC_ARROW);

wndcls.hIcon=LoadIcon(NULL,IDI_ERROR);

wndcls.hInstance=hInstance;

wndcls.lpfnWndProc=Winproc;

wndcls.lpszClassName=classname;

wndcls.lpszMenuName=NULL;

wndcls.style=CS_HREDRAW | CS_VREDRAW;

RegisterClass(&wndcls);

HWND hwnd;

hwnd=CreateWindow(classname,"my window",WS_OVERLAPPEDWINDOW,200,200,800,500,NULL,NULL,wndcls.hInstance,NULL);

ShowWindow(hwnd,nCmdShow);

UpdateWindow(hwnd);

m_hwnd=hwnd;

m_hinstance=hInstance;

if (InitKeyBoard())

{

while (msg.message!=WM_QUIT)

{

if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))

{

TranslateMessage(&msg);

DispatchMessage(&msg);

}

else

{

if (IsKeyPressed(DIK_LEFT))

{

MessageBox(hwnd,"左","msg",MB_OK);

}

if (IsKeyPressed(DIK_RIGHT))

{

MessageBox(hwnd,"右","msg",MB_OK);

}

if (IsKeyPressed(DIK_DOWN))

{

MessageBox(hwnd,"下","msg",MB_OK);

}

if (IsKeyPressed(DIK_UP))

{

MessageBox(hwnd,"上","msg",MB_OK);

}

}

}

}

Release();

return 0;

}

繼續閱讀