source: pro-violet-viettel/sourcecode/assets/js/jquery/hotkeys.js @ 574

Last change on this file since 574 was 289, checked in by dungnv, 11 years ago
File size: 2.9 KB
Line 
1(function() {
2
3var $ = jQuery; // Handle namespaced jQuery
4
5// DocumentCloud workspace hotkeys. To tell if a key is currently being pressed,
6// just ask `VS.app.hotkeys.[key]` on `keypress`, or ask `VS.app.hotkeys.key(e)`
7// on `keydown`.
8//
9// For the most headache-free way to use this utility, check modifier keys,
10// like shift and command, with `VS.app.hotkeys.shift`, and check every other
11// key with `VS.app.hotkeys.key(e) == 'key_name'`.
12VS.app.hotkeys = {
13
14  // Keys that will be mapped to the `hotkeys` namespace.
15  KEYS: {
16    '16':  'shift',
17    '17':  'command',
18    '91':  'command',
19    '93':  'command',
20    '224': 'command',
21    '13':  'enter',
22    '37':  'left',
23    '38':  'upArrow',
24    '39':  'right',
25    '40':  'downArrow',
26    '46':  'delete',
27    '8':   'backspace',
28    '35':  'end',
29    '36':  'home',
30    '9':   'tab',
31    '188': 'comma'
32  },
33
34  // Binds global keydown and keyup events to listen for keys that match `this.KEYS`.
35  initialize : function() {
36    _.bindAll(this, 'down', 'up', 'blur');
37    $(document).bind('keydown', this.down);
38    $(document).bind('keyup', this.up);
39    $(window).bind('blur', this.blur);
40  },
41
42  // On `keydown`, turn on all keys that match.
43  down : function(e) {
44    var key = this.KEYS[e.which];
45    if (key) this[key] = true;
46  },
47
48  // On `keyup`, turn off all keys that match.
49  up : function(e) {
50    var key = this.KEYS[e.which];
51    if (key) this[key] = false;
52  },
53
54  // If an input is blurred, all keys need to be turned off, since they are no longer
55  // able to modify the document.
56  blur : function(e) {
57    for (var key in this.KEYS) this[this.KEYS[key]] = false;
58  },
59
60  // Check a key from an event and return the common english name.
61  key : function(e) {
62    return this.KEYS[e.which];
63  },
64
65  // Colon is special, since the value is different between browsers.
66  colon : function(e) {
67    var charCode = e.which;
68    return charCode && String.fromCharCode(charCode) == ":";
69  },
70
71  // Check a key from an event and match it against any known characters.
72  // The `keyCode` is different depending on the event type: `keydown` vs. `keypress`.
73  //
74  // These were determined by looping through every `keyCode` and `charCode` that
75  // resulted from `keydown` and `keypress` events and counting what was printable.
76  printable : function(e) {
77    var code = e.which;
78    if (e.type == 'keydown') {
79      if (code == 32 ||                      // space
80          (code >= 48 && code <= 90) ||      // 0-1a-z
81          (code >= 96 && code <= 111) ||     // 0-9+-/*.
82          (code >= 186 && code <= 192) ||    // ;=,-./^
83          (code >= 219 && code <= 222)) {    // (\)'
84        return true;
85      }
86    } else {
87      // [space]!"#$%&'()*+,-.0-9:;<=>?@A-Z[\]^_`a-z{|} and unicode characters
88      if ((code >= 32 && code <= 126)  ||
89          (code >= 160 && code <= 500) ||
90          (String.fromCharCode(code) == ":")) {
91        return true;
92      }
93    }
94    return false;
95  }
96
97};
98
99})();
Note: See TracBrowser for help on using the repository browser.