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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
|
/*
Copyright (c) 2004-2011, The Dojo Foundation All Rights Reserved.
Available via Academic Free License >= 2.1 OR the modified BSD license.
see: http://dojotoolkit.org/license for details
*/
if(!dojo._hasResource["dojox.drawing.manager.keys"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
dojo._hasResource["dojox.drawing.manager.keys"] = true;
dojo.provide("dojox.drawing.manager.keys");
(function(){
// Ref: isEdit allows events to happen in Drawing, like TextBlocks
var isEdit = false;
// Ref: enabled = false allows inputs outside of drawing to function
var enabled = true;
var alphabet = "abcdefghijklmnopqrstuvwxyz";
dojox.drawing.manager.keys = {
// summary:
// A singleton, master object that detects
// keyboard keys and events
// Connect to it like:
// dojo.connect(this.keys, "onEnter", ....);
//
// arrowIncrement:Number
// The amount, in pixels, a selected Stencil will
// move on an arrow key event
arrowIncrement:1,
//
// arrowShiftIncrement: Number
// The amount, in pixels, a selected Stencil will
// move on an arrow key + SHIFT event
arrowShiftIncrement:10,
//
// shift: [readonly] Boolean
// Indicates whether the Shift key is currently pressed
shift:false,
//
// ctrl: [readonly] Boolean
// Indicates whether the Control key is currently pressed
ctrl:false,
//
// alt: [readonly] Boolean
// Indicates whether the Alt or Option key is currently pressed
alt:false,
//
// cmmd: [readonly] Boolean
// Indicates whether the Apple Command key is currently pressed
cmmd:false, // apple key
//
// meta: [readonly] Boolean
// Indicates whether any 'meta' key is currently pressed:
// shift || ctrl || cmmd || alt
meta:false, // any meta key
onDelete: function(/* Event */evt){
// summary:
// Event fires when Delete key is released
},
onEsc: function(/* Event */evt){
// summary:
// Event fires when ESC key is released
},
onEnter: function(/* Event */evt){
// summary:
// Event fires when Enter key is released
},
onArrow: function(/* Event */evt){
// summary:
// Event fires when an Arrow key is released
// You will have to further check if evt.keyCode
// is 37,38,39, or 40
},
onKeyDown: function(/* Event */evt){
// summary:
// Event fires when any key is pressed
},
onKeyUp: function(/* Event */evt){
// summary:
// Event fires when any key is released
},
listeners:[],
register: function(options){
// summary:
// Register an object and callback to be notified
// of events.
// NOTE: Not really used in code, but should work.
// See manager.mouse for similar usage
//
var _handle = dojox.drawing.util.uid("listener");
this.listeners.push({
handle:_handle,
scope: options.scope || window,
callback:options.callback,
keyCode:options.keyCode
});
},
_getLetter: function(evt){
if(!evt.meta && evt.keyCode>=65 && evt.keyCode<=90){
return alphabet.charAt(evt.keyCode-65);
}
return null;
},
_mixin: function(evt){
// summary:
// Internal. Mixes in key events.
evt.meta = this.meta;
evt.shift = this.shift;
evt.alt = this.alt;
evt.cmmd = this.cmmd;
evt.letter = this._getLetter(evt);
return evt;
},
editMode: function(_isedit){
// summary:
// Relinquishes control of events to another portion
// of Drawing; namely the TextBlock.
isEdit = _isedit;
},
enable: function(_enabled){
// summary:
// Enables or disables key events, to relinquish
// control to something outside of Drawing; input
// fields for example.
// You may need to call this directly if you are
// using textareas or contenteditables.
// NOTE: See scanForFields
enabled = _enabled;
},
scanForFields: function(){
// summary:
// Scans the document for inputs
// and calls this automatically. However you may need
// to call this if you create inputs after the fact.
//
if(this._fieldCons){
dojo.forEach(this._fieldCons, dojo.disconnect, dojo);
}
this._fieldCons = [];
dojo.query("input").forEach(function(n){
var a = dojo.connect(n, "focus", this, function(evt){
this.enable(false);
});
var b = dojo.connect(n, "blur", this, function(evt){
this.enable(true);
});
this._fieldCons.push(a);
this._fieldCons.push(b);
}, this);
},
init: function(){
// summary:
// Initialize the keys object
//
// a little extra time is needed in some browsers
setTimeout(dojo.hitch(this, "scanForFields"), 500);
dojo.connect(document, "blur", this, function(evt){
// when command tabbing to another application, the key "sticks"
// this clears any key used for such activity
this.meta = this.shift = this.ctrl = this.cmmd = this.alt = false;
});
dojo.connect(document, "keydown", this, function(evt){
if(!enabled){ return; }
if(evt.keyCode==16){
this.shift = true;
}
if(evt.keyCode==17){
this.ctrl = true;
}
if(evt.keyCode==18){
this.alt = true;
}
if(evt.keyCode==224){
this.cmmd = true;
}
this.meta = this.shift || this.ctrl || this.cmmd || this.alt;
if(!isEdit){
this.onKeyDown(this._mixin(evt));
if(evt.keyCode==8 || evt.keyCode==46){
dojo.stopEvent(evt);
}
}
});
dojo.connect(document, "keyup", this, function(evt){
if(!enabled){ return; }
//console.log("KEY UP:", evt.keyCode);
var _stop = false;
if(evt.keyCode==16){
this.shift = false;
}
if(evt.keyCode==17){
this.ctrl = false;
}
if(evt.keyCode==18){
this.alt = false;
}
if(evt.keyCode==224){
this.cmmd = false;
}
this.meta = this.shift || this.ctrl || this.cmmd || this.alt;
!isEdit && this.onKeyUp(this._mixin(evt));
if(evt.keyCode==13){
console.warn("KEY ENTER");
this.onEnter(evt);
_stop = true;
}
if(evt.keyCode==27){
this.onEsc(evt);
_stop = true;
}
if(evt.keyCode==8 || evt.keyCode==46){
this.onDelete(evt);
_stop = true;
}
if(_stop && !isEdit){
dojo.stopEvent(evt);
}
});
dojo.connect(document, "keypress", this, function(evt){
if(!enabled){ return; }
var inc = this.shift ? this.arrowIncrement*this.arrowShiftIncrement : this.arrowIncrement;
var x =0, y =0;
if(evt.keyCode==32 && !isEdit){ //space
dojo.stopEvent(evt);
}
if(evt.keyCode==37){ //left
x = -inc;
}
if(evt.keyCode==38){ //up
y = -inc;
}
if(evt.keyCode==39){ //right
x = inc;
}
if(evt.keyCode==40){ //down
y = inc;
}
if(x || y){
evt.x = x;
evt.y = y;
evt.shift = this.shift;
if(!isEdit){
this.onArrow(evt);
dojo.stopEvent(evt);
}
}
});
}
};
dojo.addOnLoad(dojox.drawing.manager.keys, "init");
})();
}
|