1/* 2 * FCKeditor - The text editor for Internet - http://www.fckeditor.net 3 * Copyright (C) 2003-2007 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 // Combine it with the CTRL, SHIFT and ALT states. 80 var keyModifiers = 0 ; 81 82 if ( ev.ctrlKey || ev.metaKey ) 83 keyModifiers += CTRL ; 84 85 if ( ev.shiftKey ) 86 keyModifiers += SHIFT ; 87 88 if ( ev.altKey ) 89 keyModifiers += ALT ; 90 91 var keyCombination = keystroke + keyModifiers ; 92 93 var cancelIt = keystrokeHandler._CancelIt = false ; 94 95 // Look for its definition availability. 96 var keystrokeValue = keystrokeHandler.Keystrokes[ keyCombination ] ; 97 98// FCKDebug.Output( 'KeyDown: ' + keyCombination + ' - Value: ' + keystrokeValue ) ; 99 100 // If the keystroke is defined 101 if ( keystrokeValue ) 102 { 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 ) ) ) 107 return true ; 108 109 cancelIt = true ; 110 } 111 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 ) ) ) 114 { 115 keystrokeHandler._CancelIt = true ; 116 117 if ( ev.preventDefault ) 118 return ev.preventDefault() ; 119 120 ev.returnValue = false ; 121 ev.cancelBubble = true ; 122 return false ; 123 } 124 125 return true ; 126} 127 128function _FCKKeystrokeHandler_OnKeyPress( ev, keystrokeHandler ) 129{ 130 if ( keystrokeHandler._CancelIt ) 131 { 132// FCKDebug.Output( 'KeyPress Cancel', 'Red') ; 133 134 if ( ev.preventDefault ) 135 return ev.preventDefault() ; 136 137 return false ; 138 } 139 140 return true ; 141} 142