Getting an error with the callback function on Keyboard Pressed event

Hi…

I am getting an error with this

listener->onKeyPressed= CC_CALLBACK_2(KeyPressed::Pressed, this);

saying

Severity    Code    Description    Project    File    Line
Error    C2893    Failed to specialize function template 'unknown-type std::invoke(_Callable &&,_Types &&...)'    com.cocos.test_cocos    c:\program files (x86)\microsoft visual studio 14.0\vc\include\type_traits    1501

Can someone help me out i am new to Cocos2d-x framework. I was following this Tutorial

thx

Show more of your code.

What does KeyPressed::Press look like?

Its the as on the tutorial

void KeyPressed::Pressed(cocos2d::EventKeyboard::KeyCode keyCode, cocos2d::Event *event)
{
    CCLOG("Key Pressed");
}

And how did you create the listener?
Like

auto listener = EventListenerKeyboard::create();

Is KeyPressed a separate class?
What’s the declaration of Pressed?

We have a Programmers Guide and this should help.

class KeyPressed : public cocos2d::Layer
{
public:
    void Pressed(cocos2d::EventKeyboard::KeyCode keyCode, cocos2d::Event *event);
    void Released(cocos2d::EventKeyboard::KeyCode keyCode, cocos2d::Event *event);
};

void KeyPressed::Pressed(cocos2d::EventKeyboard::KeyCode keyCode, cocos2d::Event *event)
{
    CCLOG("Key Pressed");
}

Inside ::init()

auto listener = EventListenerKeyboard::create();
listener->onKeyPressed = CC_CALLBACK_2(KeyPressed::Pressed, this);
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this);

Anyways I was able to callback with the help of lambda expression but if the usual way works it would have been great

 listener->onKeyReleased = [](EventKeyboard::KeyCode keyCode, Event* event)
{
     CCLOG("Inside Lambda");
}

Getting the same error with other tutorials too but able to get around it using lambda

Sorry, so you are saying this:

// creating a keyboard event listener
auto listener = EventListenerKeyboard::create();
listener->onKeyPressed = CC_CALLBACK_2(KeyboardTest::onKeyPressed, this);
listener->onKeyReleased = CC_CALLBACK_2(KeyboardTest::onKeyReleased, this);

_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);

// Implementation of the keyboard event callback function prototype
void KeyboardTest::onKeyPressed(EventKeyboard::KeyCode keyCode, Event* event)
{
        log("Key with keycode %d pressed", keyCode);
}

void KeyboardTest::onKeyReleased(EventKeyboard::KeyCode keyCode, Event* event)
{
        log("Key with keycode %d released", keyCode);
}

Doesn’t work?

Are you replacing this KeyboardTest:: with your class?

I don’t see an error with your code and it works fine for me.

Did you change anything in your post?

When I copied your code earlier, I got:

void Pressed(cocos2d::EventKeyboard::KeyCode keyCode, cocos2d::Event *event ,bool key);

Must be some compiler bug.

Is it possible to provide your .h/.cpp files as a download?

I am quite sure I didn’t code anything wrong.I guess its maybe because of my compiler(visual studio professional 2015) and the changes they did on it and regarding the .h/.cpp I am just doing copy paste from the tutorials and adding is to the template project. I was not coding it right from scratch.I can still provide it if you want…

Yes i did replace it…

Yes, please. So I can do some tests with your original code and we are on the same page.

That would have been an error as the function does not take 3 arguments.

All the header files and cpp I have also included a screen shot
https://drive.google.com/drive/folders/0Byu15VNB6uJ_UDhqSm1iZDB5OEU

Why not get it working in your HelloWorld class and then worry about moving the code elsewhere?

Thanks, but can you make the link accessible to users without a google account?
It seems there is a google account login required.

I made .cpp and .h files allowable extensions, so they can be uploaded directly.
HelloWorldScene.cpp (6.7 KB)
HelloWorldScene.h (465 Bytes)
KeyPressed.cpp (305 Bytes)
KeyPressed.h (503 Bytes)

Thank you, All I have to do now, is waiting for the upload :wink:

I added his files. I suggested a double check of getting it working in HelloWorldScene and then move his logic. I tested the code from the PG and it works.

I can’t see the line, this error is related to. You would have to scroll down and post more of the error. I can only see the trait error referring to the stl, but not the code line it refers to.

You cannot pass a this pointer to the CC_CALLBACK_2 macro, as you want to bind the Pressed function to a KeyPressed object. The this pointer points to a HelloWorld object.

_listener->onKeyPressed = CC_CALLBACK_2(KeyPressed::Pressed, this);

Try if your error goes away with this:

KeyPressed *keyPressed = new KeyPressed();

_listener->onKeyPressed = CC_CALLBACK_2(KeyPressed::Pressed, keyPressed);

Normally the compiler error would be different to yours.

Another error is here:

// trigger when moving touch
listener1->onTouchMoved = [](Touch* touch, Event* event) {
    // your code
    // crt(touch, event);
    return true;
};

The function signature is:

typedef std::function<void(Touch*, Event*)> ccTouchCallback;

You do not need to return a bool!

// trigger when moving touch
listener1->onTouchMoved = [](Touch* touch, Event* event) {
    // your code
    // crt(touch, event);
};

Ok but its not getting compiled on my compiler :sweat:

I guess i have to switch to a different compiler… :disappointed: