如何在javascript中激活更多键
我想在我的Javascript代码中添加更多键:如何在javascript中激活更多键,javascript,Javascript,我想在我的Javascript代码中添加更多键: 功能开放(e){ 如果(例如类型!=“模糊”){ 如果(例如keyCode===70){ 警报(“F”); } } } document.onkeydown=打开; 我从你的问题中得到的是,你想检测更多的按键。检测按键的最佳方法是开关语句 功能打开(e){ 如果(例如类型!=“模糊”){ 开关(如钥匙代码){ 案例70: 警报(“F”); 打破 案例65: 警报(“按下A”); 打破 违约: 警报(“我不知道该怎么用那把钥匙!”;//这行是
功能开放(e){
如果(例如类型!=“模糊”){
如果(例如keyCode===70){
警报(“F”);
}
}
}
document.onkeydown=打开;
我从你的问题中得到的是,你想检测更多的按键。检测按键的最佳方法是开关语句
功能打开(e){
如果(例如类型!=“模糊”){
开关(如钥匙代码){
案例70:
警报(“F”);
打破
案例65:
警报(“按下A”);
打破
违约:
警报(“我不知道该怎么用那把钥匙!”;//这行是可移动的
打破
}
}
}
document.onkeydown=打开代码>如果您不反对为此使用库,我发现它非常棒而且非常易于使用
下面是上面链接中的几个示例:
<script>
// single keys
Mousetrap.bind('4', function() { console.log('4'); });
Mousetrap.bind("?", function() { console.log('show shortcuts!'); });
Mousetrap.bind('esc', function() { console.log('escape'); }, 'keyup');
// combinations
Mousetrap.bind('command+shift+k', function() { console.log('command shift k'); });
// map multiple combinations to the same callback
Mousetrap.bind(['command+k', 'ctrl+k'], function() {
console.log('command k or control k');
// return false to prevent default browser behavior
// and stop event from bubbling
return false;
});
// gmail style sequences
Mousetrap.bind('g i', function() { console.log('go to inbox'); });
Mousetrap.bind('* a', function() { console.log('select all'); });
// konami code!
Mousetrap.bind('up up down down left right left right b a enter', function() {
console.log('konami code');
});
</script>
//单键
bind('4',function(){console.log('4');});
bind(“?”,function(){console.log('show shortcuts!');});
bind('esc',function(){console.log('escape');},'keyup');
//组合
bind('command+shift+k',function(){console.log('command-shift-k');});
//将多个组合映射到同一回调
bind(['command+k','ctrl+k'],function()){
log('command k或control k');
//返回false以防止默认浏览器行为
//并阻止事件冒泡
返回false;
});
//gmail样式序列
bind('gi',function(){console.log('gotoinbox');});
bind('*a',function(){console.log('select all');});
//科纳米代码!
Mousetrap.bind('up-up-down-left-right-left-right b a enter',函数(){
log('konami代码');
});
可能是你想要的
<script>
function OPEN(event) {
var x = event.which || event.keyCode;
alert( "The Unicode value is: " + String.fromCharCode(x));
// The Unicode value is: a
//The Unicode value is: b
}
</script>
功能打开(事件){
var x=event.which | | event.keyCode;
警报(“Unicode值为:“+String.fromCharCode(x));
//Unicode值为:a
//Unicode值为:b
}
然后将此属性添加到您的身体中
<body onkeydown="OPEN(event)">
我不太清楚你问什么?你想解决的问题是什么?然后添加它们,使用或使用。我相信你的意思是“检测更多的钥匙”