Hi, I downloaded cocos2dx v2.2.2 from the project download page: http://www.cocos2d-x.org/download
I want to add keyboard support for Mac OSX, how can I do that?
Take a look at my implementation for linux:
(cocos2d-x/platform/linux/CCEGLView.cpp)
#include "support/CCNotificationCenter.h"
#include "cocoa/CCArray.h"
#include "cocoa/CCInteger.h"
#include "cocoa/CCBool.h"
...
void keyEventHandle(int iKeyID,int iKeyState) {
CCInteger* pKeyID = CCInteger::create(iKeyID);
CCBool* pKeyState = CCBool::create(iKeyState != GLFW_RELEASE);
CCArray* pPair = CCArray::createWithCapacity(2);
pPair->addObject(pKeyID);
pPair->addObject(pKeyState);
cocos2d::CCNotificationCenter::sharedNotificationCenter()->postNotification(
"KeyStateChanged", pPair);
if (iKeyState == GLFW_RELEASE) {
return;
}
if (iKeyID == GLFW_KEY_BACKSPACE) {
CCIMEDispatcher::sharedDispatcher()->dispatchDeleteBackward();
} else if (iKeyID == GLFW_KEY_ENTER) {
CCIMEDispatcher::sharedDispatcher()->dispatchInsertText("\n", 1);
} else if (iKeyID == GLFW_KEY_TAB) {
}
else if( iKeyID == GLFW_KEY_ESC)
{
CCDirector::sharedDirector()->getKeypadDispatcher()->dispatchKeypadMSG(kTypeBackClicked);
}
}
And draft of a simple KeyboardListener
/*
* KeyboardListener.h
*
* Created on: Feb 13, 2014
* Author: Jakub Kuderski
*/
#pragma once
#include "utils/Utils.h"
#include "cocos2d.h"
#include "GL/glfw.h"
#include < string >
#include < map >
USING_NS_CC;
namespace Utils
{
class KeyboardListener : public CCObject
{
public:
enum class Key : int
{
Delete = GLFW_KEY_DEL,
Tilde = 96
};
static KeyboardListener* getInstance();
~KeyboardListener();
static bool purge();
bool getKeyState( Key key );
private:
static KeyboardListener* m_pInstance;
std::map< int, bool > m_keyStates;
void onKeyPressed ( CCObject* pKeyCodeStatePair );
KeyboardListener();
BLOCK_COPY_OBJECT ( KeyboardListener );
};
} //namespace Utils
/*
* KeyboardListener.cpp
*
* Created on: Feb 13, 2014
* Author: Jakub Kuderski
*/
#include "utils/KeyboardListener.h"
namespace Utils
{
KeyboardListener* KeyboardListener::m_pInstance = nullptr;
KeyboardListener::KeyboardListener()
{
CCNotificationCenter::sharedNotificationCenter()->addObserver( this,
callfuncO_selector( KeyboardListener::onKeyPressed ), "KeyStateChanged", nullptr );
}
KeyboardListener::~KeyboardListener()
{
CCNotificationCenter::sharedNotificationCenter()->removeAllObservers( this );
}
KeyboardListener* KeyboardListener::getInstance()
{
if ( !m_pInstance )
{
m_pInstance = new KeyboardListener();
}
return m_pInstance;
}
bool KeyboardListener::purge()
{
if ( m_pInstance == nullptr )
{
return false;
}
CC_SAFE_DELETE( m_pInstance );
return true;
}
void KeyboardListener::onKeyPressed ( CCObject* pKeyCodeStatePair )
{
assert ( pKeyCodeStatePair );
CCArray* pPair = dynamic_cast< CCArray* > ( pKeyCodeStatePair );
assert ( pPair );
CCInteger* pKeyCode = dynamic_cast< CCInteger* >( pPair->objectAtIndex( 0 ) );
assert ( pKeyCode );
CCBool* pState = dynamic_cast< CCBool* >( pPair->objectAtIndex( 1 ) );
assert ( pState );
m_keyStates[pKeyCode->getValue()] = pState->getValue();
LOG ( "Key:\t%d\t%s", pKeyCode->getValue(), pState->getValue() ? "pressed" : "unpressed" );
}
bool KeyboardListener::getKeyState( Key key )
{
return m_keyStates[(int) key];
}
} //namespace Utils