📄 ginput.cpp
字号:
#include <dinput.h>
#include <dinputd.h>
#include "ginput.h"
#pragma comment (lib,"dinput8.lib")
#pragma comment (lib,"dxguid.lib")
CGInput::CGInput(HINSTANCE hInstance,HWND hWnd)
{
if(DI_OK!=DirectInput8Create( hInstance,DIRECTINPUT_VERSION,
IID_IDirectInput8,(LPVOID*)&m_pDInput,NULL))
MessageBox(hWnd,"创建DirectInput 对象失败!","ERROR",MB_ICONERROR|MB_OK);
//创建DirectInput8对象
if(DI_OK!=m_pDInput->CreateDevice(GUID_SysKeyboard,&m_pDInputKB,NULL))
MessageBox(hWnd,"创建键盘设备失败!","ERROR",MB_ICONERROR|MB_OK);
//创建键盘设备
if(DI_OK!= m_pDInputKB->SetDataFormat(&c_dfDIKeyboard))
MessageBox(hWnd,"设置键盘数据格式失败!","ERROR",MB_ICONERROR|MB_OK);
//设置数据格式
m_pDInputKB->SetCooperativeLevel(hWnd,DISCL_EXCLUSIVE|DISCL_BACKGROUND);
m_pDInputKB->Acquire(); //如果失败,获取数据时再尝试获取
//if(DI_OK!=m_pDInputKB->Acquire())
// MessageBox(hwnd,"Acquire KeyBoard Failed!","Error",MB_OK|MB_ICONERROR);
//////////////////////////////////////////////////////////////
if(DI_OK!=m_pDInput->CreateDevice(GUID_SysMouse,&m_pDInputMouse,NULL))
MessageBox(hWnd,"创建鼠标设备失败!","ERROR",MB_ICONERROR|MB_OK);
//创建鼠标设备
if(DI_OK!= m_pDInputMouse->SetDataFormat(&c_dfDIMouse))
MessageBox(hWnd,"设置鼠标数据格式失败!","ERROR",MB_ICONERROR|MB_OK);
//设置数据格式
m_pDInputMouse->SetCooperativeLevel(hWnd,DISCL_EXCLUSIVE|DISCL_FOREGROUND);
m_pDInputMouse->Acquire(); //如果失败,获取数据时再尝试获取
// if(DI_OK!=m_pDInputMouse->Acquire())
// MessageBox(m_hwnd,"Acquire Mouse Failed!","Error",MB_OK|MB_ICONERROR);
//设置协调层级
//清除状态
memset(&m_strKeyState,0,sizeof(m_strKeyState));
memset(&m_strKeyStateOld,0,sizeof(m_strKeyState));
memset(&m_MouseState,0,sizeof(m_MouseState));
memset(&m_MouseStateOld,0,sizeof(m_MouseState));
}
CGInput::~CGInput(void)
{
m_pDInputMouse->Unacquire();
m_pDInputKB->Unacquire();
SAFE_RELEASE(m_pDInputMouse);
SAFE_RELEASE(m_pDInputKB);
SAFE_RELEASE(m_pDInput);
}
LRESULT CGInput::Update(void)
{
memcpy(m_strKeyStateOld,m_strKeyState,sizeof(m_strKeyState));
memcpy(&m_MouseStateOld,&m_MouseState,sizeof(m_MouseState));
if(DI_OK!=m_pDInputKB->GetDeviceState(sizeof(m_strKeyState),m_strKeyState))
// MessageBox(NULL,"Failed to GetDeviceState","ERROR",MB_ICONERROR|MB_OK);
{
memset(&m_strKeyState,0,sizeof(m_strKeyState)); //清除上一次的状态
m_pDInputKB->Acquire();
}
if(DI_OK!=m_pDInputMouse->GetDeviceState(sizeof(m_MouseState),&m_MouseState))
{
memset(&m_MouseState,0,sizeof(m_MouseState)); //清除上一次的状态
m_pDInputMouse->Acquire();
}
return DI_OK;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -