2 Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.
3 For licensing, see LICENSE.html or http://ckeditor.com/license
6 // Register a plugin named "sample".
7 CKEDITOR.plugins.add( 'keystrokes',
9 beforeInit : function( editor )
12 * Controls keystrokes typing in this editor instance.
13 * @name CKEDITOR.editor.prototype.keystrokeHandler
14 * @type CKEDITOR.keystrokeHandler
17 editor.keystrokeHandler = new CKEDITOR.keystrokeHandler( editor );
19 editor.specialKeys = {};
22 init : function( editor )
24 var keystrokesConfig = editor.config.keystrokes,
25 blockedConfig = editor.config.blockedKeystrokes;
27 var keystrokes = editor.keystrokeHandler.keystrokes,
28 blockedKeystrokes = editor.keystrokeHandler.blockedKeystrokes;
30 for ( var i = 0 ; i < keystrokesConfig.length ; i++ )
31 keystrokes[ keystrokesConfig[i][0] ] = keystrokesConfig[i][1];
33 for ( i = 0 ; i < blockedConfig.length ; i++ )
34 blockedKeystrokes[ blockedConfig[i] ] = 1;
39 * Controls keystrokes typing in an editor instance.
41 * @param {CKEDITOR.editor} editor The editor instance.
44 CKEDITOR.keystrokeHandler = function( editor )
46 if ( editor.keystrokeHandler )
47 return editor.keystrokeHandler;
50 * List of keystrokes associated to commands. Each entry points to the
51 * command to be executed.
58 * List of keystrokes that should be blocked if not defined at
59 * {@link keystrokes}. In this way it is possible to block the default
60 * browser behavior for those keystrokes.
64 this.blockedKeystrokes = {};
78 var onKeyDown = function( event )
80 // The DOM event object is passed by the "data" property.
83 var keyCombination = event.getKeystroke();
84 var command = this.keystrokes[ keyCombination ];
85 var editor = this._.editor;
87 cancel = ( editor.fire( 'key', { keyCode : keyCombination } ) === true );
93 var data = { from : 'keystrokeHandler' };
94 cancel = ( editor.execCommand( command, data ) !== false );
99 var handler = editor.specialKeys[ keyCombination ];
100 cancel = ( handler && handler( editor ) === true );
103 cancel = !!this.blockedKeystrokes[ keyCombination ];
108 event.preventDefault( true );
113 var onKeyPress = function( event )
118 event.data.preventDefault( true );
122 CKEDITOR.keystrokeHandler.prototype =
125 * Attaches this keystroke handle to a DOM object. Keystrokes typed
126 ** over this object will get handled by this keystrokeHandler.
127 * @param {CKEDITOR.dom.domObject} domObject The DOM object to attach
131 attach : function( domObject )
133 // For most browsers, it is enough to listen to the keydown event
135 domObject.on( 'keydown', onKeyDown, this );
137 // Some browsers instead, don't cancel key events in the keydown, but in the
138 // keypress. So we must do a longer trip in those cases.
139 if ( CKEDITOR.env.opera || ( CKEDITOR.env.gecko && CKEDITOR.env.mac ) )
140 domObject.on( 'keypress', onKeyPress, this );
146 * A list of keystrokes to be blocked if not defined in the {@link CKEDITOR.config.keystrokes}
147 * setting. In this way it is possible to block the default browser behavior
148 * for those keystrokes.
150 * @default (see example)
152 * // This is actually the default value.
153 * config.blockedKeystrokes =
155 * CKEDITOR.CTRL + 66 /*B*/,
156 * CKEDITOR.CTRL + 73 /*I*/,
157 * CKEDITOR.CTRL + 85 /*U*/
160 CKEDITOR.config.blockedKeystrokes =
162 CKEDITOR.CTRL + 66 /*B*/,
163 CKEDITOR.CTRL + 73 /*I*/,
164 CKEDITOR.CTRL + 85 /*U*/
168 * A list associating keystrokes to editor commands. Each element in the list
169 * is an array where the first item is the keystroke, and the second is the
170 * name of the command to be executed.
172 * @default (see example)
174 * // This is actually the default value.
175 * config.keystrokes =
177 * [ CKEDITOR.ALT + 121 /*F10*/, 'toolbarFocus' ],
178 * [ CKEDITOR.ALT + 122 /*F11*/, 'elementsPathFocus' ],
180 * [ CKEDITOR.SHIFT + 121 /*F10*/, 'contextMenu' ],
182 * [ CKEDITOR.CTRL + 90 /*Z*/, 'undo' ],
183 * [ CKEDITOR.CTRL + 89 /*Y*/, 'redo' ],
184 * [ CKEDITOR.CTRL + CKEDITOR.SHIFT + 90 /*Z*/, 'redo' ],
186 * [ CKEDITOR.CTRL + 76 /*L*/, 'link' ],
188 * [ CKEDITOR.CTRL + 66 /*B*/, 'bold' ],
189 * [ CKEDITOR.CTRL + 73 /*I*/, 'italic' ],
190 * [ CKEDITOR.CTRL + 85 /*U*/, 'underline' ],
192 * [ CKEDITOR.ALT + 109 /*-*/, 'toolbarCollapse' ]
195 CKEDITOR.config.keystrokes =
197 [ CKEDITOR.ALT + 121 /*F10*/, 'toolbarFocus' ],
198 [ CKEDITOR.ALT + 122 /*F11*/, 'elementsPathFocus' ],
200 [ CKEDITOR.SHIFT + 121 /*F10*/, 'contextMenu' ],
201 [ CKEDITOR.CTRL + CKEDITOR.SHIFT + 121 /*F10*/, 'contextMenu' ],
203 [ CKEDITOR.CTRL + 90 /*Z*/, 'undo' ],
204 [ CKEDITOR.CTRL + 89 /*Y*/, 'redo' ],
205 [ CKEDITOR.CTRL + CKEDITOR.SHIFT + 90 /*Z*/, 'redo' ],
207 [ CKEDITOR.CTRL + 76 /*L*/, 'link' ],
209 [ CKEDITOR.CTRL + 66 /*B*/, 'bold' ],
210 [ CKEDITOR.CTRL + 73 /*I*/, 'italic' ],
211 [ CKEDITOR.CTRL + 85 /*U*/, 'underline' ],
213 [ CKEDITOR.ALT + ( CKEDITOR.env.ie || CKEDITOR.env.webkit ? 189 : 109 ) /*-*/, 'toolbarCollapse' ],
214 [ CKEDITOR.ALT + 48 /*0*/, 'a11yHelp' ]
218 * Fired when any keyboard key (or combination) is pressed into the editing area.
219 * @name CKEDITOR.editor#key
221 * @param {Number} data.keyCode A number representing the key code (or
222 * combination). It is the sum of the current key code and the
223 * {@link CKEDITOR.CTRL}, {@link CKEDITOR.SHIFT} and {@link CKEDITOR.ALT}
224 * constants, if those are pressed.