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