keys.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. // wrapped by build app
  2. define("dojox/drawing/manager/keys", ["dijit","dojo","dojox"], function(dijit,dojo,dojox){
  3. dojo.provide("dojox.drawing.manager.keys");
  4. (function(){
  5. // Ref: isEdit allows events to happen in Drawing, like TextBlocks
  6. var isEdit = false;
  7. // Ref: enabled = false allows inputs outside of drawing to function
  8. var enabled = true;
  9. var alphabet = "abcdefghijklmnopqrstuvwxyz";
  10. dojox.drawing.manager.keys = {
  11. // summary:
  12. // A singleton, master object that detects
  13. // keyboard keys and events
  14. // Connect to it like:
  15. // dojo.connect(this.keys, "onEnter", ....);
  16. //
  17. // arrowIncrement:Number
  18. // The amount, in pixels, a selected Stencil will
  19. // move on an arrow key event
  20. arrowIncrement:1,
  21. //
  22. // arrowShiftIncrement: Number
  23. // The amount, in pixels, a selected Stencil will
  24. // move on an arrow key + SHIFT event
  25. arrowShiftIncrement:10,
  26. //
  27. // shift: [readonly] Boolean
  28. // Indicates whether the Shift key is currently pressed
  29. shift:false,
  30. //
  31. // ctrl: [readonly] Boolean
  32. // Indicates whether the Control key is currently pressed
  33. ctrl:false,
  34. //
  35. // alt: [readonly] Boolean
  36. // Indicates whether the Alt or Option key is currently pressed
  37. alt:false,
  38. //
  39. // cmmd: [readonly] Boolean
  40. // Indicates whether the Apple Command key is currently pressed
  41. cmmd:false, // apple key
  42. //
  43. // meta: [readonly] Boolean
  44. // Indicates whether any 'meta' key is currently pressed:
  45. // shift || ctrl || cmmd || alt
  46. meta:false, // any meta key
  47. onDelete: function(/* Event */evt){
  48. // summary:
  49. // Event fires when Delete key is released
  50. },
  51. onEsc: function(/* Event */evt){
  52. // summary:
  53. // Event fires when ESC key is released
  54. },
  55. onEnter: function(/* Event */evt){
  56. // summary:
  57. // Event fires when Enter key is released
  58. },
  59. onArrow: function(/* Event */evt){
  60. // summary:
  61. // Event fires when an Arrow key is released
  62. // You will have to further check if evt.keyCode
  63. // is 37,38,39, or 40
  64. },
  65. onKeyDown: function(/* Event */evt){
  66. // summary:
  67. // Event fires when any key is pressed
  68. },
  69. onKeyUp: function(/* Event */evt){
  70. // summary:
  71. // Event fires when any key is released
  72. },
  73. listeners:[],
  74. register: function(options){
  75. // summary:
  76. // Register an object and callback to be notified
  77. // of events.
  78. // NOTE: Not really used in code, but should work.
  79. // See manager.mouse for similar usage
  80. //
  81. var _handle = dojox.drawing.util.common.uid("listener");
  82. this.listeners.push({
  83. handle:_handle,
  84. scope: options.scope || window,
  85. callback:options.callback,
  86. keyCode:options.keyCode
  87. });
  88. },
  89. _getLetter: function(evt){
  90. if(!evt.meta && evt.keyCode>=65 && evt.keyCode<=90){
  91. return alphabet.charAt(evt.keyCode-65);
  92. }
  93. return null;
  94. },
  95. _mixin: function(evt){
  96. // summary:
  97. // Internal. Mixes in key events.
  98. evt.meta = this.meta;
  99. evt.shift = this.shift;
  100. evt.alt = this.alt;
  101. evt.cmmd = this.cmmd;
  102. evt.letter = this._getLetter(evt);
  103. return evt;
  104. },
  105. editMode: function(_isedit){
  106. // summary:
  107. // Relinquishes control of events to another portion
  108. // of Drawing; namely the TextBlock.
  109. isEdit = _isedit;
  110. },
  111. enable: function(_enabled){
  112. // summary:
  113. // Enables or disables key events, to relinquish
  114. // control to something outside of Drawing; input
  115. // fields for example.
  116. // You may need to call this directly if you are
  117. // using textareas or contenteditables.
  118. // NOTE: See scanForFields
  119. enabled = _enabled;
  120. },
  121. scanForFields: function(){
  122. // summary:
  123. // Scans the document for inputs
  124. // and calls this automatically. However you may need
  125. // to call this if you create inputs after the fact.
  126. //
  127. if(this._fieldCons){
  128. dojo.forEach(this._fieldCons, dojo.disconnect, dojo);
  129. }
  130. this._fieldCons = [];
  131. dojo.query("input").forEach(function(n){
  132. var a = dojo.connect(n, "focus", this, function(evt){
  133. this.enable(false);
  134. });
  135. var b = dojo.connect(n, "blur", this, function(evt){
  136. this.enable(true);
  137. });
  138. this._fieldCons.push(a);
  139. this._fieldCons.push(b);
  140. }, this);
  141. },
  142. init: function(){
  143. // summary:
  144. // Initialize the keys object
  145. //
  146. // a little extra time is needed in some browsers
  147. setTimeout(dojo.hitch(this, "scanForFields"), 500);
  148. dojo.connect(document, "blur", this, function(evt){
  149. // when command tabbing to another application, the key "sticks"
  150. // this clears any key used for such activity
  151. this.meta = this.shift = this.ctrl = this.cmmd = this.alt = false;
  152. });
  153. dojo.connect(document, "keydown", this, function(evt){
  154. if(!enabled){ return; }
  155. if(evt.keyCode==16){
  156. this.shift = true;
  157. }
  158. if(evt.keyCode==17){
  159. this.ctrl = true;
  160. }
  161. if(evt.keyCode==18){
  162. this.alt = true;
  163. }
  164. if(evt.keyCode==224){
  165. this.cmmd = true;
  166. }
  167. this.meta = this.shift || this.ctrl || this.cmmd || this.alt;
  168. if(!isEdit){
  169. this.onKeyDown(this._mixin(evt));
  170. if(evt.keyCode==8 || evt.keyCode==46){
  171. dojo.stopEvent(evt);
  172. }
  173. }
  174. });
  175. dojo.connect(document, "keyup", this, function(evt){
  176. if(!enabled){ return; }
  177. //console.log("KEY UP:", evt.keyCode);
  178. var _stop = false;
  179. if(evt.keyCode==16){
  180. this.shift = false;
  181. }
  182. if(evt.keyCode==17){
  183. this.ctrl = false;
  184. }
  185. if(evt.keyCode==18){
  186. this.alt = false;
  187. }
  188. if(evt.keyCode==224){
  189. this.cmmd = false;
  190. }
  191. this.meta = this.shift || this.ctrl || this.cmmd || this.alt;
  192. !isEdit && this.onKeyUp(this._mixin(evt));
  193. if(evt.keyCode==13){
  194. console.warn("KEY ENTER");
  195. this.onEnter(evt);
  196. _stop = true;
  197. }
  198. if(evt.keyCode==27){
  199. this.onEsc(evt);
  200. _stop = true;
  201. }
  202. if(evt.keyCode==8 || evt.keyCode==46){
  203. this.onDelete(evt);
  204. _stop = true;
  205. }
  206. if(_stop && !isEdit){
  207. dojo.stopEvent(evt);
  208. }
  209. });
  210. dojo.connect(document, "keypress", this, function(evt){
  211. if(!enabled){ return; }
  212. var inc = this.shift ? this.arrowIncrement*this.arrowShiftIncrement : this.arrowIncrement;
  213. var x =0, y =0;
  214. if(evt.keyCode==32 && !isEdit){ //space
  215. dojo.stopEvent(evt);
  216. }
  217. if(evt.keyCode==37){ //left
  218. x = -inc;
  219. }
  220. if(evt.keyCode==38){ //up
  221. y = -inc;
  222. }
  223. if(evt.keyCode==39){ //right
  224. x = inc;
  225. }
  226. if(evt.keyCode==40){ //down
  227. y = inc;
  228. }
  229. if(x || y){
  230. evt.x = x;
  231. evt.y = y;
  232. evt.shift = this.shift;
  233. if(!isEdit){
  234. this.onArrow(evt);
  235. dojo.stopEvent(evt);
  236. }
  237. }
  238. });
  239. }
  240. };
  241. dojo.addOnLoad(dojox.drawing.manager.keys, "init");
  242. })();
  243. });