source: Dev/branches/rest-dojo-ui/client/dojox/drawing/manager/keys.js @ 256

Last change on this file since 256 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

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