UNPKG

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