Ticket #2140: fckkeystrokehandler.js

File fckkeystrokehandler.js, 5.1 KB (added by Hidekazu Kubota, 16 years ago)
Line 
1/*
2 * FCKeditor - The text editor for Internet - http://www.fckeditor.net
3 * Copyright (C) 2003-2008 Frederico Caldeira Knabben
4 *
5 * == BEGIN LICENSE ==
6 *
7 * Licensed under the terms of any of the following licenses at your
8 * choice:
9 *
10 *  - GNU General Public License Version 2 or later (the "GPL")
11 *    http://www.gnu.org/licenses/gpl.html
12 *
13 *  - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
14 *    http://www.gnu.org/licenses/lgpl.html
15 *
16 *  - Mozilla Public License Version 1.1 or later (the "MPL")
17 *    http://www.mozilla.org/MPL/MPL-1.1.html
18 *
19 * == END LICENSE ==
20 *
21 * Control keyboard keystroke combinations.
22 */
23
24var FCKKeystrokeHandler = function( cancelCtrlDefaults )
25{
26        this.Keystrokes = new Object() ;
27        this.CancelCtrlDefaults = ( cancelCtrlDefaults !== false ) ;
28}
29
30/*
31 * Listen to keystroke events in an element or DOM document object.
32 *              @target: The element or document to listen to keystroke events.
33 */
34FCKKeystrokeHandler.prototype.AttachToElement = function( target )
35{
36        // For newer browsers, it is enough to listen to the keydown event only.
37        // Some browsers instead, don't cancel key events in the keydown, but in the
38        // keypress. So we must do a longer trip in those cases.
39        FCKTools.AddEventListenerEx( target, 'keydown', _FCKKeystrokeHandler_OnKeyDown, this ) ;
40        if ( FCKBrowserInfo.IsGecko10 || FCKBrowserInfo.IsOpera || ( FCKBrowserInfo.IsGecko && FCKBrowserInfo.IsMac ) )
41                FCKTools.AddEventListenerEx( target, 'keypress', _FCKKeystrokeHandler_OnKeyPress, this ) ;
42}
43
44/*
45 * Sets a list of keystrokes. It can receive either a single array or "n"
46 * arguments, each one being an array of 1 or 2 elemenst. The first element
47 * is the keystroke combination, and the second is the value to assign to it.
48 * If the second element is missing, the keystroke definition is removed.
49 */
50FCKKeystrokeHandler.prototype.SetKeystrokes = function()
51{
52        // Look through the arguments.
53        for ( var i = 0 ; i < arguments.length ; i++ )
54        {
55                var keyDef = arguments[i] ;
56
57                // If the configuration for the keystrokes is missing some element or has any extra comma
58                // this item won't be valid, so skip it and keep on processing.
59                if ( !keyDef )
60                        continue ;
61
62                if ( typeof( keyDef[0] ) == 'object' )          // It is an array with arrays defining the keystrokes.
63                        this.SetKeystrokes.apply( this, keyDef ) ;
64                else
65                {
66                        if ( keyDef.length == 1 )               // If it has only one element, remove the keystroke.
67                                delete this.Keystrokes[ keyDef[0] ] ;
68                        else                                                    // Otherwise add it.
69                                this.Keystrokes[ keyDef[0] ] = keyDef[1] === true ? true : keyDef ;
70                }
71        }
72}
73
74function _FCKKeystrokeHandler_OnKeyDown( ev, keystrokeHandler )
75{
76        // Get the key code.
77        var keystroke = ev.keyCode || ev.which ;
78
79        // Added by hidekaz 2007/12/12
80        // Ignore key down of enter-key for IME(Input Method Editor) users on Gecko (MacOSX).
81        // Use key press event instead of key down event.
82        if( keystroke == 13 && ( FCKBrowserInfo.IsGecko && FCKBrowserInfo.IsMac )){
83                keystrokeHandler._CancelIt = true ;
84                ev.preventDefault();
85                return false ;
86        }
87        // end hidekaz
88
89        // Combine it with the CTRL, SHIFT and ALT states.
90        var keyModifiers = 0 ;
91
92        if ( ev.ctrlKey || ev.metaKey )
93                keyModifiers += CTRL ;
94
95        if ( ev.shiftKey )
96                keyModifiers += SHIFT ;
97
98        if ( ev.altKey )
99                keyModifiers += ALT ;
100
101        var keyCombination = keystroke + keyModifiers ;
102
103        var cancelIt = keystrokeHandler._CancelIt = false ;
104
105        // Look for its definition availability.
106        var keystrokeValue = keystrokeHandler.Keystrokes[ keyCombination ] ;
107
108//      FCKDebug.Output( 'KeyDown: ' + keyCombination + ' - Value: ' + keystrokeValue ) ;
109
110        // If the keystroke is defined
111        if ( keystrokeValue )
112        {
113                // If the keystroke has been explicitly set to "true" OR calling the
114                // "OnKeystroke" event, it doesn't return "true", the default behavior
115                // must be preserved.
116                if ( keystrokeValue === true || !( keystrokeHandler.OnKeystroke && keystrokeHandler.OnKeystroke.apply( keystrokeHandler, keystrokeValue ) ) )
117                        return true ;
118
119                cancelIt = true ;
120        }
121
122        // By default, it will cancel all combinations with the CTRL key only (except positioning keys).
123        if ( cancelIt || ( keystrokeHandler.CancelCtrlDefaults && keyModifiers == CTRL && ( keystroke < 33 || keystroke > 40 ) ) )
124        {
125                keystrokeHandler._CancelIt = true ;
126
127                if ( ev.preventDefault )
128                        return ev.preventDefault() ;
129
130                ev.returnValue = false ;
131                ev.cancelBubble = true ;
132                return false ;
133        }
134
135        return true ;
136}
137
138function _FCKKeystrokeHandler_OnKeyPress( ev, keystrokeHandler )
139{
140        // Added by hidekaz 2007/12/12
141        // Ignore key down of enter-key for IME (Input Method Editor) users on Gecko (MacOSX).
142        // Use key press event instead of key down event.
143        var keystroke = ev.keyCode || ev.which ;
144        if( keystroke == 13 && ( FCKBrowserInfo.IsGecko && FCKBrowserInfo.IsMac )){
145                return true ;
146        }
147        // end hidekaz
148
149        if ( keystrokeHandler._CancelIt )
150        {
151//              FCKDebug.Output( 'KeyPress Cancel', 'Red') ;
152
153                if ( ev.preventDefault )
154                        return ev.preventDefault() ;
155
156                return false ;
157        }
158
159        return true ;
160}
© 2003 – 2022, CKSource sp. z o.o. sp.k. All rights reserved. | Terms of use | Privacy policy