Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
81155 views
1
/**
2
* Copyright 2013-2014, 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
* @providesModule SyntheticKeyboardEvent
10
* @typechecks static-only
11
*/
12
13
"use strict";
14
15
var SyntheticUIEvent = require('SyntheticUIEvent');
16
17
var getEventCharCode = require('getEventCharCode');
18
var getEventKey = require('getEventKey');
19
var getEventModifierState = require('getEventModifierState');
20
21
/**
22
* @interface KeyboardEvent
23
* @see http://www.w3.org/TR/DOM-Level-3-Events/
24
*/
25
var KeyboardEventInterface = {
26
key: getEventKey,
27
location: null,
28
ctrlKey: null,
29
shiftKey: null,
30
altKey: null,
31
metaKey: null,
32
repeat: null,
33
locale: null,
34
getModifierState: getEventModifierState,
35
// Legacy Interface
36
charCode: function(event) {
37
// `charCode` is the result of a KeyPress event and represents the value of
38
// the actual printable character.
39
40
// KeyPress is deprecated, but its replacement is not yet final and not
41
// implemented in any major browser. Only KeyPress has charCode.
42
if (event.type === 'keypress') {
43
return getEventCharCode(event);
44
}
45
return 0;
46
},
47
keyCode: function(event) {
48
// `keyCode` is the result of a KeyDown/Up event and represents the value of
49
// physical keyboard key.
50
51
// The actual meaning of the value depends on the users' keyboard layout
52
// which cannot be detected. Assuming that it is a US keyboard layout
53
// provides a surprisingly accurate mapping for US and European users.
54
// Due to this, it is left to the user to implement at this time.
55
if (event.type === 'keydown' || event.type === 'keyup') {
56
return event.keyCode;
57
}
58
return 0;
59
},
60
which: function(event) {
61
// `which` is an alias for either `keyCode` or `charCode` depending on the
62
// type of the event.
63
if (event.type === 'keypress') {
64
return getEventCharCode(event);
65
}
66
if (event.type === 'keydown' || event.type === 'keyup') {
67
return event.keyCode;
68
}
69
return 0;
70
}
71
};
72
73
/**
74
* @param {object} dispatchConfig Configuration used to dispatch this event.
75
* @param {string} dispatchMarker Marker identifying the event target.
76
* @param {object} nativeEvent Native browser event.
77
* @extends {SyntheticUIEvent}
78
*/
79
function SyntheticKeyboardEvent(dispatchConfig, dispatchMarker, nativeEvent) {
80
SyntheticUIEvent.call(this, dispatchConfig, dispatchMarker, nativeEvent);
81
}
82
83
SyntheticUIEvent.augmentClass(SyntheticKeyboardEvent, KeyboardEventInterface);
84
85
module.exports = SyntheticKeyboardEvent;
86
87