📄 keycontrol.cpp
字号:
// KeyControl.cpp: implementation of the JFZKeyControl class.
//
//////////////////////////////////////////////////////////////////////
#include "KeyControl.h"
#include "GameDoc.h"
#include "GameView.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CKeyControl::CKeyControl()
{
}
CKeyControl::~CKeyControl()
{
}
CKeyControl* CKeyControl::CreateInstance(CGameDoc* pDoc)
{
// 创建新的CBControl实例
CKeyControl* pObj = new CKeyControl;
// 如果实例创建成功,调用初始化函数
if (pObj)
{
// 设置关联文档
pObj->SetDocument(pDoc);
// 初始化对象
if (!pObj->InitData())
{
pObj->Release();
pObj = NULL;
}
}
// 返回实例指针
return pObj;
}
bool CKeyControl::InitData()
{
int i = 0;
for (i = 0; i < 15; i++)
m_nKeyPress[i] = 0;
for (i = 0;i < 15; i++)
m_bKeyPress[i] = false;
m_nAnyKeyStatus = 0;
return true;
}
void CKeyControl::Release()
{
FreeData();
if (this)
delete this;
}
void CKeyControl::FreeData()
{
for (int i =0; i < 15; i++)
m_nKeyPress[i] = 0;
m_nAnyKeyStatus = 0;
}
uint16 CKeyControl::GetKeyHitCounts(EKEYCODE eKey)
{
int nCount = m_nKeyPress[eKey];
return nCount;
}
void CKeyControl::ReleaseKeyPress(EKEYCODE eKey, int nHitCount)
{
if (m_nKeyPress[eKey] > nHitCount)
m_nKeyPress[eKey] -= nHitCount;
else
m_nKeyPress[eKey] = 0;
}
bool CKeyControl::HandleKeyPress(AEEEvent eCode, uint16 wParam, uint32 dwParam)
{
// 只要有键,就记录状态
m_nAnyKeyStatus |= eCode;
switch ((AVKType)wParam)
{
// 0,1,3,7,9,*,#键缺省处理,不作任何响应
case AVK_0:
case AVK_STAR:
case AVK_POUND:
return TRUE;
case AVK_1:
SetKeyPress(EKEY_KEY1);
return true;
case AVK_2:
SetKeyPress(EKEY_KEY2);
return true;
case AVK_3:
SetKeyPress(EKEY_KEY3);
return true;
case AVK_4:
SetKeyPress(EKEY_KEY4);
return true;
case AVK_6:
SetKeyPress(EKEY_KEY6);
return true;
case AVK_7:
SetKeyPress(EKEY_KEY7);
return true;
case AVK_8:
SetKeyPress(EKEY_KEY8);
return true;
case AVK_9:
SetKeyPress(EKEY_KEY9);
return true;
case AVK_UP:
SetKeyPress(EKEY_UP);
return TRUE;
case AVK_DOWN:
SetKeyPress(EKEY_DOWN);
return TRUE;
case AVK_LEFT:
SetKeyPress(EKEY_LEFT);
return TRUE;
case AVK_RIGHT:
SetKeyPress(EKEY_RIGHT);
return TRUE;
case AVK_5:
case AVK_SELECT:
SetKeyPress(EKEY_SELECT);
return TRUE;
case AVK_CLR:
SetKeyPress(EKEY_CLR);
m_nAnyKeyStatus = 0;
return TRUE;
default:
m_nAnyKeyStatus = 0;
return FALSE;
};
// 没有处理事件
return true;
}
bool CKeyControl::HandleKeyRelease(AEEEvent eCode, uint16 wParam, uint32 dwParam)
{
switch ((AVKType)wParam)
{
// 0,1,3,7,9,*,#键缺省处理,不作任何响应
case AVK_0:
case AVK_STAR:
case AVK_POUND:
return TRUE;
case AVK_1:
SetKeyRelease(EKEY_KEY1);
return true;
case AVK_2:
SetKeyRelease(EKEY_KEY2);
return true;
case AVK_3:
SetKeyRelease(EKEY_KEY3);
return true;
case AVK_4:
SetKeyRelease(EKEY_KEY4);
return true;
case AVK_6:
SetKeyRelease(EKEY_KEY6);
return true;
case AVK_7:
SetKeyRelease(EKEY_KEY7);
return true;
case AVK_8:
SetKeyRelease(EKEY_KEY8);
return true;
case AVK_9:
SetKeyRelease(EKEY_KEY9);
return true;
case AVK_UP:
SetKeyRelease(EKEY_UP);
return TRUE;
case AVK_DOWN:
SetKeyRelease(EKEY_DOWN);
return TRUE;
case AVK_LEFT:
SetKeyRelease(EKEY_LEFT);
return TRUE;
case AVK_RIGHT:
SetKeyRelease(EKEY_RIGHT);
return TRUE;
case AVK_5:
case AVK_SELECT:
SetKeyRelease(EKEY_SELECT);
return TRUE;
case AVK_CLR:
SetKeyRelease(EKEY_CLR);
return TRUE;
// 缺省标记没有处理
default:
return FALSE;
};
// 没有处理事件
return true;
}
void CKeyControl::SetKeyPress(EKEYCODE eKey)
{
m_nKeyPress[eKey]++;
}
void CKeyControl::SetKeyRelease(EKEYCODE eKey)
{
m_nKeyPress[eKey] = 0;
if (m_pDoc->m_pGameView)
{
if (m_pDoc->m_pGameView->m_nCurGameStatus == 1)// && m_pDoc->m_pGameView->m_tPlayer.nLife > 0)
{
if (eKey == EKEY_LEFT)
{
if (m_pDoc->m_pGameView->m_tPlayer.m_nCurDoing == 1)
m_pDoc->m_pGameView->m_tPlayer.m_nCurDoing = 0;
m_pDoc->m_pGameView->m_bMoveLeft = FALSE;
SetKeyPressStatus(eKey, false);
return;
}
else if (eKey == EKEY_RIGHT)
{
if (m_pDoc->m_pGameView->m_tPlayer.m_nCurDoing == 1)
m_pDoc->m_pGameView->m_tPlayer.m_nCurDoing = 0;
SetKeyPressStatus(eKey, false);
return;
}
else if (eKey == EKEY_UP)
{
// if (m_pDoc->m_pGameView->m_tPlayer.nCurDoing < 5)//!= 5)
// m_pDoc->m_pGameView->m_tPost.nPostCurDoing = 0;
SetKeyPressStatus(eKey, false);
return;
}
// else if (eKey == EKEY_DOWN && m_pDoc->m_pGameView->m_tPlayer.bRoll== FALSE)
// {
// if (m_pDoc->m_pGameView->m_tPlayer.nCurDoing < 5)//!= 5)
// m_pDoc->m_pGameView->m_tPost.nPostCurDoing = 0;
// SetKeyPressStatus(eKey, false);
// return;
// }
// else if (eKey == EKEY_KEY1 && m_pDoc->m_pGameView->m_tPlayer.bRoll== FALSE)
// {
// if (m_pDoc->m_pGameView->m_tPlayer.nCurDoing < 5)//!= 5)
// {
// m_pDoc->m_pGameView->m_tPlayer.nCurDoing = 0;
// m_pDoc->m_pGameView->m_tPlayer.nPlayerNum = 0;
// }
// SetKeyPressStatus(eKey, false);
// return;
// }
// else if (eKey == EKEY_KEY3 && m_pDoc->m_pGameView->m_tPlayer.bRoll== FALSE)
// {
// if (m_pDoc->m_pGameView->m_tPlayer.nCurDoing < 5)//!= 5)
// {
// m_pDoc->m_pGameView->m_tPlayer.nCurDoing = 0;
// m_pDoc->m_pGameView->m_tPlayer.nPlayerNum = 0;
// }
// SetKeyPressStatus(eKey, false);
// return;
// }
// else if (eKey == EKEY_KEY4 && m_pDoc->m_pGameView->m_tPlayer.bRoll== FALSE)
// {
// if (m_pDoc->m_pGameView->m_tPlayer.nCurDoing < 5)//!= 5)
// {
// m_pDoc->m_pGameView->m_tPlayer.nCurDoing = 0;
// m_pDoc->m_pGameView->m_tPlayer.nPlayerNum = 0;
// }
// SetKeyPressStatus(eKey, false);
// return;
// }
// else if (eKey == EKEY_KEY6 && m_pDoc->m_pGameView->m_tPlayer.bRoll== FALSE)
// {
// if (m_pDoc->m_pGameView->m_tPlayer.nCurDoing < 5)//!= 5)
// {
// m_pDoc->m_pGameView->m_tPlayer.nCurDoing = 0;
// m_pDoc->m_pGameView->m_tPlayer.nPlayerNum = 0;
// }
// SetKeyPressStatus(eKey, false);
// return;
// }
// else if (eKey == EKEY_KEY8 && m_pDoc->m_pGameView->m_tPlayer.bRoll== FALSE)
// {
// if (m_pDoc->m_pGameView->m_tPlayer.nCurDoing < 5)//!= 5)
// {
// m_pDoc->m_pGameView->m_tPlayer.nCurDoing = 0;
// m_pDoc->m_pGameView->m_tPlayer.nPlayerNum = 0;
// }
// SetKeyPressStatus(eKey, false);
// return;
// }
// else if (eKey == EKEY_SELECT && m_pDoc->m_pGameView->m_tPlayer.bRoll== FALSE)
// {
// if (m_pDoc->m_pGameView->m_tPlayer.nCurDoing < 5)//!= 5)
// m_pDoc->m_pGameView->m_tPlayer.nCurDoing = 0;
// SetKeyPressStatus(eKey, false);
// return;
// }
}
}
SetKeyPressStatus(eKey, false);
}
// 标志当前键是否被按下,在处理此键后设置,用于标志键被按住时,是否处理
void CKeyControl::SetKeyPressStatus(EKEYCODE eKey, bool bPress)
{
m_bKeyPress[eKey] = bPress;
}
bool CKeyControl::GetKeyPressStatus(EKEYCODE eKey)
{
return m_bKeyPress[eKey];
}
boolean CKeyControl::IsAnyKeyPress()
{
boolean bRes = false;
bRes = (m_nAnyKeyStatus == 0) ? false : true;
m_nAnyKeyStatus = 0;
return bRes;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -