How to recognition keyboard event in cocos2d-x ……(Tutorial by YUYE)

How to recognition keyboard event in cocos2d-x ……(Tutorial by YUYE)
0

#1

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

Using the Keyboard to Move Sprites(3.0 beta)
YUYE'Tutorials(What kind of the tutorial do you want to learn and we'll make it for you)
#2

Thank you for writing this but I’m still having a hard time with it I keep getting errors, is there anymore documentation on the subject of key recognition for windows?


#3

could you send a simple demo for me , I’ll debug it for you.

yuye.liu@chukong-inc.com

Devin Stafford wrote:

Thank you for writing this but I’m still having a hard time with it I keep getting errors, is there anymore documentation on the subject of key recognition for windows?


#4

I am trying to use run your code in my game but there are many errors when i build it. can you help me with that . i am trying to make my sprite move up down.


#5

@djain, this post was outdated, and @yuye has left our team.
Which engine version are you using, and what’s your target platform?


#6

I am using v3.2 and i am developing on visual studio. so win32 is my target platform.
so far i have this code working but my sprite(_ship) is not moving up and down. it is moving right and left when i press left arrow and right arrow.

                auto eventListener = EventListenerKeyboard::create();

	    eventListener->onKeyPressed = [](EventKeyboard::KeyCode keyCode, Event* event){
		
	Vec2 loc = event->getCurrentTarget()->getPosition();
    switch(keyCode){
        case EventKeyboard::KeyCode::KEY_LEFT_ARROW:
        case EventKeyboard::KeyCode::KEY_A:
            event->getCurrentTarget()->setPosition(loc.x-10,loc.y);
            break;
        case EventKeyboard::KeyCode::KEY_RIGHT_ARROW:
        case EventKeyboard::KeyCode::KEY_D:
            event->getCurrentTarget()->setPosition(loc.x+10,loc.y);
            break;
        case EventKeyboard::KeyCode::KEY_UP_ARROW:
        case EventKeyboard::KeyCode::KEY_W:
            event->getCurrentTarget()->setPosition(loc.x,++loc.y);
            break;
        case EventKeyboard::KeyCode::KEY_DOWN_ARROW:
        case EventKeyboard::KeyCode::KEY_S:
            event->getCurrentTarget()->setPosition(loc.x,--loc.y);
            break;
    }
		
};
       this->_eventDispatcher->addEventListenerWithSceneGraphPriority(eventListener, _ship);

#7

A bit weird. I can’t find mistakes in your code. You can add CCLog in KEY_W and KEY_S, to see if they are invoked correctly.

When you press “W” and “S”, will the source code run into case EventKeyboard::Keycode::KEY_W ?


#8

am I misunderstanding?

case EventKeyboard::KeyCode::KEY_LEFT_ARROW:
        case EventKeyboard::KeyCode::KEY_A:
            event->getCurrentTarget()->setPosition(loc.x-10,loc.y);
            break;

as an example, this code would just move left from current location - 10.

Isn’t that what you are seeing. Perhaps maybe I misunderstand your post.