input.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /*
  2. * noVNC: HTML5 VNC client
  3. * Copyright (C) 2012 Joel Martin
  4. * Copyright (C) 2013 Samuel Mannehed for Cendio AB
  5. * Licensed under MPL 2.0 or any later version (see LICENSE.txt)
  6. */
  7. /*jslint browser: true, white: false */
  8. /*global window, Util */
  9. var Keyboard, Mouse;
  10. (function () {
  11. "use strict";
  12. //
  13. // Keyboard event handler
  14. //
  15. Keyboard = function (defaults) {
  16. this._keyDownList = []; // List of depressed keys
  17. // (even if they are happy)
  18. Util.set_defaults(this, defaults, {
  19. 'target': document,
  20. 'focused': true
  21. });
  22. // create the keyboard handler
  23. this._handler = new KeyEventDecoder(kbdUtil.ModifierSync(),
  24. VerifyCharModifier( /* jshint newcap: false */
  25. TrackKeyState(
  26. EscapeModifiers(this._handleRfbEvent.bind(this))
  27. )
  28. )
  29. ); /* jshint newcap: true */
  30. // keep these here so we can refer to them later
  31. this._eventHandlers = {
  32. 'keyup': this._handleKeyUp.bind(this),
  33. 'keydown': this._handleKeyDown.bind(this),
  34. 'keypress': this._handleKeyPress.bind(this),
  35. 'blur': this._allKeysUp.bind(this)
  36. };
  37. };
  38. Keyboard.prototype = {
  39. // private methods
  40. _handleRfbEvent: function (e) {
  41. if (this._onKeyPress) {
  42. Util.Debug("onKeyPress " + (e.type == 'keydown' ? "down" : "up") +
  43. ", keysym: " + e.keysym.keysym + "(" + e.keysym.keyname + ")");
  44. this._onKeyPress(e.keysym.keysym, e.type == 'keydown');
  45. }
  46. },
  47. _handleKeyDown: function (e) {
  48. if (!this._focused) { return true; }
  49. if (this._handler.keydown(e)) {
  50. // Suppress bubbling/default actions
  51. Util.stopEvent(e);
  52. return false;
  53. } else {
  54. // Allow the event to bubble and become a keyPress event which
  55. // will have the character code translated
  56. return true;
  57. }
  58. },
  59. _handleKeyPress: function (e) {
  60. if (!this._focused) { return true; }
  61. if (this._handler.keypress(e)) {
  62. // Suppress bubbling/default actions
  63. Util.stopEvent(e);
  64. return false;
  65. } else {
  66. // Allow the event to bubble and become a keyPress event which
  67. // will have the character code translated
  68. return true;
  69. }
  70. },
  71. _handleKeyUp: function (e) {
  72. if (!this._focused) { return true; }
  73. if (this._handler.keyup(e)) {
  74. // Suppress bubbling/default actions
  75. Util.stopEvent(e);
  76. return false;
  77. } else {
  78. // Allow the event to bubble and become a keyPress event which
  79. // will have the character code translated
  80. return true;
  81. }
  82. },
  83. _allKeysUp: function () {
  84. Util.Debug(">> Keyboard.allKeysUp");
  85. this._handler.releaseAll();
  86. Util.Debug("<< Keyboard.allKeysUp");
  87. },
  88. // Public methods
  89. grab: function () {
  90. //Util.Debug(">> Keyboard.grab");
  91. var c = this._target;
  92. Util.addEvent(c, 'keydown', this._eventHandlers.keydown);
  93. Util.addEvent(c, 'keyup', this._eventHandlers.keyup);
  94. Util.addEvent(c, 'keypress', this._eventHandlers.keypress);
  95. // Release (key up) if window loses focus
  96. Util.addEvent(window, 'blur', this._eventHandlers.blur);
  97. //Util.Debug("<< Keyboard.grab");
  98. },
  99. ungrab: function () {
  100. //Util.Debug(">> Keyboard.ungrab");
  101. var c = this._target;
  102. Util.removeEvent(c, 'keydown', this._eventHandlers.keydown);
  103. Util.removeEvent(c, 'keyup', this._eventHandlers.keyup);
  104. Util.removeEvent(c, 'keypress', this._eventHandlers.keypress);
  105. Util.removeEvent(window, 'blur', this._eventHandlers.blur);
  106. // Release (key up) all keys that are in a down state
  107. this._allKeysUp();
  108. //Util.Debug(">> Keyboard.ungrab");
  109. },
  110. sync: function (e) {
  111. this._handler.syncModifiers(e);
  112. }
  113. };
  114. Util.make_properties(Keyboard, [
  115. ['target', 'wo', 'dom'], // DOM element that captures keyboard input
  116. ['focused', 'rw', 'bool'], // Capture and send key events
  117. ['onKeyPress', 'rw', 'func'] // Handler for key press/release
  118. ]);
  119. //
  120. // Mouse event handler
  121. //
  122. Mouse = function (defaults) {
  123. this._mouseCaptured = false;
  124. this._doubleClickTimer = null;
  125. this._lastTouchPos = null;
  126. // Configuration attributes
  127. Util.set_defaults(this, defaults, {
  128. 'target': document,
  129. 'focused': true,
  130. 'scale': 1.0,
  131. 'touchButton': 1
  132. });
  133. this._eventHandlers = {
  134. 'mousedown': this._handleMouseDown.bind(this),
  135. 'mouseup': this._handleMouseUp.bind(this),
  136. 'mousemove': this._handleMouseMove.bind(this),
  137. 'mousewheel': this._handleMouseWheel.bind(this),
  138. 'mousedisable': this._handleMouseDisable.bind(this)
  139. };
  140. };
  141. Mouse.prototype = {
  142. // private methods
  143. _captureMouse: function () {
  144. // capturing the mouse ensures we get the mouseup event
  145. if (this._target.setCapture) {
  146. this._target.setCapture();
  147. }
  148. // some browsers give us mouseup events regardless,
  149. // so if we never captured the mouse, we can disregard the event
  150. this._mouseCaptured = true;
  151. },
  152. _releaseMouse: function () {
  153. if (this._target.releaseCapture) {
  154. this._target.releaseCapture();
  155. }
  156. this._mouseCaptured = false;
  157. },
  158. _resetDoubleClickTimer: function () {
  159. this._doubleClickTimer = null;
  160. },
  161. _handleMouseButton: function (e, down) {
  162. if (!this._focused) { return true; }
  163. if (this._notify) {
  164. this._notify(e);
  165. }
  166. var evt = (e ? e : window.event);
  167. var pos = Util.getEventPosition(e, this._target, this._scale);
  168. var bmask;
  169. if (e.touches || e.changedTouches) {
  170. // Touch device
  171. // When two touches occur within 500 ms of each other and are
  172. // close enough together a double click is triggered.
  173. if (down == 1) {
  174. if (this._doubleClickTimer === null) {
  175. this._lastTouchPos = pos;
  176. } else {
  177. clearTimeout(this._doubleClickTimer);
  178. // When the distance between the two touches is small enough
  179. // force the position of the latter touch to the position of
  180. // the first.
  181. var xs = this._lastTouchPos.x - pos.x;
  182. var ys = this._lastTouchPos.y - pos.y;
  183. var d = Math.sqrt((xs * xs) + (ys * ys));
  184. // The goal is to trigger on a certain physical width, the
  185. // devicePixelRatio brings us a bit closer but is not optimal.
  186. var threshold = 20 * (window.devicePixelRatio || 1);
  187. if (d < threshold) {
  188. pos = this._lastTouchPos;
  189. }
  190. }
  191. this._doubleClickTimer = setTimeout(this._resetDoubleClickTimer.bind(this), 500);
  192. }
  193. bmask = this._touchButton;
  194. // If bmask is set
  195. } else if (evt.which) {
  196. /* everything except IE */
  197. bmask = 1 << evt.button;
  198. } else {
  199. /* IE including 9 */
  200. bmask = (evt.button & 0x1) + // Left
  201. (evt.button & 0x2) * 2 + // Right
  202. (evt.button & 0x4) / 2; // Middle
  203. }
  204. if (this._onMouseButton) {
  205. Util.Debug("onMouseButton " + (down ? "down" : "up") +
  206. ", x: " + pos.x + ", y: " + pos.y + ", bmask: " + bmask);
  207. this._onMouseButton(pos.x, pos.y, down, bmask);
  208. }
  209. Util.stopEvent(e);
  210. return false;
  211. },
  212. _handleMouseDown: function (e) {
  213. this._captureMouse();
  214. this._handleMouseButton(e, 1);
  215. },
  216. _handleMouseUp: function (e) {
  217. if (!this._mouseCaptured) { return; }
  218. this._handleMouseButton(e, 0);
  219. this._releaseMouse();
  220. },
  221. _handleMouseWheel: function (e) {
  222. if (!this._focused) { return true; }
  223. if (this._notify) {
  224. this._notify(e);
  225. }
  226. var evt = (e ? e : window.event);
  227. var pos = Util.getEventPosition(e, this._target, this._scale);
  228. var wheelData = evt.detail ? evt.detail * -1 : evt.wheelDelta / 40;
  229. var bmask;
  230. if (wheelData > 0) {
  231. bmask = 1 << 3;
  232. } else {
  233. bmask = 1 << 4;
  234. }
  235. if (this._onMouseButton) {
  236. this._onMouseButton(pos.x, pos.y, 1, bmask);
  237. this._onMouseButton(pos.x, pos.y, 0, bmask);
  238. }
  239. Util.stopEvent(e);
  240. return false;
  241. },
  242. _handleMouseMove: function (e) {
  243. if (! this._focused) { return true; }
  244. if (this._notify) {
  245. this._notify(e);
  246. }
  247. var evt = (e ? e : window.event);
  248. var pos = Util.getEventPosition(e, this._target, this._scale);
  249. if (this._onMouseMove) {
  250. this._onMouseMove(pos.x, pos.y);
  251. }
  252. Util.stopEvent(e);
  253. return false;
  254. },
  255. _handleMouseDisable: function (e) {
  256. if (!this._focused) { return true; }
  257. var evt = (e ? e : window.event);
  258. var pos = Util.getEventPosition(e, this._target, this._scale);
  259. /* Stop propagation if inside canvas area */
  260. if ((pos.realx >= 0) && (pos.realy >= 0) &&
  261. (pos.realx < this._target.offsetWidth) &&
  262. (pos.realy < this._target.offsetHeight)) {
  263. //Util.Debug("mouse event disabled");
  264. Util.stopEvent(e);
  265. return false;
  266. }
  267. return true;
  268. },
  269. // Public methods
  270. grab: function () {
  271. var c = this._target;
  272. if ('ontouchstart' in document.documentElement) {
  273. Util.addEvent(c, 'touchstart', this._eventHandlers.mousedown);
  274. Util.addEvent(window, 'touchend', this._eventHandlers.mouseup);
  275. Util.addEvent(c, 'touchend', this._eventHandlers.mouseup);
  276. Util.addEvent(c, 'touchmove', this._eventHandlers.mousemove);
  277. }
  278. Util.addEvent(c, 'mousedown', this._eventHandlers.mousedown);
  279. Util.addEvent(window, 'mouseup', this._eventHandlers.mouseup);
  280. Util.addEvent(c, 'mouseup', this._eventHandlers.mouseup);
  281. Util.addEvent(c, 'mousemove', this._eventHandlers.mousemove);
  282. Util.addEvent(c, (Util.Engine.gecko) ? 'DOMMouseScroll' : 'mousewheel',
  283. this._eventHandlers.mousewheel);
  284. /* Work around right and middle click browser behaviors */
  285. Util.addEvent(document, 'click', this._eventHandlers.mousedisable);
  286. Util.addEvent(document.body, 'contextmenu', this._eventHandlers.mousedisable);
  287. },
  288. ungrab: function () {
  289. var c = this._target;
  290. if ('ontouchstart' in document.documentElement) {
  291. Util.removeEvent(c, 'touchstart', this._eventHandlers.mousedown);
  292. Util.removeEvent(window, 'touchend', this._eventHandlers.mouseup);
  293. Util.removeEvent(c, 'touchend', this._eventHandlers.mouseup);
  294. Util.removeEvent(c, 'touchmove', this._eventHandlers.mousemove);
  295. }
  296. Util.removeEvent(c, 'mousedown', this._eventHandlers.mousedown);
  297. Util.removeEvent(window, 'mouseup', this._eventHandlers.mouseup);
  298. Util.removeEvent(c, 'mouseup', this._eventHandlers.mouseup);
  299. Util.removeEvent(c, 'mousemove', this._eventHandlers.mousemove);
  300. Util.removeEvent(c, (Util.Engine.gecko) ? 'DOMMouseScroll' : 'mousewheel',
  301. this._eventHandlers.mousewheel);
  302. /* Work around right and middle click browser behaviors */
  303. Util.removeEvent(document, 'click', this._eventHandlers.mousedisable);
  304. Util.removeEvent(document.body, 'contextmenu', this._eventHandlers.mousedisable);
  305. }
  306. };
  307. Util.make_properties(Mouse, [
  308. ['target', 'ro', 'dom'], // DOM element that captures mouse input
  309. ['notify', 'ro', 'func'], // Function to call to notify whenever a mouse event is received
  310. ['focused', 'rw', 'bool'], // Capture and send mouse clicks/movement
  311. ['scale', 'rw', 'float'], // Viewport scale factor 0.0 - 1.0
  312. ['onMouseButton', 'rw', 'func'], // Handler for mouse button click/release
  313. ['onMouseMove', 'rw', 'func'], // Handler for mouse movement
  314. ['touchButton', 'rw', 'int'] // Button mask (1, 2, 4) for touch devices (0 means ignore clicks)
  315. ]);
  316. })();