键位扫描码,用event.keyCode获得

本文详细介绍了通过JavaScript获取键盘按键的扫描码及如何利用event对象判断用户按下的是哪个键。此外,还提供了几种实用的判断函数,如判断是否为数字键、字母键或字符键等。
一、键位扫描码,用event.keyCode获得
8
 
退格    
9
 
Tab     
12
 
小键盘<5>numlock未锁定时
13
 
回车    
16
 
Shift    
17
 
Ctrl     
18
 
Alt    
19
 
Pause Break
20
 
CapsLock 
27
 
Esc     
33
 
PgUp    
34
 
PgDn    
35
 
End     
36
 
Home   
37
 
38
 
39
 
40
 
45
 
Insert和小键盘的Ins
46
 
Delete和小键盘的Del
48
 
0 )
49
 
1 !
50
 
2 @
51
 
3 #
52
 
4 $
53
 
5 %
54
 
6 ^
55
 
7 &
56
 
8   *
57
 
9 (
65
 
A(小写一样)
66
 
B
67
 
C
68
 
D
69
 
E
70
 
F
71
 
G
72
 
H
73
 
I
74
 
J
75
 
K
76
 
L
77
 
M
78
 
N
79
 
O
80
 
P
81
 
Q
82
 
R
83
 
S
84
 
T
85
 
U
86
 
V
87
 
W
88
 
X
89
 
Y
90
 
Z
91
 
左窗键
92
 
右窗键
93
 
仿右鼠标键
96
 
0(小键盘)
97
 
1(小键盘)
98
 
2(小键盘)
99
 
3(小键盘)
100
 
4(小键盘)
101
 
5(小键盘)
102
 
6(小键盘)
103
 
7(小键盘)
104
 
8(小键盘)
105
 
9(小键盘)
106
 
*(小键盘
107
 
+(小键盘)
109
 
-(小键盘)
111
 
/(小键盘)
112
 
F1      IE占用
113
 
F2     
114
 
F3      IE占用
115
 
F4      IE占用
116
 
F5      IE占用
117
 
F6      IE占用
118
 
F7     
119
 
F8     
120
 
F9     
121
 
F10    
取不到
 
F11     
取不到
 
F12    
取不到
 
Print Screen SysRq
144
 
NumLock
145
 
Scroll Lock
186
 
:    ;   
187
 
+    =  
188
 
<    ,   
189
 
-    _   
190
 
>    .   
191
 
?    /   
192
 
`    ~   
219
 
{    [   
220
 
|    /    
221
 
}    ]   
222
 
"    '   
 
二、检测同时按下的控制键用:
event.altKey     true为按下了Alt键
event.ctrlKey    true为按下了Ctrl键
event.shiftKey   true为按下了Shift键
 
三、判断函数
//是否数字键
function isnumkey(nkey)
{
if(!event.altKey && !event.ctrlKey && !event.shiftKey &&
(nkey>=48 && nkey<=57 || nkey>=96 && nkey<=105))
return true;
else
return false;
}
 
//是否字母键
function isalphakey(nkey)
{
if(!event.altKey && !event.ctrlKey && nkey>=65 && nkey<=90)
return true;
else
return false;
}
 
//是否字符键
function ischarkey(nkey)
{
if(!event.altKey && !event.ctrlKey && (nkey>=65 && nkey<=90 ||
nkey>=48 && nkey<=57 || nkey>=96 && nkey<=105 || nkey==106 ||
nkey==107 || nkey==109 || nkey==110 || nkey==111 ||
nkey>=186 && nkey<=192 || nkey>=219 && nkey<=222))
return true;
else
return false;
}

 
std::list<NotifyArgs> KeyboardInputMapper::processKey(nsecs_t when, nsecs_t readTime, bool down, int32_t scanCode, int32_t usageCode) { std::list<NotifyArgs> out; int32_t keyCode; int32_t keyMetaState; uint32_t policyFlags; int32_t flags = AKEY_EVENT_FLAG_FROM_SYSTEM; if (getDeviceContext().mapKey(scanCode, usageCode, mMetaState, &keyCode, &keyMetaState, &policyFlags)) { keyCode = AKEYCODE_UNKNOWN; keyMetaState = mMetaState; policyFlags = 0; } nsecs_t downTime = when; std::optional<size_t> keyDownIndex = findKeyDownIndex(scanCode); //#ifdef OPLUS_FEATURE_ROTATE_VOLUMEKEY //Xiaobo.Ding@ANDROID.Input 2022/09/09, Add for ROTATE_VOLUMEKEY if (mSupportRotateVolume) { keyCode = rotateVolumeKeyCode(keyCode, down); } //endif /* OPLUS_FEATURE_ROTATE_VOLUMEKEY */ if (down) { // Rotate key codes according to orientation if needed. if (mParameters.orientationAware) { keyCode = rotateKeyCode(keyCode, getOrientation()); } // Add key down. if (keyDownIndex) { // key repeat, be sure to use same keycode as before in case of rotation keyCode = mKeyDowns[*keyDownIndex].keyCode; downTime = mKeyDowns[*keyDownIndex].downTime; flags = mKeyDowns[*keyDownIndex].flags; } else { // key down if ((policyFlags & POLICY_FLAG_VIRTUAL) && getContext()->shouldDropVirtualKey(when, keyCode, scanCode)) { return out; } if (policyFlags & POLICY_FLAG_GESTURE) { out += getDeviceContext().cancelTouch(when, readTime); flags |= AKEY_EVENT_FLAG_KEEP_TOUCH_MODE; } KeyDown keyDown; keyDown.keyCode = keyCode; keyDown.scanCode = scanCode; keyDown.downTime = when; keyDown.flags = flags; mKeyDowns.push_back(keyDown); } onKeyDownProcessed(downTime); } else { // Remove key down. if (keyDownIndex) { // key up, be sure to use same keycode as before in case of rotation keyCode = mKeyDowns[*keyDownIndex].keyCode; downTime = mKeyDowns[*keyDownIndex].downTime; flags = mKeyDowns[*keyDownIndex].flags; mKeyDowns.erase(mKeyDowns.begin() + *keyDownIndex); } else { // key was not actually down ALOGI("Dropping key up from device %s because the key was not down. " "keyCode=%d, scanCode=%d", getDeviceName().c_str(), keyCode, scanCode); return out; } } if (updateMetaStateIfNeeded(keyCode, down)) { // If global meta state changed send it along with the key. // If it has not changed then we'll use what keymap gave us, // since key replacement logic might temporarily reset a few // meta bits for given key. keyMetaState = mMetaState; } DeviceId deviceId = getDeviceId(); // On first down: Process key for keyboard classification (will send reconfiguration if the // keyboard type change) if (down && !keyDownIndex) { KeyboardClassifier& classifier = getDeviceContext().getContext()->getKeyboardClassifier(); classifier.processKey(deviceId, scanCode, keyMetaState); getDeviceContext().setKeyboardType(classifier.getKeyboardType(deviceId)); } KeyboardType keyboardType = getDeviceContext().getKeyboardType(); // Any key down on an external keyboard should wake the device. // We don't do this for internal keyboards to prevent them from waking up in your pocket. // For internal keyboards and devices for which the default wake behavior is explicitly // prevented (e.g. TV remotes), the key layout file should specify the policy flags for each // wake key individually. if (down && getDeviceContext().isExternal() && !mParameters.doNotWakeByDefault && !(keyboardType != KeyboardType::ALPHABETIC && isMediaKey(keyCode))) { policyFlags |= POLICY_FLAG_WAKE; } //#ifndef OPLUS_FEATURE_INPUT_LASER //Xingxing.Guo@ANDROID.6722667, 2024/03/04, add for pad laser if (getDeviceContext().isLaserPencilDevice()) { ALOGD("Laser from device %s " "keyCode=%d, scanCode=%d", getDeviceName().c_str(), keyCode, scanCode); if (!mKeyboardConfig.oplusConfigParameters.laserEnabled) { ALOGV("Laser is not supported!"); return out; } else if (policyFlags & POLICY_FLAG_WAKE) { policyFlags &= ~POLICY_FLAG_WAKE; } } //#endif /* OPLUS_FEATURE_INPUT_LASER */ if (mParameters.handlesKeyRepeat) { policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT; } //#ifdef OPLUS_FEATURE_INPUT // Xingxing.Guo@ANDROID.INPUT, 7251550, 2024/06/06, add for spruce. if (SpruceSwipEnable) { SpruceSwipEnable = false; when = SpruceSwipValue; } //#endif OPLUS_FEATURE_INPUT */ out.emplace_back(NotifyKeyArgs(getContext()->getNextId(), when, readTime, deviceId, mSource, getDisplayId(), policyFlags, down ? AKEY_EVENT_ACTION_DOWN : AKEY_EVENT_ACTION_UP, flags, keyCode, scanCode, keyMetaState, downTime)); //#ifdef OPLUS_EXTENSION_DEBUG //luochuang@ANDROID.INPUT, 2021/05/25, saupwkProcessKey feature extern void __attribute__((weak)) saupwkProcessKey(int32_t scanCode, bool down); if(saupwkProcessKey) { ALOGV("theia start saupwkProcessKey"); saupwkProcessKey(scanCode, down); } //#endif /* OPLUS_EXTENSION_DEBUG */ return out; }这段代作用是什么,跳过客制化内容,讲解
最新发布
09-19
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值