Keyboard events not working with cocos2d-js 3.0b

With cocos2d-js 3.0a2 I used keyboard events in the following way:

    if( 'keyboard' in cc.sys.capabilities ) {
        cc.eventManager.addListener({
            event: cc.EventListener.KEYBOARD,
            onKeyPressed:function(key, event) {
            	switch(key) {
            	case KEY_LEFT_CODE:
            		self.isSwipeLeft = true;
            		break;
            	case KEY_RIGHT_CODE:
            		self.isSwipeRight = true;
            		break;
            	case KEY_UP_CODE:
            		self.isSwipeUp = true;
            		break;
            	case KEY_DOWN_CODE:
            		self.isSwipeDown = true;
            		break;
            	}
            },
            onKeyReleased:function(key, event) {
            	switch(key) {
            	case KEY_LEFT_CODE:
            		self.isSwipeLeft = false;
            		break;
            	case KEY_RIGHT_CODE:
            		self.isSwipeRight = false;
            		break;
            	case KEY_UP_CODE:
            		self.isSwipeUp = false;
            		break;
            	case KEY_DOWN_CODE:
            		self.isSwipeDown = false;
            		break;
                }
            }
        }, this);
    }

Now it doesn’t enter the callbacks anymore. Why could that be?

Which platform are you using? Keyboard is forbidden by default on mobile, But you can activate it in jsb_boot.js

    if( locSys.isMobile ) {
        capabilities["accelerometer"] = true;
        capabilities["touches"] = true;
        capabilities["keyboard"] = true;   // add this line 
    }

The same thing!
Upgraded to beta and the keyboard stopped working in browsers. Rolled back to alpha 2 :(((

Hi, @lavizrap @chiroi30

We have just been noticed that Cocos2d-x have recently modified the value for all key code, but this incompatible issue will be solved very soon

I still have this issue. Its very strange. The test suite (Event Test) works correctly. I use the same code as there and I don’t get the events. I debugged it down to DOM level and it got stranger. It calls this function in ccInputExtension.js:

p._registerKeyboardEvent = function(){
cc._addEventListener(cc._canvas, “keydown”, function (e) {
cc.eventManager.dispatchEvent(new cc.EventKeyboard(e.keyCode, true));
e.stopPropagation();
e.preventDefault();
});
cc._addEventListener(cc._canvas, “keyup”, function (e) {
cc.eventManager.dispatchEvent(new cc.EventKeyboard(e.keyCode, false));
e.stopPropagation();
e.preventDefault();
});
};

It registers both listeners but the callback functions are never called. It seems no problem with cocos, cause this is pure DOM code, but still, it works in the test suite.

Any idea?

Still facing the same issue here.

I can see the “keydown” and “keyup” event is corretly registered with the canvas, but doesn’t get its callback called. The keyboard events seem to be not propagated to the canvas. Is that possible?

I don’t have much experience with HTML5. In my case, the keyboard events are not working in the testbed also.

The keyboard events are also for me not triggered (in my game and in the test examples).

I am new to cocos2d JS and using 3.0 beta. I am experiencing the same problem. Is there any solution yet or should I use different version?

Thanks

Same PROBLEM! Keyboard events of test suite (Event Test) are not triggered in Chrome with cocos2d-js 3.0beta, but triggered with cocos2d-js 3.0alpha! It’s NOT about key code. This bug is so basic and important function. It is unacceptable that it is appearing in beta version. Does anybody test the code before release?

We are really sorry guys, the problem have been solved after the beta release, so please wait a little while for the next release. Truly sorry for such basic mistakes

Can we get a hot-fix/patch for this?

A patch would be nice to resolve this.

A patch is needed. Can you give us at least the commit which fixes the issue?

Sorry.
This problem has been fixed in the new version.
The canvas object does not get to the keyboard events.
Under normal conditions, canvas is not available keyboard events.

In this PR has been fixed keyboard events:

1 Like

Thank you! All is running fine now … :slight_smile: