键盘操作

数据加载中...#ifndef __CKeyInput_SCENE_H__
#define __CKeyInput_SCENE_H__

#include "cocos2d.h"
#include "cocos-ext.h"

USING_NS_CC;
USING_NS_CC_EXT;

struct keyCodeName
{
std::string KeyCodeName;
EventKeyboard::KeyCode keyCode;
};

static keyCodeName g_keyCodeNameArray[] = 
{
{  "NONE", EventKeyboard::KeyCode::KEY_NONE              },
{  "PAUSE", EventKeyboard::KeyCode::KEY_PAUSE  },
{  "SCROLL_LOCK", EventKeyboard::KeyCode::KEY_SCROLL_LOCK  },
{  "PRINT", EventKeyboard::KeyCode::KEY_PRINT  },
{  "SYSREQ", EventKeyboard::KeyCode::KEY_SYSREQ  },
{  "BREAK", EventKeyboard::KeyCode::KEY_BREAK  },
{  "ESCAPE", EventKeyboard::KeyCode::KEY_ESCAPE  },
{  "BACKSPACE", EventKeyboard::KeyCode::KEY_BACKSPACE  },
{  "TAB", EventKeyboard::KeyCode::KEY_TAB  },
{  "BACK_TAB", EventKeyboard::KeyCode::KEY_BACK_TAB  },
{  "RETURN", EventKeyboard::KeyCode::KEY_RETURN  },
{  "CAPS_LOCK", EventKeyboard::KeyCode::KEY_CAPS_LOCK  },
{  "SHIFT", EventKeyboard::KeyCode::KEY_SHIFT  },
{  "CTRL", EventKeyboard::KeyCode::KEY_CTRL  },
{  "ALT", EventKeyboard::KeyCode::KEY_ALT  },
{  "MENU", EventKeyboard::KeyCode::KEY_MENU  },
{  "HYPER", EventKeyboard::KeyCode::KEY_HYPER  },
{  "INSERT", EventKeyboard::KeyCode::KEY_INSERT  },
{  "HOME", EventKeyboard::KeyCode::KEY_HOME  },
{  "PG_UP", EventKeyboard::KeyCode::KEY_PG_UP  },
{  "DELETE", EventKeyboard::KeyCode::KEY_DELETE  },
{  "END", EventKeyboard::KeyCode::KEY_END  },
{  "PG_DOWN", EventKeyboard::KeyCode::KEY_PG_DOWN  },
{  "LEFT_ARROW", EventKeyboard::KeyCode::KEY_LEFT_ARROW  },
{  "RIGHT_ARROW", EventKeyboard::KeyCode::KEY_RIGHT_ARROW  },
{  "UP_ARROW", EventKeyboard::KeyCode::KEY_UP_ARROW  },
{  "DOWN_ARROW", EventKeyboard::KeyCode::KEY_DOWN_ARROW  },
{  "NUM_LOCK", EventKeyboard::KeyCode::KEY_NUM_LOCK  },
{  "KP_PLUS", EventKeyboard::KeyCode::KEY_KP_PLUS  },
{  "KP_MINUS", EventKeyboard::KeyCode::KEY_KP_MINUS  },
{  "KP_MULTIPLY", EventKeyboard::KeyCode::KEY_KP_MULTIPLY  },
{  "KP_DIVIDE", EventKeyboard::KeyCode::KEY_KP_DIVIDE            },
{  "KP_ENTER", EventKeyboard::KeyCode::KEY_KP_ENTER   },
{  "KP_HOME", EventKeyboard::KeyCode::KEY_KP_HOME   },
{  "KP_UP", EventKeyboard::KeyCode::KEY_KP_UP   },
{  "KP_PG_UP", EventKeyboard::KeyCode::KEY_KP_PG_UP   },
{  "KP_LEFT", EventKeyboard::KeyCode::KEY_KP_LEFT   },
{  "KP_FIVE", EventKeyboard::KeyCode::KEY_KP_FIVE   },
{  "KP_RIGHT", EventKeyboard::KeyCode::KEY_KP_RIGHT   },
{  "KP_END", EventKeyboard::KeyCode::KEY_KP_END   },
{  "KP_DOWN", EventKeyboard::KeyCode::KEY_KP_DOWN   },
{  "KP_PG_DOWN", EventKeyboard::KeyCode::KEY_KP_PG_DOWN   },
{  "KP_INSERT", EventKeyboard::KeyCode::KEY_KP_INSERT   },
{  "KP_DELETE", EventKeyboard::KeyCode::KEY_KP_DELETE   },
{  "F1", EventKeyboard::KeyCode::KEY_F1   },
{  "F2", EventKeyboard::KeyCode::KEY_F2   },
{  "F3", EventKeyboard::KeyCode::KEY_F3   },
{  "F4", EventKeyboard::KeyCode::KEY_F4   },
{  "F5", EventKeyboard::KeyCode::KEY_F5   },
{  "F6", EventKeyboard::KeyCode::KEY_F6   },
{  "F7", EventKeyboard::KeyCode::KEY_F7   },
{  "F8", EventKeyboard::KeyCode::KEY_F8   },
{  "F9", EventKeyboard::KeyCode::KEY_F9   },
{  "F10", EventKeyboard::KeyCode::KEY_F10   },
{  "F11", EventKeyboard::KeyCode::KEY_F11   },
{  "F12", EventKeyboard::KeyCode::KEY_F12   },
{  "SPACE", EventKeyboard::KeyCode::KEY_SPACE   },
{  "EXCLAM", EventKeyboard::KeyCode::KEY_EXCLAM   },
{  "QUOTE", EventKeyboard::KeyCode::KEY_QUOTE   },
{  "NUMBER", EventKeyboard::KeyCode::KEY_NUMBER   },
{  "DOLLAR", EventKeyboard::KeyCode::KEY_DOLLAR   },
{  "PERCENT", EventKeyboard::KeyCode::KEY_PERCENT   },
{  "CIRCUMFLEX", EventKeyboard::KeyCode::KEY_CIRCUMFLEX   },
{  "AMPERSAND", EventKeyboard::KeyCode::KEY_AMPERSAND    },
{  "APOSTROPHE", EventKeyboard::KeyCode::KEY_APOSTROPHE    },
{  "LEFT_PARENTHESIS", EventKeyboard::KeyCode::KEY_LEFT_PARENTHESIS  },
{  "RIGHT_PARENTHESIS", EventKeyboard::KeyCode::KEY_RIGHT_PARENTHESIS },
{  "ASTERISK", EventKeyboard::KeyCode::KEY_ASTERISK    },
{  "PLUS", EventKeyboard::KeyCode::KEY_PLUS    },
{  "COMMA", EventKeyboard::KeyCode::KEY_COMMA    },
{  "MINUS", EventKeyboard::KeyCode::KEY_MINUS    },
{  "PERIOD", EventKeyboard::KeyCode::KEY_PERIOD    },
{  "SLASH", EventKeyboard::KeyCode::KEY_SLASH    },
{  "0", EventKeyboard::KeyCode::KEY_0    },
{  "1", EventKeyboard::KeyCode::KEY_1    },
{  "2", EventKeyboard::KeyCode::KEY_2    },
{  "3", EventKeyboard::KeyCode::KEY_3    },
{  "4", EventKeyboard::KeyCode::KEY_4    },
{  "5", EventKeyboard::KeyCode::KEY_5    },
{  "6", EventKeyboard::KeyCode::KEY_6    },
{  "7", EventKeyboard::KeyCode::KEY_7    },
{  "8", EventKeyboard::KeyCode::KEY_8    },
{  "9", EventKeyboard::KeyCode::KEY_9    },
{  "COLON", EventKeyboard::KeyCode::KEY_COLON    },
{  "SEMICOLON", EventKeyboard::KeyCode::KEY_SEMICOLON    },
{  "LESS_THAN", EventKeyboard::KeyCode::KEY_LESS_THAN    },
{  "EQUAL", EventKeyboard::KeyCode::KEY_EQUAL    },
{  "GREATER_THAN", EventKeyboard::KeyCode::KEY_GREATER_THAN    },
{  "QUESTION", EventKeyboard::KeyCode::KEY_QUESTION    },
{  "AT", EventKeyboard::KeyCode::KEY_AT    },
{  "CAPITAL_A", EventKeyboard::KeyCode::KEY_CAPITAL_A    },
{  "CAPITAL_B", EventKeyboard::KeyCode::KEY_CAPITAL_B    },
{  "CAPITAL_C", EventKeyboard::KeyCode::KEY_CAPITAL_C,    },
{  "CAPITAL_D", EventKeyboard::KeyCode::KEY_CAPITAL_D,    },
{  "CAPITAL_E", EventKeyboard::KeyCode::KEY_CAPITAL_E,    },
{  "CAPITAL_F", EventKeyboard::KeyCode::KEY_CAPITAL_F,       },
{  "CAPITAL_G", EventKeyboard::KeyCode::KEY_CAPITAL_G,  },
{  "CAPITAL_H", EventKeyboard::KeyCode::KEY_CAPITAL_H,  },
{  "CAPITAL_I", EventKeyboard::KeyCode::KEY_CAPITAL_I,  },
{  "CAPITAL_J", EventKeyboard::KeyCode::KEY_CAPITAL_J,  },
{  "CAPITAL_K", EventKeyboard::KeyCode::KEY_CAPITAL_K,  },
{  "CAPITAL_L", EventKeyboard::KeyCode::KEY_CAPITAL_L,  },
{  "CAPITAL_M", EventKeyboard::KeyCode::KEY_CAPITAL_M,  },
{  "CAPITAL_N", EventKeyboard::KeyCode::KEY_CAPITAL_N,  },
{  "CAPITAL_O", EventKeyboard::KeyCode::KEY_CAPITAL_O,  },
{  "CAPITAL_P", EventKeyboard::KeyCode::KEY_CAPITAL_P,  },
{  "CAPITAL_Q", EventKeyboard::KeyCode::KEY_CAPITAL_Q,  },
{  "CAPITAL_R", EventKeyboard::KeyCode::KEY_CAPITAL_R,  },
{  "CAPITAL_S", EventKeyboard::KeyCode::KEY_CAPITAL_S,  },
{  "CAPITAL_T", EventKeyboard::KeyCode::KEY_CAPITAL_T,  },
{  "CAPITAL_U", EventKeyboard::KeyCode::KEY_CAPITAL_U,  },
{  "CAPITAL_V", EventKeyboard::KeyCode::KEY_CAPITAL_V,  },
{  "CAPITAL_W", EventKeyboard::KeyCode::KEY_CAPITAL_W,  },
{  "CAPITAL_X", EventKeyboard::KeyCode::KEY_CAPITAL_X,  },
{  "CAPITAL_Y", EventKeyboard::KeyCode::KEY_CAPITAL_Y,  },
{  "CAPITAL_Z", EventKeyboard::KeyCode::KEY_CAPITAL_Z,  },
{  "LEFT_BRACKET", EventKeyboard::KeyCode::KEY_LEFT_BRACKET,  },
{  "BACK_SLASH", EventKeyboard::KeyCode::KEY_BACK_SLASH,  },
{  "RIGHT_BRACKET", EventKeyboard::KeyCode::KEY_RIGHT_BRACKET,  },
{  "UNDERSCORE", EventKeyboard::KeyCode::KEY_UNDERSCORE,  },
{  "GRAVE", EventKeyboard::KeyCode::KEY_GRAVE,  },
{  "A", EventKeyboard::KeyCode::KEY_A,  },
{  "B", EventKeyboard::KeyCode::KEY_B,  },
{  "C", EventKeyboard::KeyCode::KEY_C,  },
{  "D", EventKeyboard::KeyCode::KEY_D,  },
{  "E", EventKeyboard::KeyCode::KEY_E,  },
{  "F", EventKeyboard::KeyCode::KEY_F,  },
{  "G", EventKeyboard::KeyCode::KEY_G, },
{  "H", EventKeyboard::KeyCode::KEY_H, },
{  "I", EventKeyboard::KeyCode::KEY_I, },
{  "J", EventKeyboard::KeyCode::KEY_J, },
{  "K", EventKeyboard::KeyCode::KEY_K, },
{  "L", EventKeyboard::KeyCode::KEY_L, },
{  "M", EventKeyboard::KeyCode::KEY_M, },
{  "N", EventKeyboard::KeyCode::KEY_N, },
{  "O", EventKeyboard::KeyCode::KEY_O, },
{  "P", EventKeyboard::KeyCode::KEY_P, },
{  "Q", EventKeyboard::KeyCode::KEY_Q, },
{  "S", EventKeyboard::KeyCode::KEY_S,   },
{  "S", EventKeyboard::KeyCode::KEY_S,   },
{  "T", EventKeyboard::KeyCode::KEY_T,   },
{  "U", EventKeyboard::KeyCode::KEY_U,   },
{  "V", EventKeyboard::KeyCode::KEY_V,   },
{  "W", EventKeyboard::KeyCode::KEY_W,   },
{  "X", EventKeyboard::KeyCode::KEY_X,   },
{  "Y", EventKeyboard::KeyCode::KEY_Y,   },
{  "Z", EventKeyboard::KeyCode::KEY_Z,   },
{  "LEFT_BRACE", EventKeyboard::KeyCode::KEY_LEFT_BRACE,   },
{  "BAR", EventKeyboard::KeyCode::KEY_BAR,   },
{  "RIGHT_BRACE", EventKeyboard::KeyCode::KEY_RIGHT_BRACE,  },
{  "TILDE", EventKeyboard::KeyCode::KEY_TILDE,   },
{  "EURO", EventKeyboard::KeyCode::KEY_EURO,   },
{  "POUND", EventKeyboard::KeyCode::KEY_POUND,   },
{  "YEN", EventKeyboard::KeyCode::KEY_YEN,   },
{  "MIDDLE_DOT", EventKeyboard::KeyCode::KEY_MIDDLE_DOT,   },
{  "SEARCH", EventKeyboard::KeyCode::KEY_SEARCH,   },
{  "DPAD_LEFT", EventKeyboard::KeyCode::KEY_DPAD_LEFT,   },
{  "DPAD_RIGHT", EventKeyboard::KeyCode::KEY_DPAD_RIGHT,   },
{  "DPAD_UP", EventKeyboard::KeyCode::KEY_DPAD_UP,   },
{  "DPAD_DOWN", EventKeyboard::KeyCode::KEY_DPAD_DOWN,    },
{  "DPAD_CENTER", EventKeyboard::KeyCode::KEY_DPAD_CENTER,  },
{  "ENTER", EventKeyboard::KeyCode::KEY_ENTER,        },
{  "PLAY", EventKeyboard::KeyCode::KEY_PLAY}
};

class CKeyInput : public cocos2d::Layer
{
public:
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();  

// there's no 'id' in cpp, so we recommend returning the class instance pointer
static cocos2d::Scene* scene();

// implement the "static node()" method manually
CREATE_FUNC(CKeyInput);

private:
 virtual void onEnter() override;

void onKeyPressed(EventKeyboard::KeyCode code, Event* event);
void onKeyReleased(EventKeyboard::KeyCode code, Event* event);

// 获取键盘名字
std::string GetKeyName(EventKeyboard::KeyCode code);
};

#endif // __HELLOWORLD_SCENE_H__
#include "CKeyInput.h"
#include "AppMacros.h"

Scene* CKeyInput::scene()
{
// 'scene' is an autorelease object
auto scene = Scene::create();

// 'layer' is an autorelease object
CKeyInput *layer = CKeyInput::create();

// add layer as a child to scene
scene->addChild(layer);

// return the scene
return scene;
}

// on "init" you need to initialize your instance
bool CKeyInput::init()
{
//////////////////////////////
// 1. super init first
if ( !Layer::init() )
{
return false;
}
return true;
}

// KeyboardEventTest
void CKeyInput::onEnter()
{
Layer::onEnter();

Vec2 origin = Director::getInstance()->getVisibleOrigin();
Size size = Director::getInstance()->getVisibleSize();

auto statusLabel = Label::createWithSystemFont("No keyboard event received!", "", 20);
statusLabel->setPosition(origin + Vec2(size.width/2, size.height/2));
addChild(statusLabel);

auto keyboardListener = EventListenerKeyboard::create();
keyboardListener->onKeyPressed = CC_CALLBACK_2(CKeyInput::onKeyPressed, this);
keyboardListener->onKeyReleased = CC_CALLBACK_2(CKeyInput::onKeyReleased, this);
_eventDispatcher->addEventListenerWithFixedPriority(keyboardListener, -11);
_keyboardListener = keyboardListener;

/*
auto listener = EventListenerKeyboard::create();
listener->onKeyPressed = [](EventKeyboard::KeyCode keyCode, Event* event)
{
char buf[100] = {0};
sprintf(buf, "Key %d was pressed!", (int)keyCode);
auto label = static_cast<Label*>(event->getCurrentTarget());
label->setString(buf);
};

listener->onKeyReleased = [](EventKeyboard::KeyCode keyCode, Event* event)
{
char buf[100] = {0};
sprintf(buf, "Key %d was released!", (int)keyCode);
auto label = static_cast<Label*>(event->getCurrentTarget());
label->setString(buf);
};

_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, statusLabel);
*/
}

void CKeyInput::onKeyPressed(EventKeyboard::KeyCode code, Event* event)
{
std::string name = GetKeyName(code);
log("CKeyInput:onKeyPressed, keycode: %s_%d", name.c_str(), (int)code);
}

void CKeyInput::onKeyReleased(EventKeyboard::KeyCode code, Event* event)
{
std::string name = GetKeyName(code);
log("CKeyInput:onKeyReleased, keycode: %s_%d", name.c_str(), (int)code);
}

// 获取键盘名字
std::string  CKeyInput::GetKeyName(EventKeyboard::KeyCode code)
{
int size = sizeof(g_keyCodeNameArray) / sizeof(keyCodeName);

for (int i = 0; i < size; ++i)
{
const keyCodeName& table = g_keyCodeNameArray[i];
if (table.keyCode == code)
{
return table.KeyCodeName;
}
}

return "";
}
发布了370 篇原创文章 · 获赞 36 · 访问量 187万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览