天天看点

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;

}

继续阅读