h1.At first you can add code in “AppDelegate.cpp”, you will make sure write the code below the “AppDelegate::applicationDidFinishLaunching()”
[html] view plaincopy
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
HelloWorld *g_layer;
void Win32SetKeyLayer(HelloWorld *layer)
{
g_layer = layer;
}
void Win32KeyHook( UINT message,WPARAM wParam, LPARAM lParam )
{
CCLog("Win32KeyHook message %d wParam %d lParam %d", message, wParam, lParam);
if (g_layer)
g_layer->onWin32KeyEvent(message, wParam, lParam);
}
#endif
h1.in AppDelegate::applicationDidFinishLaunching()
[html] view plaincopy
bool AppDelegate::applicationDidFinishLaunching() {
// initialize director
CCDirector* pDirector = CCDirector::sharedDirector();
CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
// add keyboard event
pEGLView->setAccelerometerKeyHook(Win32KeyHook);///////////////////////////////////
#endif // CC_PLATFORM_WIN32
pDirector->setOpenGLView(pEGLView);
// turn on display FPS
pDirector->setDisplayStats(true);
// set FPS. the default value is 1.0/60 if you don't call this
pDirector->setAnimationInterval(1.0 / 60);
// create a scene. it's an autorelease object
CCScene *pScene = HelloWorld::scene();
// run
pDirector->runWithScene(pScene);
return true;
}
h1.In HelloWorldScene.cpp
[html] view plaincopy
CCScene* HelloWorld::scene()
{
// 'scene' is an autorelease object
CCScene *scene = CCScene::create();
// 'layer' is an autorelease object
HelloWorld *layer = HelloWorld::create();
// add layer as a child to scene
scene->addChild(layer);
extern void Win32SetKeyLayer(HelloWorld *layer);
Win32SetKeyLayer(layer);
// return the scene
return scene;
}
[html] view plaincopy
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
void HelloWorld::onWin32KeyEvent( UINT message,WPARAM wParam, LPARAM lParam )
{
CCLog("onWin32KeyEvent message %d wParam %d lParam %d", message, wParam, lParam);
/*
// Up
Win32KeyHook message 256 wParam 38 lParam 21495809
onWin32KeyEvent message 256 wParam 38 lParam 21495809
Win32KeyHook message 257 wParam 38 lParam -1052246015
onWin32KeyEvent message 257 wParam 38 lParam -1052246015
// Down
Win32KeyHook message 256 wParam 40 lParam 22020097
onWin32KeyEvent message 256 wParam 40 lParam 22020097
Win32KeyHook message 257 wParam 40 lParam -1051721727
onWin32KeyEvent message 257 wParam 40 lParam -1051721727
// Left
Win32KeyHook message 256 wParam 37 lParam 21692417
onWin32KeyEvent message 256 wParam 37 lParam 21692417
Win32KeyHook message 257 wParam 37 lParam -1052049407
onWin32KeyEvent message 257 wParam 37 lParam -1052049407
// Right
Win32KeyHook message 256 wParam 39 lParam 21823489
onWin32KeyEvent message 256 wParam 39 lParam 21823489
Win32KeyHook message 257 wParam 39 lParam -1051918335
onWin32KeyEvent message 257 wParam 39 lParam -1051918335
*/
if (message == 256)
{
switch (wParam)
{
case 38:
moveHero(1);
break;
case 40:
moveHero(2);
break;
case 37:
moveHero(3);
break;
case 39:
moveHero(4);
break;
}
}
else if (message == 257)
{
}
}
#endif
[html] view plaincopy
void HelloWorld::moveHero( int diraction ) { CCLog(“moveHero: %d”,diraction); }h1.//Now you can try to press “up,down,left,right” key to check the print