1 /* ***** BEGIN LICENSE BLOCK *****
  2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3  *
  4  * The contents of this file are subject to the Mozilla Public License Version
  5  * 1.1 (the "License"); you may not use this file except in compliance with
  6  * the License. You may obtain a copy of the License at
  7  * http://www.mozilla.org/MPL/
  8  *
  9  * Software distributed under the License is distributed on an "AS IS" basis,
 10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 11  * for the specific language governing rights and limitations under the
 12  * License.
 13  *
 14  * The Initial Developer of the Original Code is Fireinput Inc.
 15  *
 16  * Portions created by the Initial Developer are Copyright (C) 2007
 17  * the Initial Developer. All Rights Reserved.
 18  *
 19  * Contributor(s):
 20  *     Olly Ja <ollyja@gmail.com>
 21  *
 22  * Alternatively, the contents of this file may be used under the terms of
 23  * either the GNU General Public License Version 2 or later (the "GPL"), or
 24  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 25  * in which case the provisions of the GPL or the LGPL are applicable instead
 26  * of those above. If you wish to allow use of your version of this file only
 27  * under the terms of either the GPL or the LGPL, and not to allow others to
 28  * use your version of this file under the terms of the MPL, indicate your
 29  * decision by deleting the provisions above and replace them with the notice
 30  * and other provisions required by the GPL or the LGPL. If you do not delete
 31  * the provisions above, a recipient may use your version of this file under
 32  * the terms of any one of the MPL, the GPL or the LGPL.
 33  *
 34  * ***** END LICENSE BLOCK ***** 
 35  */
 36 
 37 /* the mapping between DOM_VK_* and its value */
 38 var keyCodeMapper=new Object();
 39 keyCodeMapper['3']='CANCEL'
 40 keyCodeMapper['6']='HELP'
 41 keyCodeMapper['8']='BACK_SPACE'
 42 keyCodeMapper['9']='TAB'
 43 keyCodeMapper['12']='CLEAR'
 44 keyCodeMapper['13']='RETURN'
 45 keyCodeMapper['14']='ENTER'
 46 keyCodeMapper['16']='SHIFT'
 47 keyCodeMapper['17']='CONTROL'
 48 keyCodeMapper['18']='ALT'
 49 keyCodeMapper['19']='PAUSE'
 50 keyCodeMapper['20']='CAPS_LOCK'
 51 keyCodeMapper['27']='ESCAPE'
 52 keyCodeMapper['32']='SPACE'
 53 keyCodeMapper['33']='PAGE_UP'
 54 keyCodeMapper['34']='PAGE_DOWN'
 55 keyCodeMapper['35']='END'
 56 keyCodeMapper['36']='HOME'
 57 keyCodeMapper['37']='LEFT'
 58 keyCodeMapper['38']='UP'
 59 keyCodeMapper['39']='RIGHT'
 60 keyCodeMapper['40']='DOWN'
 61 keyCodeMapper['44']='PRINTSCREEN'
 62 keyCodeMapper['45']='INSERT'
 63 keyCodeMapper['46']='DELETE'
 64 keyCodeMapper['48']='0'
 65 keyCodeMapper['49']='1'
 66 keyCodeMapper['50']='2'
 67 keyCodeMapper['51']='3'
 68 keyCodeMapper['52']='4'
 69 keyCodeMapper['53']='5'
 70 keyCodeMapper['54']='6'
 71 keyCodeMapper['55']='7'
 72 keyCodeMapper['56']='8'
 73 keyCodeMapper['57']='9'
 74 keyCodeMapper['59']=';'
 75 keyCodeMapper['61']='='
 76 keyCodeMapper['65']='A'
 77 keyCodeMapper['66']='B'
 78 keyCodeMapper['67']='C'
 79 keyCodeMapper['68']='D'
 80 keyCodeMapper['69']='E'
 81 keyCodeMapper['70']='F'
 82 keyCodeMapper['71']='G'
 83 keyCodeMapper['72']='H'
 84 keyCodeMapper['73']='I'
 85 keyCodeMapper['74']='J'
 86 keyCodeMapper['75']='K'
 87 keyCodeMapper['76']='L'
 88 keyCodeMapper['77']='M'
 89 keyCodeMapper['78']='N'
 90 keyCodeMapper['79']='O'
 91 keyCodeMapper['80']='P'
 92 keyCodeMapper['81']='Q'
 93 keyCodeMapper['82']='R'
 94 keyCodeMapper['83']='S'
 95 keyCodeMapper['84']='T'
 96 keyCodeMapper['85']='U'
 97 keyCodeMapper['86']='V'
 98 keyCodeMapper['87']='W'
 99 keyCodeMapper['88']='X'
100 keyCodeMapper['89']='Y'
101 keyCodeMapper['90']='Z'
102 keyCodeMapper['93']='CONTEXT_MENU'
103 keyCodeMapper['96']='NUMPAD0'
104 keyCodeMapper['97']='NUMPAD1'
105 keyCodeMapper['98']='NUMPAD2'
106 keyCodeMapper['99']='NUMPAD3'
107 keyCodeMapper['100']='NUMPAD4'
108 keyCodeMapper['101']='NUMPAD5'
109 keyCodeMapper['102']='NUMPAD6'
110 keyCodeMapper['103']='NUMPAD7'
111 keyCodeMapper['104']='NUMPAD8'
112 keyCodeMapper['105']='NUMPAD9'
113 keyCodeMapper['106']='MULTIPLY'
114 keyCodeMapper['107']='ADD'
115 keyCodeMapper['108']='SEPARATOR'
116 keyCodeMapper['109']='SUBTRACT'
117 keyCodeMapper['110']='DECIMAL'
118 keyCodeMapper['111']='DIVIDE'
119 keyCodeMapper['112']='F1'
120 keyCodeMapper['113']='F2'
121 keyCodeMapper['114']='F3'
122 keyCodeMapper['115']='F4'
123 keyCodeMapper['116']='F5'
124 keyCodeMapper['117']='F6'
125 keyCodeMapper['118']='F7'
126 keyCodeMapper['119']='F8'
127 keyCodeMapper['120']='F9'
128 keyCodeMapper['121']='F10'
129 keyCodeMapper['122']='F11'
130 keyCodeMapper['123']='F12'
131 keyCodeMapper['124']='F13'
132 keyCodeMapper['125']='F14'
133 keyCodeMapper['126']='F15'
134 keyCodeMapper['127']='F16'
135 keyCodeMapper['128']='F17'
136 keyCodeMapper['129']='F18'
137 keyCodeMapper['130']='F19'
138 keyCodeMapper['131']='F20'
139 keyCodeMapper['132']='F21'
140 keyCodeMapper['133']='F22'
141 keyCodeMapper['134']='F23'
142 keyCodeMapper['135']='F24'
143 keyCodeMapper['144']='NUM_LOCK'
144 keyCodeMapper['145']='SCROLL_LOCK'
145 keyCodeMapper['188']=','
146 keyCodeMapper['190']='.'
147 keyCodeMapper['191']='/'
148 keyCodeMapper['192']='`'
149 keyCodeMapper['219']='{'
150 keyCodeMapper['220']='\\'
151 keyCodeMapper['221']='}'
152 keyCodeMapper['222']='\''
153 keyCodeMapper['224']='META'
154 
155 /* a list of key and its command mapping */
156 var keyActions = [
157     {name: "openKey", value: "",  command: "Fireinput.toggleFireinput()"},
158     {name: "openEditorKey", value: "", command: "FireinputHelp.openEditor()"},
159     {name: "toggleIMEKey", value: "", command: "Fireinput.toggleIMEMode()"},
160     {name: "quickToggleIMEKey", value: "", command: ""},
161     {name: "switchInputMethodKey", value: "", command: "Fireinput.switchInputMethod()"},
162     {name: "toggleHalfKey", value: "", command: "Fireinput.toggleHalfMode()"},
163     {name: "togglePuncKey", value: "", command: "Fireinput.togglePunctMode()"},
164     {name: "toggleEncodingKey", value: "", command: "Fireinput.toggleEncodingMode()"},
165     {name: "pageUpKey", value: "", command: null},
166     {name: "pageDownKey", value: "", command: null},
167     {name: "selectFirstKey", value: "", command: null},
168     {name: "selectSecondKey", value: "", command: null},
169     {name: "selectThirdKey", value: "", command: null}
170 ]; 
171 
172 var FireinputKeyBinding = 
173 {
174     keyValueHash: null, 
175     keyCodeHash: null, 
176 
177     // initialize key value 
178     init: function()
179     {
180        this.keyValueHash = new FireinputHash(); 
181        this.keyCodeHash = new FireinputHash(); 
182  
183        for (var i=0; i<keyActions.length; i++)
184        {
185           keyActions[i].value = fireinputPrefGetDefault(keyActions[i].name); 
186           this.keyValueHash.setItem(keyActions[i].value, keyActions[i]); 
187           this.keyValueHash.setItem(keyActions[i].name, keyActions[i]); 
188        }
189 
190        // create string to code value 
191        for(var k in keyCodeMapper)
192        {
193           this.keyCodeHash.setItem(keyCodeMapper[k], k); 
194        }
195 
196     }, 
197 
198     // re-read from pref 
199     refreshKeySetting: function()
200     {
201        this.init(); 
202     }, 
203     
204     getKeyCodeByString: function(keyString)
205     {
206        return this.keyCodeHash.getItem(keyString.toUpperCase());
207     }, 
208 
209     disableConflictKey: function(allowInputKeys)
210     {
211        // reset first 
212        for (var i=0; i<keyActions.length; i++)
213        {
214           keyActions[i].disabled = false; 
215        }
216        for(var i=0; i<allowInputKeys.length; i++)
217        {
218           var keyCode = this.getKeyCodeByString(allowInputKeys.charAt(i)); 
219 
220           // the stored value is at upper position 
221           keyCode = keyCode << 4; 
222           if(this.keyValueHash.hasItem(keyCode))
223           {
224              var keyAction = this.keyValueHash.getItem(keyCode); 
225              keyAction.disabled = true; 
226           }
227        }
228 
229     },
230 
231     getKeyString: function(option)
232     {
233        var keyAction = this.keyValueHash.getItem(option); 
234        if(keyAction)
235        {
236            var key = keyCodeMapper[this.getDecodedKey(keyAction.value)]; 
237            var modifiers = this.getDecodedModifier(keyAction.value); 
238            var str = ''; 
239            str += (modifiers & Event.CONTROL_MASK)?'Ctrl+':'';
240            str += (modifiers & Event.ALT_MASK)?'Alt+':'';
241            str += (modifiers & Event.SHIFT_MASK)?'Shift+':'';
242            str += (modifiers & Event.META_MASK)?'Meta+':''; 
243              
244            return key ? (str + key) : str; 
245        }
246     },
247 
248     getKeyActionCode: function(option)
249     {
250        var keyAction = this.keyValueHash.getItem(option); 
251        return this.getDecodedKey(keyAction.value); 
252     }, 
253    
254     getKeyActionModifiers: function(option)
255     {
256        var keyAction = this.keyValueHash.getItem(option);
257        return this.getDecodedModifier(keyAction.value);
258     },
259  
260     checkKeyEvent: function(event)
261     {
262        var keyvalue = this.getEncodedKeyEvent(event); 
263        var keyAction = this.keyValueHash.getItem(keyvalue); 
264        if(keyAction && keyAction.command)
265        {
266           event.preventDefault();
267           event.stopPropagation();
268           window.eval(keyAction.command); 
269        }
270  
271     }, 
272 
273     isTrue: function(option, event)
274     {
275        var keyAction = this.keyValueHash.getItem(option); 
276        if(keyAction.disabled)
277           return false; 
278 
279        var keyvalue = this.getEncodedKeyEvent(event); 
280        return keyAction.value == keyvalue; 
281     }, 
282 
283     getEncodedKeyEvent: function (event)
284     {
285        return (event.keyCode ? event.keyCode : event.charCode) << 4 | this.getEncodedModifierEvent(event); 
286     }, 
287 
288     getEncodedModifierEvent: function(event) 
289     {
290        return event.altKey   * Event.ALT_MASK     |
291               event.ctrlKey  * Event.CONTROL_MASK |
292               event.shiftKey * Event.SHIFT_MASK   |
293               event.metaKey  * Event.META_MASK;
294     },
295     
296     getDecodedKey: function(value)
297     {
298        return value >> 4; 
299     },
300 
301     getDecodedModifier: function(value)
302     {
303 
304        return value & 0xF; 
305     }
306   
307 }; 


syntax highlighted by Code2HTML, v. 0.9.1