笔记本numpad是什么键_numpad是什么键( 五 )


Safari和Chrome浏览器还为事件对象定义了类似key的keyIdentifier属性 , 对于功能键 , 返回“Shift”、“Enter”等 , 对于可打印字符 , 返回这个字符的Unicode编码 , 如A键 , 值就是“U+0041”;但目前所有浏览器都把这个属性删除了;
code属性:代表触发事件的物理按键;
txt.addEventListener(keydown, function(event){console.log(key: + event.key);console.log(code: + event.code);});字母键返回:“KeyLetter” , 如:“KeyA” , “KeyB”等;数字键返回:“Digitnumber” , 如:“Digit0”、“Digit1”等;特殊键按其名称编码:“Enter”、“Backspace”、“Tab”等;
在不同的系统上 , 其所对应的物理按钮键值有差异 , 以下以windows为例:
键 code值
ESCEscape数字0-9Digit0-Digit9MinusMinus EqualEqualBackspace BackspaceTabTabA-ZKeyA-KeyZBracketBracketLeft/BracketRightEnterEnterControlControlLeft/ControlRightSemicolon SemicolonQuoteQuote Backquote Backquote ShiftShiftLeft/”ShiftRight”Backslash Backslash CommaComma PeriodPeriodSlashSlashAltAltLeft/”AltRight”SpaceSpaceCapsLock CapsLockF1-F12F1-“F12”PausePauseScrollLock ScrollLockPrintScreen PrintScreenNumpad0-9 Numpad0-“Numpad9”NumpadSubstract NumpadSubtract (减法)NumpadAddNumpadAdd(加法)NumpadDecimal NumpadDecimal NumpadMultiply NumpadMultiply IntBackslash IntlBackslash (反斜杠 , 除法)NumpadEqual NumpadEqual (等于)NumLockNumLockHomeHomeEndEndArrowUpArrowUpArrowLeftArrowLeftArrowRightArrowRightArrowDownArrowDownPageUpPageUpPageDownPageDownInsertInsertDeleteDeleteMetaMetaLeft/ “MetaRightContextMenuContextMenuPowerPowerBrowserSearch BrowserSearchBrowserFavoritesBrowserFavoritesBrowserRefreshBrowserRefreshBrowserStopBrowserStopBrowserForwardBrowserForwardBrowserBackBrowserBackLaunchApp1LaunchApp1LaunchMail“LaunchMail另外 , 针对输入法输入 , key属性是识别不了的 , 其会返回“Process“ , 而code属性返回还是其物理键码;
不同的语言 , key值是不同的 , 但code是一样的;如:
document.addEventListener(keydown, function(event){if(event.code == KeyZ &&(event.ctrlKey || event.metaKey)){console.log(Undo);}},false)示例:用键盘控制一个元素:结构与样式:
style.world{margin:0;padding:0;background-color:black;width:400px;height:400px;}#spaceship{fill:orange; stroke:red;stroke-width:2px;}/stylesvg class=worldpolygon id=spaceship points=15,0 0,30 30,30 //svgscriptrefresh();/scriptscriptvar shipSize = {width:30, height:30};var position = {x:200, y:200};var moveRate = 9, turnRate = 5, angle = 0;var spaceship = document.getElementById(spaceship);function updatePosition(offset){var rad = angle * (Math.PI / 180);position.x += (Math.sin(rad) * offset);position.y -= (Math.cos(rad) * offset);if(position.x0)position.x = 399;else if(position.x399)position.x = 0;if(position.y0)position.y = 399;else if(position.y399)position.y = 0;}function refresh(){var x = position.x - (shipSize.width / 2);var y = position.y - (shipSize.height / 2);var transform = translate( + x ++ y + ) rotate( + angle +15 15+ );spaceship.setAttribute(transform, transform);}/script应用:
window.addEventListener(keydown, function(event){if(event.preventDefaulted)return;switch(event.code){case KeyS:case ArrowDown:updatePosition(-moveRate);break;case KeyW:case ArrowUp:updatePosition(moveRate);break;case KeyA:case ArrowLeft:angle -= turnRate;break;case KeyD:case ArrowRight:angle += turnRate;break;}refresh();event.preventDefault();// 这样它就不会被处理两次}, true);refresh();repeat属性:是一个只读属性 , 返回一个布尔值 , 如果按键被一直按住 , 返回值为true
txt.addEventListener(keydown, function(event){console.log(event.repeat);},false);辅助键:键盘事件对象也支持辅助键 , 有altKey、ctrlKey、shiftKey和metaKey属性(与鼠标事件的辅助键一样):
function handlerEvent(event){var txt = document.getElementById(txt);event = event || window.event;txt.value += n + event.type;txt.value += nkeyCode: + event.keyCode;txt.value += ncharCode: + event.charCode;var arrKeys = [];if(event.shiftKey) arrKeys.push(shift);if(event.altKey) arrKeys.push(alt);if(event.ctrlKey) arrKeys.push(ctrl);txt.value += n按钮: + arrKeys; }getModifierState()方法:
DOM3级还为event对象添加了getModifierState()方法 , 该方法接收一个参数 , 即等于Shift、Control、AltGraph或Meta的字符串 , 表示要检测的辅助键 , 如果指定的辅助键是活动的 , 该方法返回true , 否则返回false;
if(event.getModifierState){console.log(event.getModifierState(Shift));}通过event对象的shiftKey、altKey、ctrlKey和metaKey属性也可以取得类似的属性;IE8及以下不支持此方法;