diff --git a/tests/cpp-tests/Classes/Box2DTestBed/Box2dView.cpp b/tests/cpp-tests/Classes/Box2DTestBed/Box2dView.cpp index f4f9a1c878..86924143ad 100644 --- a/tests/cpp-tests/Classes/Box2DTestBed/Box2dView.cpp +++ b/tests/cpp-tests/Classes/Box2DTestBed/Box2dView.cpp @@ -188,6 +188,12 @@ bool Box2DView::initWithEntryID(int entryId) _eventDispatcher->addEventListenerWithFixedPriority(listener, -10); _touchListener = listener; + auto keyboardListener = EventListenerKeyboard::create(); + keyboardListener->onKeyPressed = CC_CALLBACK_2(Box2DView::onKeyPressed, this); + keyboardListener->onKeyReleased = CC_CALLBACK_2(Box2DView::onKeyReleased, this); + _eventDispatcher->addEventListenerWithFixedPriority(keyboardListener, -11); + _keyboardListener = keyboardListener; + return true; } @@ -222,6 +228,7 @@ Box2DView::~Box2DView() { // Removes Touch Event Listener _eventDispatcher->removeEventListener(_touchListener); + _eventDispatcher->removeEventListener(_keyboardListener); delete m_test; } // @@ -239,7 +246,7 @@ bool Box2DView::onTouchBegan(Touch* touch, Event* event) auto nodePosition = convertToNodeSpace( touchLocation ); log("Box2DView::onTouchBegan, pos: %f,%f -> %f,%f", touchLocation.x, touchLocation.y, nodePosition.x, nodePosition.y); - return m_test->MouseDown(b2Vec2(nodePosition.x,nodePosition.y)); + return m_test->MouseDown(b2Vec2(nodePosition.x,nodePosition.y)); } void Box2DView::onTouchMoved(Touch* touch, Event* event) @@ -262,6 +269,18 @@ void Box2DView::onTouchEnded(Touch* touch, Event* event) m_test->MouseUp(b2Vec2(nodePosition.x,nodePosition.y)); } +void Box2DView::onKeyPressed(EventKeyboard::KeyCode code, Event* event) +{ + log("Box2dView:onKeyPressed, keycode: %d", code); + m_test->Keyboard(static_cast(code)); +} + +void Box2DView::onKeyReleased(EventKeyboard::KeyCode code, Event* event) +{ + log("onKeyReleased, keycode: %d", code); + m_test->KeyboardUp(static_cast(code)); +} + // void Box2DView::accelerometer(UIAccelerometer* accelerometer, Acceleration* acceleration) // { // //// Only run for valid values diff --git a/tests/cpp-tests/Classes/Box2DTestBed/Box2dView.h b/tests/cpp-tests/Classes/Box2DTestBed/Box2dView.h index b7c71823ef..4c73a975c2 100644 --- a/tests/cpp-tests/Classes/Box2DTestBed/Box2dView.h +++ b/tests/cpp-tests/Classes/Box2DTestBed/Box2dView.h @@ -32,6 +32,7 @@ class Test; class Box2DView : public Layer { EventListenerTouchOneByOne* _touchListener; + EventListenerKeyboard* _keyboardListener; TestEntry* m_entry; Test* m_test; int m_entryID; @@ -47,6 +48,9 @@ public: bool onTouchBegan(Touch* touch, Event* event); void onTouchMoved(Touch* touch, Event* event); void onTouchEnded(Touch* touch, Event* event); + + void onKeyPressed(EventKeyboard::KeyCode code, Event* event); + void onKeyReleased(EventKeyboard::KeyCode code, Event* event); //virtual void accelerometer(UIAccelerometer* accelerometer, Acceleration* acceleration); static Box2DView* viewWithEntryID(int entryId);