-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetCharCode.js
More file actions
34 lines (30 loc) · 1006 Bytes
/
getCharCode.js
File metadata and controls
34 lines (30 loc) · 1006 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import isAppleSafari from './utils/isAppleSafari'
/**
* 返回触发事件的 charCode
* ========================================================================
* @method getCharCode
* @see https://developer.mozilla.org/zh-CN/docs/Web/API/KeyboardEvent/keyCode
* @see https://developer.mozilla.org/zh-CN/docs/Web/API/KeyboardEvent/charCode
* @param {Event} evt - Event 对象
* @return {Number} - 返回事件的 charCode
*/
const getCharCode = function (evt) {
let code = evt.keyCode || evt.charCode
// keycodes for webkit/safari
const webkitKeymap = {
63232: 38, // up
63233: 40, // down
63234: 37, // left
63235: 39, // right
63276: 33, // page up
63277: 34, // page down
25: 9 // The SHIFT-TAB (Safari provides a different key code in
// this case, even though the shiftKey modifier is set)
}
// webkit key normalization
if (isAppleSafari() && code in webkitKeymap) {
code = webkitKeymap[code]
}
return code
}
export default getCharCode