keyboard.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. var Locale = require('./locale');
  2. var KeyCombo = require('./key-combo');
  3. function Keyboard(targetWindow, targetElement, platform, userAgent) {
  4. this._locale = null;
  5. this._currentContext = null;
  6. this._contexts = {};
  7. this._listeners = [];
  8. this._appliedListeners = [];
  9. this._locales = {};
  10. this._targetElement = null;
  11. this._targetWindow = null;
  12. this._targetPlatform = '';
  13. this._targetUserAgent = '';
  14. this._isModernBrowser = false;
  15. this._targetKeyDownBinding = null;
  16. this._targetKeyUpBinding = null;
  17. this._targetResetBinding = null;
  18. this._paused = false;
  19. this.setContext('global');
  20. this.watch(targetWindow, targetElement, platform, userAgent);
  21. }
  22. Keyboard.prototype.setLocale = function(localeName, localeBuilder) {
  23. var locale = null;
  24. if (typeof localeName === 'string') {
  25. if (localeBuilder) {
  26. locale = new Locale(localeName);
  27. localeBuilder(locale, this._targetPlatform, this._targetUserAgent);
  28. } else {
  29. locale = this._locales[localeName] || null;
  30. }
  31. } else {
  32. locale = localeName;
  33. localeName = locale._localeName;
  34. }
  35. this._locale = locale;
  36. this._locales[localeName] = locale;
  37. if (locale) {
  38. this._locale.pressedKeys = locale.pressedKeys;
  39. }
  40. };
  41. Keyboard.prototype.getLocale = function(localName) {
  42. localName || (localName = this._locale.localeName);
  43. return this._locales[localName] || null;
  44. };
  45. Keyboard.prototype.bind = function(keyComboStr, pressHandler, releaseHandler, preventRepeatByDefault) {
  46. if (keyComboStr === null || typeof keyComboStr === 'function') {
  47. preventRepeatByDefault = releaseHandler;
  48. releaseHandler = pressHandler;
  49. pressHandler = keyComboStr;
  50. keyComboStr = null;
  51. }
  52. if (
  53. keyComboStr &&
  54. typeof keyComboStr === 'object' &&
  55. typeof keyComboStr.length === 'number'
  56. ) {
  57. for (var i = 0; i < keyComboStr.length; i += 1) {
  58. this.bind(keyComboStr[i], pressHandler, releaseHandler);
  59. }
  60. return;
  61. }
  62. this._listeners.push({
  63. keyCombo : keyComboStr ? new KeyCombo(keyComboStr) : null,
  64. pressHandler : pressHandler || null,
  65. releaseHandler : releaseHandler || null,
  66. preventRepeat : preventRepeatByDefault || false,
  67. preventRepeatByDefault : preventRepeatByDefault || false
  68. });
  69. };
  70. Keyboard.prototype.addListener = Keyboard.prototype.bind;
  71. Keyboard.prototype.on = Keyboard.prototype.bind;
  72. Keyboard.prototype.unbind = function(keyComboStr, pressHandler, releaseHandler) {
  73. if (keyComboStr === null || typeof keyComboStr === 'function') {
  74. releaseHandler = pressHandler;
  75. pressHandler = keyComboStr;
  76. keyComboStr = null;
  77. }
  78. if (
  79. keyComboStr &&
  80. typeof keyComboStr === 'object' &&
  81. typeof keyComboStr.length === 'number'
  82. ) {
  83. for (var i = 0; i < keyComboStr.length; i += 1) {
  84. this.unbind(keyComboStr[i], pressHandler, releaseHandler);
  85. }
  86. return;
  87. }
  88. for (var i = 0; i < this._listeners.length; i += 1) {
  89. var listener = this._listeners[i];
  90. var comboMatches = !keyComboStr && !listener.keyCombo ||
  91. listener.keyCombo && listener.keyCombo.isEqual(keyComboStr);
  92. var pressHandlerMatches = !pressHandler && !releaseHandler ||
  93. !pressHandler && !listener.pressHandler ||
  94. pressHandler === listener.pressHandler;
  95. var releaseHandlerMatches = !pressHandler && !releaseHandler ||
  96. !releaseHandler && !listener.releaseHandler ||
  97. releaseHandler === listener.releaseHandler;
  98. if (comboMatches && pressHandlerMatches && releaseHandlerMatches) {
  99. this._listeners.splice(i, 1);
  100. i -= 1;
  101. }
  102. }
  103. };
  104. Keyboard.prototype.removeListener = Keyboard.prototype.unbind;
  105. Keyboard.prototype.off = Keyboard.prototype.unbind;
  106. Keyboard.prototype.setContext = function(contextName) {
  107. if(this._locale) { this.releaseAllKeys(); }
  108. if (!this._contexts[contextName]) {
  109. this._contexts[contextName] = [];
  110. }
  111. this._listeners = this._contexts[contextName];
  112. this._currentContext = contextName;
  113. };
  114. Keyboard.prototype.getContext = function() {
  115. return this._currentContext;
  116. };
  117. Keyboard.prototype.withContext = function(contextName, callback) {
  118. var previousContextName = this.getContext();
  119. this.setContext(contextName);
  120. callback();
  121. this.setContext(previousContextName);
  122. };
  123. Keyboard.prototype.watch = function(targetWindow, targetElement, targetPlatform, targetUserAgent) {
  124. var _this = this;
  125. this.stop();
  126. if (!targetWindow) {
  127. if (!global.addEventListener && !global.attachEvent) {
  128. throw new Error('Cannot find global functions addEventListener or attachEvent.');
  129. }
  130. targetWindow = global;
  131. }
  132. if (typeof targetWindow.nodeType === 'number') {
  133. targetUserAgent = targetPlatform;
  134. targetPlatform = targetElement;
  135. targetElement = targetWindow;
  136. targetWindow = global;
  137. }
  138. if (!targetWindow.addEventListener && !targetWindow.attachEvent) {
  139. throw new Error('Cannot find addEventListener or attachEvent methods on targetWindow.');
  140. }
  141. this._isModernBrowser = !!targetWindow.addEventListener;
  142. var userAgent = targetWindow.navigator && targetWindow.navigator.userAgent || '';
  143. var platform = targetWindow.navigator && targetWindow.navigator.platform || '';
  144. targetElement && targetElement !== null || (targetElement = targetWindow.document);
  145. targetPlatform && targetPlatform !== null || (targetPlatform = platform);
  146. targetUserAgent && targetUserAgent !== null || (targetUserAgent = userAgent);
  147. this._targetKeyDownBinding = function(event) {
  148. _this.pressKey(event.keyCode, event);
  149. };
  150. this._targetKeyUpBinding = function(event) {
  151. _this.releaseKey(event.keyCode, event);
  152. };
  153. this._targetResetBinding = function(event) {
  154. _this.releaseAllKeys(event)
  155. };
  156. this._bindEvent(targetElement, 'keydown', this._targetKeyDownBinding);
  157. this._bindEvent(targetElement, 'keyup', this._targetKeyUpBinding);
  158. this._bindEvent(targetWindow, 'focus', this._targetResetBinding);
  159. this._bindEvent(targetWindow, 'blur', this._targetResetBinding);
  160. this._targetElement = targetElement;
  161. this._targetWindow = targetWindow;
  162. this._targetPlatform = targetPlatform;
  163. this._targetUserAgent = targetUserAgent;
  164. };
  165. Keyboard.prototype.stop = function() {
  166. var _this = this;
  167. if (!this._targetElement || !this._targetWindow) { return; }
  168. this._unbindEvent(this._targetElement, 'keydown', this._targetKeyDownBinding);
  169. this._unbindEvent(this._targetElement, 'keyup', this._targetKeyUpBinding);
  170. this._unbindEvent(this._targetWindow, 'focus', this._targetResetBinding);
  171. this._unbindEvent(this._targetWindow, 'blur', this._targetResetBinding);
  172. this._targetWindow = null;
  173. this._targetElement = null;
  174. };
  175. Keyboard.prototype.pressKey = function(keyCode, event) {
  176. if (this._paused) { return; }
  177. if (!this._locale) { throw new Error('Locale not set'); }
  178. this._locale.pressKey(keyCode);
  179. this._applyBindings(event);
  180. };
  181. Keyboard.prototype.releaseKey = function(keyCode, event) {
  182. if (this._paused) { return; }
  183. if (!this._locale) { throw new Error('Locale not set'); }
  184. this._locale.releaseKey(keyCode);
  185. this._clearBindings(event);
  186. };
  187. Keyboard.prototype.releaseAllKeys = function(event) {
  188. if (this._paused) { return; }
  189. if (!this._locale) { throw new Error('Locale not set'); }
  190. this._locale.pressedKeys.length = 0;
  191. this._clearBindings(event);
  192. };
  193. Keyboard.prototype.pause = function() {
  194. if (this._paused) { return; }
  195. if (this._locale) { this.releaseAllKeys(); }
  196. this._paused = true;
  197. };
  198. Keyboard.prototype.resume = function() {
  199. this._paused = false;
  200. };
  201. Keyboard.prototype.reset = function() {
  202. this.releaseAllKeys();
  203. this._listeners.length = 0;
  204. };
  205. Keyboard.prototype._bindEvent = function(targetElement, eventName, handler) {
  206. return this._isModernBrowser ?
  207. targetElement.addEventListener(eventName, handler, false) :
  208. targetElement.attachEvent('on' + eventName, handler);
  209. };
  210. Keyboard.prototype._unbindEvent = function(targetElement, eventName, handler) {
  211. return this._isModernBrowser ?
  212. targetElement.removeEventListener(eventName, handler, false) :
  213. targetElement.detachEvent('on' + eventName, handler);
  214. };
  215. Keyboard.prototype._getGroupedListeners = function() {
  216. var listenerGroups = [];
  217. var listenerGroupMap = [];
  218. var listeners = this._listeners;
  219. if (this._currentContext !== 'global') {
  220. listeners = [].concat(listeners, this._contexts.global);
  221. }
  222. listeners.sort(function(a, b) {
  223. return (b.keyCombo ? b.keyCombo.keyNames.length : 0) - (a.keyCombo ? a.keyCombo.keyNames.length : 0);
  224. }).forEach(function(l) {
  225. var mapIndex = -1;
  226. for (var i = 0; i < listenerGroupMap.length; i += 1) {
  227. if (listenerGroupMap[i] === null && l.keyCombo === null ||
  228. listenerGroupMap[i] !== null && listenerGroupMap[i].isEqual(l.keyCombo)) {
  229. mapIndex = i;
  230. }
  231. }
  232. if (mapIndex === -1) {
  233. mapIndex = listenerGroupMap.length;
  234. listenerGroupMap.push(l.keyCombo);
  235. }
  236. if (!listenerGroups[mapIndex]) {
  237. listenerGroups[mapIndex] = [];
  238. }
  239. listenerGroups[mapIndex].push(l);
  240. });
  241. return listenerGroups;
  242. };
  243. Keyboard.prototype._applyBindings = function(event) {
  244. var preventRepeat = false;
  245. event || (event = {});
  246. event.preventRepeat = function() { preventRepeat = true; };
  247. event.pressedKeys = this._locale.pressedKeys.slice(0);
  248. var pressedKeys = this._locale.pressedKeys.slice(0);
  249. var listenerGroups = this._getGroupedListeners();
  250. for (var i = 0; i < listenerGroups.length; i += 1) {
  251. var listeners = listenerGroups[i];
  252. var keyCombo = listeners[0].keyCombo;
  253. if (keyCombo === null || keyCombo.check(pressedKeys)) {
  254. for (var j = 0; j < listeners.length; j += 1) {
  255. var listener = listeners[j];
  256. if (keyCombo === null) {
  257. listener = {
  258. keyCombo : new KeyCombo(pressedKeys.join('+')),
  259. pressHandler : listener.pressHandler,
  260. releaseHandler : listener.releaseHandler,
  261. preventRepeat : listener.preventRepeat,
  262. preventRepeatByDefault : listener.preventRepeatByDefault
  263. };
  264. }
  265. if (listener.pressHandler && !listener.preventRepeat) {
  266. listener.pressHandler.call(this, event);
  267. if (preventRepeat) {
  268. listener.preventRepeat = preventRepeat;
  269. preventRepeat = false;
  270. }
  271. }
  272. if (listener.releaseHandler && this._appliedListeners.indexOf(listener) === -1) {
  273. this._appliedListeners.push(listener);
  274. }
  275. }
  276. if (keyCombo) {
  277. for (var j = 0; j < keyCombo.keyNames.length; j += 1) {
  278. var index = pressedKeys.indexOf(keyCombo.keyNames[j]);
  279. if (index !== -1) {
  280. pressedKeys.splice(index, 1);
  281. j -= 1;
  282. }
  283. }
  284. }
  285. }
  286. }
  287. };
  288. Keyboard.prototype._clearBindings = function(event) {
  289. event || (event = {});
  290. for (var i = 0; i < this._appliedListeners.length; i += 1) {
  291. var listener = this._appliedListeners[i];
  292. var keyCombo = listener.keyCombo;
  293. if (keyCombo === null || !keyCombo.check(this._locale.pressedKeys)) {
  294. listener.preventRepeat = listener.preventRepeatByDefault;
  295. listener.releaseHandler.call(this, event);
  296. this._appliedListeners.splice(i, 1);
  297. i -= 1;
  298. }
  299. }
  300. };
  301. module.exports = Keyboard;