UNPKG

2.58 kBJavaScriptView Raw
1/**
2 * Copyright (c) 2013-present, Facebook, Inc.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 */
8
9'use strict';
10
11var SyntheticUIEvent = require('./SyntheticUIEvent');
12
13var getEventCharCode = require('./getEventCharCode');
14var getEventKey = require('./getEventKey');
15var getEventModifierState = require('./getEventModifierState');
16
17/**
18 * @interface KeyboardEvent
19 * @see http://www.w3.org/TR/DOM-Level-3-Events/
20 */
21var KeyboardEventInterface = {
22 key: getEventKey,
23 location: null,
24 ctrlKey: null,
25 shiftKey: null,
26 altKey: null,
27 metaKey: null,
28 repeat: null,
29 locale: null,
30 getModifierState: getEventModifierState,
31 // Legacy Interface
32 charCode: function (event) {
33 // `charCode` is the result of a KeyPress event and represents the value of
34 // the actual printable character.
35
36 // KeyPress is deprecated, but its replacement is not yet final and not
37 // implemented in any major browser. Only KeyPress has charCode.
38 if (event.type === 'keypress') {
39 return getEventCharCode(event);
40 }
41 return 0;
42 },
43 keyCode: function (event) {
44 // `keyCode` is the result of a KeyDown/Up event and represents the value of
45 // physical keyboard key.
46
47 // The actual meaning of the value depends on the users' keyboard layout
48 // which cannot be detected. Assuming that it is a US keyboard layout
49 // provides a surprisingly accurate mapping for US and European users.
50 // Due to this, it is left to the user to implement at this time.
51 if (event.type === 'keydown' || event.type === 'keyup') {
52 return event.keyCode;
53 }
54 return 0;
55 },
56 which: function (event) {
57 // `which` is an alias for either `keyCode` or `charCode` depending on the
58 // type of the event.
59 if (event.type === 'keypress') {
60 return getEventCharCode(event);
61 }
62 if (event.type === 'keydown' || event.type === 'keyup') {
63 return event.keyCode;
64 }
65 return 0;
66 }
67};
68
69/**
70 * @param {object} dispatchConfig Configuration used to dispatch this event.
71 * @param {string} dispatchMarker Marker identifying the event target.
72 * @param {object} nativeEvent Native browser event.
73 * @extends {SyntheticUIEvent}
74 */
75function SyntheticKeyboardEvent(dispatchConfig, dispatchMarker, nativeEvent, nativeEventTarget) {
76 return SyntheticUIEvent.call(this, dispatchConfig, dispatchMarker, nativeEvent, nativeEventTarget);
77}
78
79SyntheticUIEvent.augmentClass(SyntheticKeyboardEvent, KeyboardEventInterface);
80
81module.exports = SyntheticKeyboardEvent;
\No newline at end of file