如何在cocos2dx中响应win键盘事件,一共有两种方式,下面我们就来看一下具体的实现的过程
第一种方式:[cpp] view plaincopy
#include <windows.h>
#define KEY_DOWN(vk_code) (GetAsyncKeyState(vk_code) & 0x8000 ? 1 : 0)
#define KEY_UP(vk_code) (GetAsyncKeyState(vk_code) & 0x8000 ? 0 : 1)
if( KEY_DOWN( VK_DOWN ) )
{
SceneGaming * scene = (SceneGaming *)SceneManager::getSingleton().getGamingScene();
LayerGamingPause * pLayerGamingPause = scene->getLayerPause();
pLayerGamingPause->setVisible( true );
// scene->pauseSchedulerAndActions();
}
第二种方式:在初始化时先注册回调:
[cpp] view plaincopy
CCDirector::sharedDirector()->getOpenGLView()->setAccelerometerKeyHook( XXX::keyboardHook );
XXX:keyboardHook实现如下:
[cpp] view plaincopy
void XXX::keyboardHook( UINT message,WPARAM wParam, LPARAM lParam )
{
switch (message)
{
case WM_KEYDOWN:
return;
case WM_KEYUP:
// wParam 即为键盘字母码,在此处理即可
[cpp] view plaincopy
return;
}
}