⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ckeyboard.cpp

📁 一个2D的赛车游戏 在VC下运行 开发库DIRECTX8.1以上
💻 CPP
字号:
// CMAIN LIB - APPLICATION AND DIRECT WRAPPER
//
// Written by Mauricio Teichmann Ritter
//
// Copyright (C) 2002, Brazil. All rights reserved.
// 
//

// cKeyboard.cpp: implementation of the cKeyboard class.
//
//////////////////////////////////////////////////////////////////////

#include "cApplication.h"
#include "cKeyboard.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

cKeyboard::cKeyboard()
{

}

cKeyboard::~cKeyboard()
{

}

BOOL cKeyboard::Create()
{

	cInputDevice::Create();

	HRESULT	hRet;
	

	hRet = m_lpDI->CreateDevice(GUID_SysKeyboard, &m_lpDIKeyboard, NULL); 
	if FAILED(hRet) { 
		Destroy();
		return FALSE; 
	} 

	hRet = m_lpDIKeyboard->SetDataFormat(&c_dfDIKeyboard); 
	if FAILED(hRet) { 
		Destroy();
		return FALSE; 
	} 

	
    hRet = m_lpDIKeyboard->SetCooperativeLevel(GetMainApp()->GetMainWnd(), 
                             DISCL_FOREGROUND | DISCL_NONEXCLUSIVE); 
    if FAILED(hRet) 
    { 
        Destroy();
        return FALSE; 
    } 
 
    // Get access to the input device. 
    hRet = m_lpDIKeyboard->Acquire(); 
    if FAILED(hRet) 
    { 
        Destroy(); 
        return FALSE; 
    }

	m_KbdBuffer = (char*) malloc(256);

	return TRUE;

}

void cKeyboard::Destroy()
{
	if(m_lpDIKeyboard != NULL){
		m_lpDIKeyboard->Unacquire();
		m_lpDIKeyboard->Release();
		m_lpDIKeyboard = NULL;
	}

	cInputDevice::Destroy();

	free(m_KbdBuffer);
}

void cKeyboard::Process()
{
    HRESULT  hRet; 
	// Clear upo the buffer
	memset((LPVOID)m_KbdBuffer,0, 256);

	hRet = m_lpDIKeyboard->GetDeviceState(256,(LPVOID)m_KbdBuffer); 
    if FAILED(hRet) 
    { 
        if(hRet == DIERR_INPUTLOST)
		{
				// Try to Reaquire;
			hRet = m_lpDIKeyboard->Acquire(); 
			if FAILED(hRet) 
			{ 
				Destroy(); 
				return; 
			}
			// Process Keyboard input again
			Process();
		}
		DXTRACE_MSG("FAILED TO RETRIEVE KEYBOARD INPUT!");
        return; 
    } 
}

BOOL cKeyboard::CheckKey(const int cKey)
{
	//#define KEYDOWN(name, key) (name[key] & 0x80) 
	char* pKbdBuffer = NULL;

	pKbdBuffer = m_KbdBuffer;
	pKbdBuffer+=cKey;
	return (*pKbdBuffer & 0x80);
}

LPDIRECTINPUTDEVICE8 cKeyboard::m_lpDIKeyboard = NULL;
char* cKeyboard::m_KbdBuffer = NULL;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -