2
* FCKeditor - The text editor for Internet - http://www.fckeditor.net
3
* Copyright (C) 2003-2010 Frederico Caldeira Knabben
7
* Licensed under the terms of any of the following licenses at your
10
* - GNU General Public License Version 2 or later (the "GPL")
11
* http://www.gnu.org/licenses/gpl.html
13
* - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
14
* http://www.gnu.org/licenses/lgpl.html
16
* - Mozilla Public License Version 1.1 or later (the "MPL")
17
* http://www.mozilla.org/MPL/MPL-1.1.html
21
* Control keyboard keystroke combinations.
24
var FCKKeystrokeHandler = function( cancelCtrlDefaults )
26
this.Keystrokes = new Object() ;
27
this.CancelCtrlDefaults = ( cancelCtrlDefaults !== false ) ;
31
* Listen to keystroke events in an element or DOM document object.
32
* @target: The element or document to listen to keystroke events.
34
FCKKeystrokeHandler.prototype.AttachToElement = function( target )
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 ) ;
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.
50
FCKKeystrokeHandler.prototype.SetKeystrokes = function()
52
// Look through the arguments.
53
for ( var i = 0 ; i < arguments.length ; i++ )
55
var keyDef = arguments[i] ;
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.
62
if ( typeof( keyDef[0] ) == 'object' ) // It is an array with arrays defining the keystrokes.
63
this.SetKeystrokes.apply( this, keyDef ) ;
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 ;
74
function _FCKKeystrokeHandler_OnKeyDown( ev, keystrokeHandler )
77
var keystroke = ev.keyCode || ev.which ;
79
// Combine it with the CTRL, SHIFT and ALT states.
80
var keyModifiers = 0 ;
82
if ( ev.ctrlKey || ev.metaKey )
83
keyModifiers += CTRL ;
86
keyModifiers += SHIFT ;
91
var keyCombination = keystroke + keyModifiers ;
93
var cancelIt = keystrokeHandler._CancelIt = false ;
95
// Look for its definition availability.
96
var keystrokeValue = keystrokeHandler.Keystrokes[ keyCombination ] ;
98
// FCKDebug.Output( 'KeyDown: ' + keyCombination + ' - Value: ' + keystrokeValue ) ;
100
// If the keystroke is defined
101
if ( keystrokeValue )
103
// If the keystroke has been explicitly set to "true" OR calling the
104
// "OnKeystroke" event, it doesn't return "true", the default behavior
105
// must be preserved.
106
if ( keystrokeValue === true || !( keystrokeHandler.OnKeystroke && keystrokeHandler.OnKeystroke.apply( keystrokeHandler, keystrokeValue ) ) )
112
// By default, it will cancel all combinations with the CTRL key only (except positioning keys).
113
if ( cancelIt || ( keystrokeHandler.CancelCtrlDefaults && keyModifiers == CTRL && ( keystroke < 33 || keystroke > 40 ) ) )
115
keystrokeHandler._CancelIt = true ;
117
if ( ev.preventDefault )
118
return ev.preventDefault() ;
120
ev.returnValue = false ;
121
ev.cancelBubble = true ;
128
function _FCKKeystrokeHandler_OnKeyPress( ev, keystrokeHandler )
130
if ( keystrokeHandler._CancelIt )
132
// FCKDebug.Output( 'KeyPress Cancel', 'Red') ;
134
if ( ev.preventDefault )
135
return ev.preventDefault() ;