This repository has been archived by the owner on May 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Keyboard.js
79 lines (73 loc) · 2.47 KB
/
Keyboard.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
var EventDispatcher = require('./EventDispatcher');
var KeyboardEvent = require('./KeyboardEvent');
/**
* EventDispatcher for keyboard events
* @class
*
* @example
* var Window = require('pex-sys').Window;
* var KeyboardEvent = require('pex-sys').KeyboardEvent;
*
* Window.create({
* init: function() {
* var kbd = this.getKeyboard()
* kbd.addEventListener(KeyboardEvent.KEY_DOWN, function(e){ });
* kbd.addEventListener(KeyboardEvent.KEY_PRESS, function(e){ });
* kbd.addEventListener(KeyboardEvent.KEY_UP, function(e){ });
* }
* })
*/
function Keyboard() {
EventDispatcher.call(this);
}
Keyboard.prototype = Object.create(EventDispatcher.prototype);
Keyboard.prototype.constructor = Keyboard;
/**
* Fires KEY_DOWN event
* @protected
*
* @param {Object} e - event data
* @param {String} e.str - '' (not used for KEY_DOWN)
* @param {Number} e.keyCode - key code (not ASCI code)
* @param {Boolean} e.altKey - is alt key pressed?
* @param {Boolean} e.shiftKey - is shift key pressed?
* @param {Boolean} e.ctrlKey - is ctrl key pressed?
* @param {Boolean} e.metaKey - is meta (apple/win) key pressed?
*/
Keyboard.prototype.handleKeyDown = function(e) {
e.keyboard = this;
this.dispatchEvent(new KeyboardEvent(KeyboardEvent.KEY_DOWN, e));
}
/**
* Fires KEY_PRESS event
* @protected
*
* @param {Object} e - event data
* @param {String} e.str - string character representing that key
* @param {Number} e.keyCode - key code (not ASCI code)
* @param {Boolean} e.altKey - is alt key pressed?
* @param {Boolean} e.shiftKey - is shift key pressed?
* @param {Boolean} e.ctrlKey - is ctrl key pressed?
* @param {Boolean} e.metaKey - is meta (apple/win) key pressed?
*/
Keyboard.prototype.handleKeyPress = function(e) {
e.keyboard = this;
this.dispatchEvent(new KeyboardEvent(KeyboardEvent.KEY_PRESS, e));
}
/**
* Fires KEY_UP event
* @protected
*
* @param {Object} e - event data
* @param {String} e.str - '' (not used for KEY_UP)
* @param {Number} e.keyCode - key code (not ASCI code)
* @param {Boolean} e.altKey - is alt key pressed?
* @param {Boolean} e.shiftKey - is shift key pressed?
* @param {Boolean} e.ctrlKey - is ctrl key pressed?
* @param {Boolean} e.metaKey - is meta (apple/win) key pressed?
*/
Keyboard.prototype.handleKeyUp = function(e) {
e.keyboard = this;
this.dispatchEvent(new KeyboardEvent(KeyboardEvent.KEY_UP, e));
}
module.exports = Keyboard;