cc.Class({ extends: cc.Component, onLoad: function () { // add key down and key up event cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this); cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this); }, onDestroy () { cc.systemEvent.off(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this); cc.systemEvent.off(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this); }, onKeyDown: function (event) { switch(event.keyCode) { case cc.macro.KEY.a: console.log('Press a key'); break; } }, onKeyUp: function (event) { switch(event.keyCode) { case cc.macro.KEY.a: console.log('release a key'); break; } } });