2 Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.
\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license
\r
6 // Register a plugin named "sample".
\r
7 CKEDITOR.plugins.add( 'keystrokes',
\r
9 beforeInit : function( editor )
\r
12 * Controls keystrokes typing in this editor instance.
\r
13 * @name CKEDITOR.editor.prototype.keystrokeHandler
\r
14 * @type CKEDITOR.keystrokeHandler
\r
17 editor.keystrokeHandler = new CKEDITOR.keystrokeHandler( editor );
\r
19 editor.specialKeys = {};
\r
22 init : function( editor )
\r
24 var keystrokesConfig = editor.config.keystrokes,
\r
25 blockedConfig = editor.config.blockedKeystrokes;
\r
27 var keystrokes = editor.keystrokeHandler.keystrokes,
\r
28 blockedKeystrokes = editor.keystrokeHandler.blockedKeystrokes;
\r
30 for ( var i = 0 ; i < keystrokesConfig.length ; i++ )
\r
32 keystrokes[ keystrokesConfig[i][0] ] = keystrokesConfig[i][1];
\r
35 for ( i = 0 ; i < blockedConfig.length ; i++ )
\r
37 blockedKeystrokes[ blockedConfig[i] ] = 1;
\r
43 * Controls keystrokes typing in an editor instance.
\r
45 * @param {CKEDITOR.editor} editor The editor instance.
\r
48 CKEDITOR.keystrokeHandler = function( editor )
\r
50 if ( editor.keystrokeHandler )
\r
51 return editor.keystrokeHandler;
\r
54 * List of keystrokes associated to commands. Each entry points to the
\r
55 * command to be executed.
\r
59 this.keystrokes = {};
\r
62 * List of keystrokes that should be blocked if not defined at
\r
63 * {@link keystrokes}. In this way it is possible to block the default
\r
64 * browser behavior for those keystrokes.
\r
68 this.blockedKeystrokes = {};
\r
82 var onKeyDown = function( event )
\r
84 // The DOM event object is passed by the "data" property.
\r
87 var keyCombination = event.getKeystroke();
\r
88 var command = this.keystrokes[ keyCombination ];
\r
89 var editor = this._.editor;
\r
91 cancel = ( editor.fire( 'key', { keyCode : keyCombination } ) === true );
\r
97 var data = { from : 'keystrokeHandler' };
\r
98 cancel = ( editor.execCommand( command, data ) !== false );
\r
103 var handler = editor.specialKeys[ keyCombination ];
\r
104 cancel = ( handler && handler( editor ) === true );
\r
107 cancel = !!this.blockedKeystrokes[ keyCombination ];
\r
112 event.preventDefault( true );
\r
117 var onKeyPress = function( event )
\r
122 event.data.preventDefault( true );
\r
126 CKEDITOR.keystrokeHandler.prototype =
\r
129 * Attaches this keystroke handle to a DOM object. Keystrokes typed
\r
130 ** over this object will get handled by this keystrokeHandler.
\r
131 * @param {CKEDITOR.dom.domObject} domObject The DOM object to attach
\r
135 attach : function( domObject )
\r
137 // For most browsers, it is enough to listen to the keydown event
\r
139 domObject.on( 'keydown', onKeyDown, this );
\r
141 // Some browsers instead, don't cancel key events in the keydown, but in the
\r
142 // keypress. So we must do a longer trip in those cases.
\r
143 if ( CKEDITOR.env.opera || ( CKEDITOR.env.gecko && CKEDITOR.env.mac ) )
\r
144 domObject.on( 'keypress', onKeyPress, this );
\r
150 * A list of keystrokes to be blocked if not defined in the {@link CKEDITOR.config.keystrokes}
\r
151 * setting. In this way it is possible to block the default browser behavior
\r
152 * for those keystrokes.
\r
154 * @default (see example)
\r
156 * // This is actually the default value.
\r
157 * config.blockedKeystrokes =
\r
159 * CKEDITOR.CTRL + 66 /*B*/,
\r
160 * CKEDITOR.CTRL + 73 /*I*/,
\r
161 * CKEDITOR.CTRL + 85 /*U*/
\r
164 CKEDITOR.config.blockedKeystrokes =
\r
166 CKEDITOR.CTRL + 66 /*B*/,
\r
167 CKEDITOR.CTRL + 73 /*I*/,
\r
168 CKEDITOR.CTRL + 85 /*U*/
\r
172 * A list associating keystrokes to editor commands. Each element in the list
\r
173 * is an array where the first item is the keystroke, and the second is the
\r
174 * name of the command to be executed.
\r
176 * @default (see example)
\r
178 * // This is actually the default value.
\r
179 * config.keystrokes =
\r
181 * [ CKEDITOR.ALT + 121 /*F10*/, 'toolbarFocus' ],
\r
182 * [ CKEDITOR.ALT + 122 /*F11*/, 'elementsPathFocus' ],
\r
184 * [ CKEDITOR.SHIFT + 121 /*F10*/, 'contextMenu' ],
\r
186 * [ CKEDITOR.CTRL + 90 /*Z*/, 'undo' ],
\r
187 * [ CKEDITOR.CTRL + 89 /*Y*/, 'redo' ],
\r
188 * [ CKEDITOR.CTRL + CKEDITOR.SHIFT + 90 /*Z*/, 'redo' ],
\r
190 * [ CKEDITOR.CTRL + 76 /*L*/, 'link' ],
\r
192 * [ CKEDITOR.CTRL + 66 /*B*/, 'bold' ],
\r
193 * [ CKEDITOR.CTRL + 73 /*I*/, 'italic' ],
\r
194 * [ CKEDITOR.CTRL + 85 /*U*/, 'underline' ],
\r
196 * [ CKEDITOR.ALT + 109 /*-*/, 'toolbarCollapse' ]
\r
199 CKEDITOR.config.keystrokes =
\r
201 [ CKEDITOR.ALT + 121 /*F10*/, 'toolbarFocus' ],
\r
202 [ CKEDITOR.ALT + 122 /*F11*/, 'elementsPathFocus' ],
\r
204 [ CKEDITOR.SHIFT + 121 /*F10*/, 'contextMenu' ],
\r
205 [ CKEDITOR.CTRL + CKEDITOR.SHIFT + 121 /*F10*/, 'contextMenu' ],
\r
207 [ CKEDITOR.CTRL + 90 /*Z*/, 'undo' ],
\r
208 [ CKEDITOR.CTRL + 89 /*Y*/, 'redo' ],
\r
209 [ CKEDITOR.CTRL + CKEDITOR.SHIFT + 90 /*Z*/, 'redo' ],
\r
211 [ CKEDITOR.CTRL + 76 /*L*/, 'link' ],
\r
213 [ CKEDITOR.CTRL + 66 /*B*/, 'bold' ],
\r
214 [ CKEDITOR.CTRL + 73 /*I*/, 'italic' ],
\r
215 [ CKEDITOR.CTRL + 85 /*U*/, 'underline' ],
\r
217 [ CKEDITOR.ALT + 109 /*-*/, 'toolbarCollapse' ],
\r
218 [ CKEDITOR.ALT + 48 /*0*/, 'a11yHelp' ]
\r
222 * Fired when any keyboard key (or combination) is pressed into the editing area.
\r
223 * @name CKEDITOR#key
\r
225 * @param {Number} data.keyCode A number representing the key code (or
\r
226 * combination). It is the sum of the current key code and the
\r
227 * {@link CKEDITOR.CTRL}, {@link CKEDITOR.SHIFT} and {@link CKEDITOR.ALT}
\r
228 * constants, if those are pressed.
\r