Skip to content

Commit 696e3dc

Browse files
committed
Prevent non-modified keys only
1 parent d8b0bf7 commit 696e3dc

2 files changed

Lines changed: 10 additions & 3 deletions

File tree

src/boot/Config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ var Config = new Class({
415415
this.inputKeyboardEventTarget = GetValue(config, 'input.keyboard.target', window);
416416

417417
/**
418-
* @const {boolean} Phaser.Boot.Config#inputKeyboardCapture - Should `preventDefault` be called automatically on every key press (true), or let each Key object set it (false)
418+
* @const {boolean} Phaser.Boot.Config#inputKeyboardCapture - Should `preventDefault` be called automatically on every key non-modified press (true), or let each Key object set it (false)
419419
*/
420420
this.inputKeyboardCapture = GetValue(config, 'input.keyboard.capture', true);
421421

src/input/keyboard/KeyboardPlugin.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,15 @@ var KeyboardPlugin = new Class({
164164
this.time = 0;
165165

166166
/**
167-
* A flag that controls if all keys pressed have `preventDefault` called on them or not.
167+
* A flag that controls if all non-modified keys pressed have `preventDefault` called on them or not.
168168
*
169169
* By default this is `true`.
170170
*
171+
* A non-modified key is one that doesn't have a modifier key held down with it. The modifier keys are
172+
* shift, control, alt and the meta key (Command on a Mac, the Windows Key on Windows).
173+
* Therefore, if the user presses shift + r, it won't prevent this combination, because of the modifier.
174+
* However, if the user presses just the r key on its own, it will have its event prevented.
175+
*
171176
* You can set this flag to stop any key from triggering the default browser action, or if you need
172177
* more specific control, you can create Key objects and set the flag on each of those instead.
173178
*
@@ -260,7 +265,9 @@ var KeyboardPlugin = new Class({
260265

261266
var key = _this.keys[event.keyCode];
262267

263-
if (_this.preventDefault || key && key.preventDefault)
268+
var modified = (event.altKey || event.ctrlKey || event.shiftKey || event.metaKey);
269+
270+
if (_this.preventDefault && !modified || key && key.preventDefault)
264271
{
265272
event.preventDefault();
266273
}

0 commit comments

Comments
 (0)