|
|
|
|
|
|
|
|
145:"scrolllock", |
|
|
145:"scrolllock", |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var KEYBYNAME = (function(){ |
|
|
var KEYBYNAME = (function(){ |
|
|
var keys = Object.keys(KEYBYCODE); |
|
|
var keys = Object.keys(KEYBYCODE); |
|
|
var o = {}; |
|
|
var o = {}; |
|
|
|
|
|
|
|
|
return (code in Object.keys(KEYBYCODE)) ? KEYBYCODE[code] : "" + code; |
|
|
return (code in Object.keys(KEYBYCODE)) ? KEYBYCODE[code] : "" + code; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
function CodesToEventName(codes){ |
|
|
|
|
|
|
|
|
function CodesToEventName(codes, mouse){ |
|
|
var ename = ""; |
|
|
var ename = ""; |
|
|
|
|
|
mouse = (mouse === true); |
|
|
for (var i=0; i < codes.length; i++){ |
|
|
for (var i=0; i < codes.length; i++){ |
|
|
ename += ((ename !== "") ? "+" : "") + CodeToKeyName(codes[i]); |
|
|
|
|
|
|
|
|
if (mouse){ |
|
|
|
|
|
switch(codes[i]){ |
|
|
|
|
|
case 0: |
|
|
|
|
|
ename += ((ename !== "") ? "+" : "") + "mouseleft"; |
|
|
|
|
|
break; |
|
|
|
|
|
case 1: |
|
|
|
|
|
ename += ((ename !== "") ? "+" : "") + "mouseright"; |
|
|
|
|
|
break; |
|
|
|
|
|
case 2: |
|
|
|
|
|
ename += ((ename !== "") ? "+" : "") + "mousemiddle"; |
|
|
|
|
|
break; |
|
|
|
|
|
case 8000: |
|
|
|
|
|
ename += ((ename !== "") ? "+" : "") + "mousemove"; |
|
|
|
|
|
break; |
|
|
|
|
|
default: |
|
|
|
|
|
ename += ((ename !== "") ? "+" : "") + "mousebtn" + codes[i].toString(); |
|
|
|
|
|
} |
|
|
|
|
|
} else { |
|
|
|
|
|
ename += ((ename !== "") ? "+" : "") + CodeToKeyName(codes[i]); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
return ename; |
|
|
return ename; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function ReorderEventName(ename){ |
|
|
function ReorderEventName(ename){ |
|
|
// This function takes a keyboard event name and reorders it into key-code order. |
|
|
|
|
|
|
|
|
// This function takes a keyboard and mouse event name and reorders it into key-code order. |
|
|
// This way users can write the event any way they want, but should still result in proper |
|
|
// This way users can write the event any way they want, but should still result in proper |
|
|
// event being called. |
|
|
// event being called. |
|
|
var elist = ename.split("+"); |
|
|
var elist = ename.split("+"); |
|
|
|
|
|
// TODO: Need to test for duplicate event names for both keyboard and mouse event names. |
|
|
var ecodes = []; |
|
|
var ecodes = []; |
|
|
|
|
|
var mcodes = []; |
|
|
for (var i=0; i < elist.length; i++){ |
|
|
for (var i=0; i < elist.length; i++){ |
|
|
var key = elist[i].trim().toLowerCase(); |
|
|
var key = elist[i].trim().toLowerCase(); |
|
|
|
|
|
|
|
|
|
|
|
// Check for mouse events first. These are hardcoded for now. |
|
|
|
|
|
if (key === "mouseleft"){ |
|
|
|
|
|
mcodes.push(0); |
|
|
|
|
|
} else if (key === "mouseright"){ |
|
|
|
|
|
mcodes.push(1); |
|
|
|
|
|
} else if (key === "mousemiddle"){ |
|
|
|
|
|
mcodes.push(2); |
|
|
|
|
|
} else if (key.startsWith("mousebtn")){ |
|
|
|
|
|
var sub = key.substring(8); |
|
|
|
|
|
if (!Number.isNaN(sub)){ |
|
|
|
|
|
mcodes.push(parseInt(sub)); |
|
|
|
|
|
} else { |
|
|
|
|
|
return ""; // This event name does not include valid mouse button code. |
|
|
|
|
|
} |
|
|
|
|
|
} else if (key === "mousemove"){ |
|
|
|
|
|
mcodes.push(8000); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Now handle keyboard event names. |
|
|
if (!(key in Object.keys(KEYBYNAME))){ |
|
|
if (!(key in Object.keys(KEYBYNAME))){ |
|
|
if (!Number.isNaN(key)) |
|
|
if (!Number.isNaN(key)) |
|
|
ecodes.push(parseInt(key)); |
|
|
ecodes.push(parseInt(key)); |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
if (ecodes.length > 0){ |
|
|
if (ecodes.length > 0){ |
|
|
ecodes.sort(function(a, b){return a-b;}); |
|
|
ecodes.sort(function(a, b){return a-b;}); |
|
|
return CodesToEventName(ecodes); |
|
|
|
|
|
|
|
|
mcodes.sort(function(a, b){return a-b;}); |
|
|
|
|
|
return CodesToEventName(ecodes) + CodesToEventName(mcodes); |
|
|
} |
|
|
} |
|
|
return ""; |
|
|
return ""; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
this.__keyboardEnabled = false; |
|
|
this.__keyboardEnabled = false; |
|
|
this.__mouseEnabled = false; |
|
|
this.__mouseEnabled = false; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var buttonID = (function(e){ |
|
|
|
|
|
var btn = e.button; |
|
|
|
|
|
if ((this.__ieMouseMode && btn === 1) || (!this.__ieMouseMode && btn === 0)){ |
|
|
|
|
|
btn = 0; |
|
|
|
|
|
} else if ((this.__ieMouseMode && e.button === 4) || (!this.__ieMouseMode && e.button === 1)){ |
|
|
|
|
|
btn = 1; |
|
|
|
|
|
} |
|
|
|
|
|
return btn; |
|
|
|
|
|
}).bind(this); |
|
|
|
|
|
|
|
|
|
|
|
var addMouseButton = (function(btn){ |
|
|
|
|
|
if (this.__mouseButtons.findIndex(b=>b[0]===btn) < 0){ |
|
|
|
|
|
this.__mouseButtons.push([btn, Math.floor(Date.now())]); |
|
|
|
|
|
return true; |
|
|
|
|
|
} |
|
|
|
|
|
return false; |
|
|
|
|
|
}).bind(this); |
|
|
|
|
|
|
|
|
|
|
|
var removeMouseButton = (function(btn){ |
|
|
|
|
|
var idx = this.__mouseButtons.findIndex(b=>b[0]===btn); |
|
|
|
|
|
var diff = -1; |
|
|
|
|
|
if (idx >= 0){ |
|
|
|
|
|
diff = Math.floor(Date.now()) - this.__mouseButtons[idx][1]; |
|
|
|
|
|
this.__mouseButtons.splice(idx, 1); |
|
|
|
|
|
} |
|
|
|
|
|
return diff; |
|
|
|
|
|
}).bind(this); |
|
|
|
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------- |
|
|
|
|
|
// Handling keyboard events. |
|
|
|
|
|
|
|
|
this.enableKeyboardInput = (function(){ |
|
|
this.enableKeyboardInput = (function(){ |
|
|
var handle_keydown = (function(e){ |
|
|
var handle_keydown = (function(e){ |
|
|
if (AddToKeymap(e.keyCode, "keydown")){ |
|
|
if (AddToKeymap(e.keyCode, "keydown")){ |
|
|
|
|
|
|
|
|
}).bind(this); |
|
|
}).bind(this); |
|
|
}).apply(this); |
|
|
}).apply(this); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------------------- |
|
|
|
|
|
// Handling mouse events. |
|
|
|
|
|
|
|
|
|
|
|
var MouseButtonsEventName = (function(){ |
|
|
|
|
|
var e = ""; |
|
|
|
|
|
for (var i=0; i < this.__mouseButtons.length; i++){ |
|
|
|
|
|
e += (e !== "") ? "+" : ""; |
|
|
|
|
|
switch (this.__mouseButtons[i]){ |
|
|
|
|
|
case 0: |
|
|
|
|
|
e += "mouseleft"; |
|
|
|
|
|
break; |
|
|
|
|
|
case 1: |
|
|
|
|
|
e += "mouseright"; |
|
|
|
|
|
break; |
|
|
|
|
|
case 2: |
|
|
|
|
|
e += "mousemiddle"; |
|
|
|
|
|
break; |
|
|
|
|
|
default: |
|
|
|
|
|
e += "mousebtn" + this.__mouseButtons[i].toString(); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
return e; |
|
|
|
|
|
}).bind(this); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// This function will only return an event name if keyboard |
|
|
|
|
|
var MouseEventName = (function(addon){ |
|
|
|
|
|
var ename = KeymapEventName(); |
|
|
|
|
|
var mname = MouseButtonsEventName(); |
|
|
|
|
|
if (mname !== "") |
|
|
|
|
|
ename += ((ename !== "") ? "+" : "") + mname; |
|
|
|
|
|
if (typeof(addon) === "string") |
|
|
|
|
|
ename += ((ename !== "") ? "+" : "") + addon; |
|
|
|
|
|
return ename; |
|
|
|
|
|
}).bind(this); |
|
|
|
|
|
|
|
|
this.enableMouseInput = (function(){ |
|
|
this.enableMouseInput = (function(){ |
|
|
var mousePosition = (function(e){ |
|
|
var mousePosition = (function(e){ |
|
|
var pos = { |
|
|
var pos = { |
|
|
|
|
|
|
|
|
pos.x = Math.floor(pos.x); |
|
|
pos.x = Math.floor(pos.x); |
|
|
pos.y = Math.floor(pos.y); |
|
|
pos.y = Math.floor(pos.y); |
|
|
return pos; |
|
|
return pos; |
|
|
}).bind(this); |
|
|
|
|
|
|
|
|
|
|
|
var buttonID = (function(e){ |
|
|
|
|
|
var btn = e.button; |
|
|
|
|
|
if ((this.__ieMouseMode && btn === 1) || (!this.__ieMouseMode && btn === 0)){ |
|
|
|
|
|
btn = 0; |
|
|
|
|
|
} else if ((this.__ieMouseMode && e.button === 4) || (!this.__ieMouseMode && e.button === 1)){ |
|
|
|
|
|
btn = 1; |
|
|
|
|
|
} |
|
|
|
|
|
return btn; |
|
|
|
|
|
}).bind(this); |
|
|
|
|
|
|
|
|
|
|
|
var addMouseButton = (function(btn){ |
|
|
|
|
|
if (this.__mouseButtons.findIndex(b=>b[0]===btn) < 0){ |
|
|
|
|
|
this.__mouseButtons.push([btn, Math.floor(Date.now())]); |
|
|
|
|
|
return true; |
|
|
|
|
|
} |
|
|
|
|
|
return false; |
|
|
|
|
|
}).bind(this); |
|
|
|
|
|
|
|
|
|
|
|
var removeMouseButton = (function(btn){ |
|
|
|
|
|
var idx = this.__mouseButtons.findIndex(b=>b[0]===btn); |
|
|
|
|
|
var diff = -1; |
|
|
|
|
|
if (idx >= 0){ |
|
|
|
|
|
diff = Math.floor(Date.now()) - this.__mouseButtons[idx][1]; |
|
|
|
|
|
this.__mouseButtons.splice(idx, 1); |
|
|
|
|
|
} |
|
|
|
|
|
return diff; |
|
|
|
|
|
}).bind(this); |
|
|
|
|
|
|
|
|
}).bind(this); |
|
|
|
|
|
|
|
|
var handle_mousemove = (function(e){ |
|
|
var handle_mousemove = (function(e){ |
|
|
var pos = mousePosition(e); |
|
|
var pos = mousePosition(e); |
|
|
|
|
|
|
|
|
if (e.stopPropagation) |
|
|
if (e.stopPropagation) |
|
|
e.stopPropagation(); |
|
|
e.stopPropagation(); |
|
|
e.cancelBubble = true; |
|
|
e.cancelBubble = true; |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
this.__mousePosition = pos; |
|
|
this.__mousePosition = pos; |
|
|
this.__mouseLastAction = "mousemove"; |
|
|
this.__mouseLastAction = "mousemove"; |
|
|
this.__emitter.emit("mousemove", { |
|
|
|
|
|
|
|
|
var ename = MouseEventName(); |
|
|
|
|
|
var data = { |
|
|
source: this, |
|
|
source: this, |
|
|
lastX: pos.lastX, |
|
|
lastX: pos.lastX, |
|
|
lastY: pos.lastY, |
|
|
lastY: pos.lastY, |
|
|
|
|
|
|
|
|
y: pos.y, |
|
|
y: pos.y, |
|
|
button: this.__mouseLastButton, |
|
|
button: this.__mouseLastButton, |
|
|
action: "mousemove" |
|
|
action: "mousemove" |
|
|
}); |
|
|
|
|
|
|
|
|
}; |
|
|
|
|
|
if (ename !== "" && ename !== "mousemove") |
|
|
|
|
|
this.__emitter.emit(ename, data); |
|
|
|
|
|
this.__emitter.emit("mousemove", data); |
|
|
} |
|
|
} |
|
|
return false; |
|
|
return false; |
|
|
}).bind(this); |
|
|
}).bind(this); |
|
|
|
|
|
|
|
|
e.cancelBubble = true; |
|
|
e.cancelBubble = true; |
|
|
} |
|
|
} |
|
|
if (addMouseButton(button)){ |
|
|
if (addMouseButton(button)){ |
|
|
this.__mousePosition = pos; |
|
|
|
|
|
this.__mouseLastButton = button; |
|
|
|
|
|
this.__mouseLastAction = "mousedown"; |
|
|
|
|
|
this.__emitter.emit("mousedown", { |
|
|
|
|
|
|
|
|
var ename = MouseEventName(); |
|
|
|
|
|
var data = { |
|
|
source: this, |
|
|
source: this, |
|
|
lastX: pos.lastX, |
|
|
lastX: pos.lastX, |
|
|
lastY: pos.lastY, |
|
|
lastY: pos.lastY, |
|
|
|
|
|
|
|
|
y: pos.y, |
|
|
y: pos.y, |
|
|
button: button, |
|
|
button: button, |
|
|
action: "mousedown" |
|
|
action: "mousedown" |
|
|
}); |
|
|
|
|
|
|
|
|
}; |
|
|
|
|
|
this.__mousePosition = pos; |
|
|
|
|
|
this.__mouseLastButton = button; |
|
|
|
|
|
this.__mouseLastAction = "mousedown"; |
|
|
|
|
|
if (ename !== "") |
|
|
|
|
|
this.__emitter.emit(ename, data); |
|
|
|
|
|
this.__emitter.emit("mousedown", data); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
return false; |
|
|
return false; |