Cocos2dx Lua键盘监听实现:事件处理与自定义响应 | 南锋

南锋

南奔万里空,脱死锋镝余

Cocos2dx Lua键盘监听实现:事件处理与自定义响应

开发环境:Cocos2dx 3.17
开发语言:lua
这里用lua实现对键盘的监听

下面直接上代码吧

1
2
3
4
5
6
7
8
9
10
local function keyboardPressed(KeyCode,event)
print(KeyCode)
end
local function keyboardReleased(KeyCode,event)
end
local listener = cc.EventListenerKeyboard:create()
listener:registerScriptHandler(keyboardPressed, cc.Handler.EVENT_KEYBOARD_PRESSED)
listener:registerScriptHandler(keyboardReleased, cc.Handler.EVENT_KEYBOARD_RELEASED)
local eventDispatcher = self:getEventDispatcher()
eventDispatcher:addEventListenerWithSceneGraphPriority(listener,self)
+