2013-05-31 20:29:32 +08:00
|
|
|
#include "KeyboardTest.h"
|
|
|
|
|
|
|
|
KeyboardTest::KeyboardTest()
|
|
|
|
{
|
2013-08-16 16:05:27 +08:00
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2014-04-09 23:31:05 +08:00
|
|
|
auto label = Label::createWithTTF("Keyboard Test", "fonts/arial.ttf", 28);
|
2013-05-31 20:29:32 +08:00
|
|
|
addChild(label, 0);
|
2013-07-12 18:04:32 +08:00
|
|
|
label->setPosition( Point(s.width/2, s.height-50) );
|
2013-05-31 20:29:32 +08:00
|
|
|
|
2013-10-23 16:14:03 +08:00
|
|
|
auto listener = EventListenerKeyboard::create();
|
|
|
|
listener->onKeyPressed = CC_CALLBACK_2(KeyboardTest::onKeyPressed, this);
|
|
|
|
listener->onKeyReleased = CC_CALLBACK_2(KeyboardTest::onKeyReleased, this);
|
|
|
|
|
2013-10-26 15:04:01 +08:00
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
2013-10-23 16:14:03 +08:00
|
|
|
|
2013-05-31 20:29:32 +08:00
|
|
|
// create a label to display the tip string
|
2014-04-09 23:31:05 +08:00
|
|
|
_label = Label::createWithTTF("Please press any key and see console log...", "fonts/arial.ttf", 22);
|
2013-07-12 18:04:32 +08:00
|
|
|
_label->setPosition(Point(s.width / 2, s.height / 2));
|
2013-06-15 14:03:30 +08:00
|
|
|
addChild(_label, 0);
|
2013-05-31 20:29:32 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_label->retain();
|
2013-05-31 20:29:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
KeyboardTest::~KeyboardTest()
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_label->release();
|
2013-05-31 20:29:32 +08:00
|
|
|
}
|
|
|
|
|
2013-09-20 19:19:31 +08:00
|
|
|
void KeyboardTest::onKeyPressed(EventKeyboard::KeyCode keyCode, Event* event)
|
2013-05-31 20:29:32 +08:00
|
|
|
{
|
2013-07-24 06:20:22 +08:00
|
|
|
log("Key with keycode %d pressed", keyCode);
|
2013-05-31 20:29:32 +08:00
|
|
|
}
|
|
|
|
|
2013-09-20 19:19:31 +08:00
|
|
|
void KeyboardTest::onKeyReleased(EventKeyboard::KeyCode keyCode, Event* event)
|
2013-05-31 20:29:32 +08:00
|
|
|
{
|
2013-07-24 06:20:22 +08:00
|
|
|
log("Key with keycode %d released", keyCode);
|
2013-05-31 20:29:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void KeyboardTestScene::runThisTest()
|
|
|
|
{
|
2013-08-16 16:05:27 +08:00
|
|
|
auto layer = new KeyboardTest();
|
2013-07-23 08:25:44 +08:00
|
|
|
addChild(layer);
|
2013-05-31 20:29:32 +08:00
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
Director::getInstance()->replaceScene(this);
|
2013-07-23 08:25:44 +08:00
|
|
|
layer->release();
|
2013-05-31 20:29:32 +08:00
|
|
|
}
|
2013-06-12 09:20:18 +08:00
|
|
|
|