我正在尝试使用组合键[[alt + i创建快捷方式,该组合通常会键入一个死键ˆ,我尝试了以下操作:
$(document).ready(function () {
$("#txtInput").keydown(
function (event) {
if (event.which == 229 && event.altKey === true) {
alert(event);
event.preventDefault();
event.stopPropagation();
}
}
);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<input id="txtInput" type="text" />
条件内的代码已执行,但在我的。是否可以防止键入此文本?"#txtInput"
中仍键入ˆ
$("#txtInput").keydown(
function (event) {
if (('Ii'.indexOf(String.fromCharCode(event.which)) >= 0) && event.altKey === true) {
console.log('Here you go');
// Do stuff here
event.preventDefault();
event.stopPropagation();
}
}
);
$('#txtInput').keydown(function(e){
if( e.altKey && e.key == 'i'){
e.preventDefault();
console.log("Blocked");
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" id="txtInput">
function custom() {
console.log('The short-cut executed');
}
let isDead = false;
document.addEventListener('keydown', e => {
if (e.altKey && e.code === 'KeyI') {
custom();
};
if (isDead) {
e.preventDefault();
}
isDead = (e.key === 'Dead');
});
<input>
死键本身无法防止,但是死键之后产生的字符。由于现在不赞成使用键盘事件的许多属性,因此代码使用了相对较新的code property,它将提供所命中的物理键的代码。