2 * FCKeditor - The text editor for Internet - http://www.fckeditor.net
\r
3 * Copyright (C) 2003-2009 Frederico Caldeira Knabben
\r
5 * == BEGIN LICENSE ==
\r
7 * Licensed under the terms of any of the following licenses at your
\r
10 * - GNU General Public License Version 2 or later (the "GPL")
\r
11 * http://www.gnu.org/licenses/gpl.html
\r
13 * - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
\r
14 * http://www.gnu.org/licenses/lgpl.html
\r
16 * - Mozilla Public License Version 1.1 or later (the "MPL")
\r
17 * http://www.mozilla.org/MPL/MPL-1.1.html
\r
21 * Control keyboard keystroke combinations.
\r
24 var FCKKeystrokeHandler = function( cancelCtrlDefaults )
\r
26 this.Keystrokes = new Object() ;
\r
27 this.CancelCtrlDefaults = ( cancelCtrlDefaults !== false ) ;
\r
31 * Listen to keystroke events in an element or DOM document object.
\r
32 * @target: The element or document to listen to keystroke events.
\r
34 FCKKeystrokeHandler.prototype.AttachToElement = function( target )
\r
36 // For newer browsers, it is enough to listen to the keydown event only.
\r
37 // Some browsers instead, don't cancel key events in the keydown, but in the
\r
38 // keypress. So we must do a longer trip in those cases.
\r
39 FCKTools.AddEventListenerEx( target, 'keydown', _FCKKeystrokeHandler_OnKeyDown, this ) ;
\r
40 if ( FCKBrowserInfo.IsGecko10 || FCKBrowserInfo.IsOpera || ( FCKBrowserInfo.IsGecko && FCKBrowserInfo.IsMac ) )
\r
41 FCKTools.AddEventListenerEx( target, 'keypress', _FCKKeystrokeHandler_OnKeyPress, this ) ;
\r
45 * Sets a list of keystrokes. It can receive either a single array or "n"
\r
46 * arguments, each one being an array of 1 or 2 elemenst. The first element
\r
47 * is the keystroke combination, and the second is the value to assign to it.
\r
48 * If the second element is missing, the keystroke definition is removed.
\r
50 FCKKeystrokeHandler.prototype.SetKeystrokes = function()
\r
52 // Look through the arguments.
\r
53 for ( var i = 0 ; i < arguments.length ; i++ )
\r
55 var keyDef = arguments[i] ;
\r
57 // If the configuration for the keystrokes is missing some element or has any extra comma
\r
58 // this item won't be valid, so skip it and keep on processing.
\r
62 if ( typeof( keyDef[0] ) == 'object' ) // It is an array with arrays defining the keystrokes.
\r
63 this.SetKeystrokes.apply( this, keyDef ) ;
\r
66 if ( keyDef.length == 1 ) // If it has only one element, remove the keystroke.
\r
67 delete this.Keystrokes[ keyDef[0] ] ;
\r
68 else // Otherwise add it.
\r
69 this.Keystrokes[ keyDef[0] ] = keyDef[1] === true ? true : keyDef ;
\r
74 function _FCKKeystrokeHandler_OnKeyDown( ev, keystrokeHandler )
\r
76 // Get the key code.
\r
77 var keystroke = ev.keyCode || ev.which ;
\r
79 // Combine it with the CTRL, SHIFT and ALT states.
\r
80 var keyModifiers = 0 ;
\r
82 if ( ev.ctrlKey || ev.metaKey )
\r
83 keyModifiers += CTRL ;
\r
86 keyModifiers += SHIFT ;
\r
89 keyModifiers += ALT ;
\r
91 var keyCombination = keystroke + keyModifiers ;
\r
93 var cancelIt = keystrokeHandler._CancelIt = false ;
\r
95 // Look for its definition availability.
\r
96 var keystrokeValue = keystrokeHandler.Keystrokes[ keyCombination ] ;
\r
98 // FCKDebug.Output( 'KeyDown: ' + keyCombination + ' - Value: ' + keystrokeValue ) ;
\r
100 // If the keystroke is defined
\r
101 if ( keystrokeValue )
\r
103 // If the keystroke has been explicitly set to "true" OR calling the
\r
104 // "OnKeystroke" event, it doesn't return "true", the default behavior
\r
105 // must be preserved.
\r
106 if ( keystrokeValue === true || !( keystrokeHandler.OnKeystroke && keystrokeHandler.OnKeystroke.apply( keystrokeHandler, keystrokeValue ) ) )
\r
112 // By default, it will cancel all combinations with the CTRL key only (except positioning keys).
\r
113 if ( cancelIt || ( keystrokeHandler.CancelCtrlDefaults && keyModifiers == CTRL && ( keystroke < 33 || keystroke > 40 ) ) )
\r
115 keystrokeHandler._CancelIt = true ;
\r
117 if ( ev.preventDefault )
\r
118 return ev.preventDefault() ;
\r
120 ev.returnValue = false ;
\r
121 ev.cancelBubble = true ;
\r
128 function _FCKKeystrokeHandler_OnKeyPress( ev, keystrokeHandler )
\r
130 if ( keystrokeHandler._CancelIt )
\r
132 // FCKDebug.Output( 'KeyPress Cancel', 'Red') ;
\r
134 if ( ev.preventDefault )
\r
135 return ev.preventDefault() ;
\r