Cocos2d-x 3.17 exposes keyboard events to Lua through cc.EventListenerKeyboard. A listener needs callbacks for key presses and releases, and it must be attached to a node through the event dispatcher.
1 | local function addKeyboardListener(node) |
Call it after the node has been created:
1 | function GameScene:onEnter() |
Scene-graph priority associates the listener with the node, so it follows the node’s running state and is normally removed with it. If a listener was registered with fixed priority or needs early cleanup, remove it explicitly:
1 | self:getEventDispatcher():removeEventListener(self.keyboardListener) |
Keep platform differences in mind: desktop builds provide physical keyboard events, while mobile gameplay normally needs touch controls or a text-input component. Key-code names can also vary with the Cocos2d-x binding version, so check the constants generated for the project when a particular key is not recognized.