Skip to content

Commit

Permalink
Merge pull request facebook#1956 from syranide/ffenter
Browse files Browse the repository at this point in the history
Improve normalization of charCode, fix FF not reporting Enter
  • Loading branch information
zpao committed Jul 31, 2014
2 parents ab2406f + b830764 commit 5d288de
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 18 deletions.
7 changes: 5 additions & 2 deletions src/browser/eventPlugins/SimpleEventPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ var SyntheticTouchEvent = require('SyntheticTouchEvent');
var SyntheticUIEvent = require('SyntheticUIEvent');
var SyntheticWheelEvent = require('SyntheticWheelEvent');

var getEventCharCode = require('getEventCharCode');

var invariant = require('invariant');
var keyOf = require('keyOf');

Expand Down Expand Up @@ -343,8 +345,9 @@ var SimpleEventPlugin = {
break;
case topLevelTypes.topKeyPress:
// FireFox creates a keypress event for function keys too. This removes
// the unwanted keypress events.
if (nativeEvent.charCode === 0) {
// the unwanted keypress events. Enter is however both printable and
// non-printable. One would expect Tab to be as well (but it isn't).
if (getEventCharCode(nativeEvent) === 0) {
return null;
}
/* falls through */
Expand Down
19 changes: 12 additions & 7 deletions src/browser/syntheticEvents/SyntheticKeyboardEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

var SyntheticUIEvent = require('SyntheticUIEvent');

var getEventCharCode = require('getEventCharCode');
var getEventKey = require('getEventKey');
var getEventModifierState = require('getEventModifierState');

Expand All @@ -43,11 +44,10 @@ var KeyboardEventInterface = {
// `charCode` is the result of a KeyPress event and represents the value of
// the actual printable character.

// KeyPress is deprecated but its replacement is not yet final and not
// implemented in any major browser.
// KeyPress is deprecated, but its replacement is not yet final and not
// implemented in any major browser. Only KeyPress has charCode.
if (event.type === 'keypress') {
// IE8 does not implement "charCode", but "keyCode" has the correct value.
return 'charCode' in event ? event.charCode : event.keyCode;
return getEventCharCode(event);
}
return 0;
},
Expand All @@ -66,9 +66,14 @@ var KeyboardEventInterface = {
},
which: function(event) {
// `which` is an alias for either `keyCode` or `charCode` depending on the
// type of the event. There is no need to determine the type of the event
// as `keyCode` and `charCode` are either aliased or default to zero.
return event.keyCode || event.charCode;
// type of the event.
if (event.type === 'keypress') {
return getEventCharCode(event);
}
if (event.type === 'keydown' || event.type === 'keyup') {
return event.keyCode;
}
return 0;
}
};

Expand Down
57 changes: 57 additions & 0 deletions src/browser/ui/dom/getEventCharCode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* Copyright 2013-2014 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @providesModule getEventCharCode
* @typechecks static-only
*/

"use strict";

/**
* `charCode` represents the actual "character code" and is safe to use with
* `String.fromCharCode`. As such, only keys that correspond to printable
* characters produce a valid `charCode`, the only exception to this is Enter.
* The Tab-key is considered non-printable and does not have a `charCode`,
* presumably because it does not produce a tab-character in browsers.
*
* @param {object} nativeEvent Native browser event.
* @return {string} Normalized `charCode` property.
*/
function getEventCharCode(nativeEvent) {
var charCode;
var keyCode = nativeEvent.keyCode;

if ('charCode' in nativeEvent) {
charCode = nativeEvent.charCode;

// FF does not set `charCode` for the Enter-key, check against `keyCode`.
if (charCode === 0 && keyCode === 13) {
charCode = 13;
}
} else {
// IE8 does not implement `charCode`, but `keyCode` has the correct value.
charCode = keyCode;
}

// Some non-printable keys are reported in `charCode`/`keyCode`, discard them.
// Must not discard the (non-)printable Enter-key.
if (charCode >= 32 || charCode === 13) {
return charCode;
}

return 0;
}

module.exports = getEventCharCode;
13 changes: 4 additions & 9 deletions src/browser/ui/dom/getEventKey.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

"use strict";

var invariant = require('invariant');
var getEventCharCode = require('getEventCharCode');

/**
* Normalization of deprecated HTML5 `key` values
Expand All @@ -41,7 +41,7 @@ var normalizeKey = {
};

/**
* Translation from legacy `which`/`keyCode` to HTML5 `key`
* Translation from legacy `keyCode` to HTML5 `key`
* Only special keys supported, all others depend on keyboard layout or browser
* @see https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent#Key_names
*/
Expand Down Expand Up @@ -93,11 +93,7 @@ function getEventKey(nativeEvent) {

// Browser does not implement `key`, polyfill as much of it as we can.
if (nativeEvent.type === 'keypress') {
// Create the character from the `charCode` ourselves and use as an almost
// perfect replacement.
var charCode = 'charCode' in nativeEvent ?
nativeEvent.charCode :
nativeEvent.keyCode;
var charCode = getEventCharCode(nativeEvent);

// The enter-key is technically both printable and non-printable and can
// thus be captured by `keypress`, no other non-printable key should.
Expand All @@ -108,8 +104,7 @@ function getEventKey(nativeEvent) {
// `keyCode` value, almost all function keys have a universal value.
return translateToKey[nativeEvent.keyCode] || 'Unidentified';
}

invariant(false, "Unexpected keyboard event type: %s", nativeEvent.type);
return '';
}

module.exports = getEventKey;

0 comments on commit 5d288de

Please sign in to comment.