Supports keyboard event. (#2734)

This commit is contained in:
James Chen
2018-05-25 14:59:35 +08:00
committed by pandamicro
parent 5217d2af1c
commit 0a15ee7d4f
3 changed files with 176 additions and 28 deletions
+66 -28
View File
@@ -24,11 +24,18 @@
****************************************************************************/
var __targetID = 0;
var __listenTouchEventMap = {};
var __listenMouseEventMap = {};
const __touchEventNames = ['touchstart', 'touchmove', 'touchend', 'touchcancel'];
const __mouseEventNames = ['mousedown', 'mousemove', 'mouseup', 'mousewheel'];
var __listenerMap = {
touch: {},
mouse: {},
keyboard: {}
};
var __handleEventNames = {
touch: ['touchstart', 'touchmove', 'touchend', 'touchcancel'],
mouse: ['mousedown', 'mousemove', 'mouseup', 'mousewheel'],
keyboard: ['keydown', 'keyup', 'keypress']
}
// Listener types
const CAPTURE = 1
@@ -57,34 +64,36 @@ function isObject(x) {
class EventTarget {
constructor() {
this._targetID = ++__targetID;
this._touchListenerCount = 0;
this._mouseListenerCount = 0;
this._listenerCount = {
touch: 0,
mouse: 0,
keyboard: 0
};
this._listeners = new Map();
}
_associateSystemEventListener(eventName) {
if (__touchEventNames.indexOf(eventName) > -1) {
if (this._touchListenerCount === 0)
__listenTouchEventMap[this._targetID] = this;
++this._touchListenerCount;
}
else if (__mouseEventNames.indexOf(eventName) > -1) {
if (this._mouseListenerCount === 0)
__listenMouseEventMap[this._targetID] = this;
++this._mouseListenerCount;
var handleEventNames;
for (var key in __handleEventNames) {
handleEventNames = __handleEventNames[key];
if (handleEventNames.indexOf(eventName) > -1) {
if (this._listenerCount[key] === 0)
__listenerMap[key][this._targetID] = this;
++this._listenerCount[key];
break;
}
}
}
_dissociateSystemEventListener(eventName) {
if (__touchEventNames.indexOf(eventName) > -1) {
--this._touchListenerCount;
if (this._touchListenerCount <= 0)
delete __listenTouchEventMap[this._targetID];
}
else if (__mouseEventNames.indexOf(eventName) > -1) {
--this._mouseListenerCount;
if (this._mouseListenerCount <= 0)
delete __listenMouseEventMap[this._targetID];
var handleEventNames;
for (var key in __handleEventNames) {
handleEventNames = __handleEventNames[key];
if (handleEventNames.indexOf(eventName) > -1) {
if (this._listenerCount[key] <= 0)
delete __listenerMap[key][this._targetID];
break;
}
}
}
@@ -267,8 +276,9 @@ function touchEventHandlerFactory(type) {
var i = 0, touchCount = touches.length;
var target;
for (let key in __listenTouchEventMap) {
target = __listenTouchEventMap[key];
var touchListenerMap = __listenerMap.touch;
for (let key in touchListenerMap) {
target = touchListenerMap[key];
for (i = 0; i < touchCount; ++i) {
touches[i].target = target;
}
@@ -293,8 +303,9 @@ function mouseEventHandlerFactory(type) {
mouseEvent.clientY = mouseEvent.screenY = mouseEvent.pageY = event.y;
var target;
for (let key in __listenMouseEventMap) {
target = __listenMouseEventMap[key];
var mouseListenerMap = __listenerMap.mouse;
for (let key in mouseListenerMap) {
target = mouseListenerMap[key];
target.dispatchEvent(mouseEvent);
}
}
@@ -305,5 +316,32 @@ jsb.onMouseMove = mouseEventHandlerFactory('mousemove');
jsb.onMouseUp = mouseEventHandlerFactory('mouseup');
jsb.onMouseWheel = mouseEventHandlerFactory('mousewheel');
function keyboardEventHandlerFactory(type) {
return (event) => {
var initArg = {
altKey: event.altKey,
ctrlKey: event.ctrlKey,
metaKey: event.metaKey,
shiftKey: event.shiftKey,
repeat: event.repeat,
code: '', // TODO
key: '', // TODO
keyCode: event.keyCode
};
const keyboardEvent = new KeyboardEvent(type, initArg);
var target;
var keyboardListenerMap = __listenerMap.keyboard;
for (let key in keyboardListenerMap) {
target = keyboardListenerMap[key];
target.dispatchEvent(keyboardEvent);
}
}
}
jsb.onKeyDown = keyboardEventHandlerFactory('keydown');
jsb.onKeyUp = keyboardEventHandlerFactory('keyup');
module.exports = EventTarget
+109
View File
@@ -0,0 +1,109 @@
/****************************************************************************
Copyright (c) 2018 Xiamen Yaji Software Co., Ltd.
http://www.cocos.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated engine source code (the "Software"), a limited,
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
to use Cocos Creator solely to develop games on your target platforms. You shall
not use Cocos Creator software for developing other software or tools that's
used for developing games. You are not granted to publish, distribute,
sublicense, and/or sell copies of Cocos Creator.
The software or tools in this License Agreement are licensed, not sold.
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
const Event = require('./Event')
class KeyboardEvent extends Event {
constructor(type, KeyboardEventInit) {
super(type)
if (typeof KeyboardEventInit === 'object') {
this._altKeyActive = KeyboardEventInit.altKey ? KeyboardEventInit.altKey : false;
this._ctrlKeyActive = KeyboardEventInit.ctrlKey ? KeyboardEventInit.ctrlKey : false;
this._metaKeyActive = KeyboardEventInit.metaKey ? KeyboardEventInit.metaKey : false;
this._shiftKeyActive = KeyboardEventInit.shiftKey ? KeyboardEventInit.shiftKey : false;
this._code = KeyboardEventInit.code ? KeyboardEventInit.code : '';
this._key = KeyboardEventInit.key ? KeyboardEventInit.key : '';
this._keyCode = KeyboardEventInit.keyCode ? KeyboardEventInit.keyCode : -1;
this._repeat = KeyboardEventInit.repeat ? KeyboardEventInit.repeat : false;
}
else {
this._altKeyActive = false;
this._ctrlKeyActive = false;
this._metaKeyActive = false;
this._shiftKeyActive = false;
this._code = "";
this._key = "";
this._keyCode = -1;
this._repeat = false;
}
}
// Returns a Boolean indicating if the modifier key, like Alt, Shift, Ctrl, or Meta, was pressed when the event was created.
getModifierState() {
return false;
}
// Returns a Boolean that is true if the Alt ( Option or ⌥ on OS X) key was active when the key event was generated.
get altKey() {
return this._altKeyActive;
}
// Returns a DOMString with the code value of the key represented by the event.
get code() {
return this._code;
}
// Returns a Boolean that is true if the Ctrl key was active when the key event was generated.
get ctrlKey() {
return this._ctrlKeyActive;
}
// Returns a Boolean that is true if the event is fired between after compositionstart and before compositionend.
get isComposing() {
return false;
}
// Returns a DOMString representing the key value of the key represented by the event.
get key() {
return this._key;
}
get keyCode() {
return this._keyCode;
}
// Returns a Number representing the location of the key on the keyboard or other input device.
get location() {
return 0;
}
// Returns a Boolean that is true if the Meta key (on Mac keyboards, the ⌘ Command key; on Windows keyboards, the Windows key (⊞)) was active when the key event was generated.
get metaKey() {
return this._metaKeyActive;
}
// Returns a Boolean that is true if the key is being held down such that it is automatically repeating.
get repeat() {
return this._repeat;
}
// Returns a Boolean that is true if the Shift key was active when the key event was generated.
get shiftKey() {
return this._shiftKeyActive;
}
}
module.exports = KeyboardEvent
+1
View File
@@ -59,6 +59,7 @@ function inject () {
window.Event = require('./Event');
window.TouchEvent = require('./TouchEvent');
window.MouseEvent = require('./MouseEvent');
window.KeyboardEvent = require('./KeyboardEvent');
window.devicePixelRatio = 1.0;
window.screen = {