[英]Restore default value of arrows key
I'm using a script (impress.js) that bins some particular action to keyup and keydown events for left, right, up and down arrows. 我正在使用一个脚本(impress.js),该脚本将一些特定的动作绑定到左,右,上和下箭头的击键和击键事件。
In some particular moments (for example while typing in a textarea) I want back the default behaviour for the arrows. 在某些特定时刻(例如在输入文本区域时),我想返回箭头的默认行为。
I tried without success with 我尝试没有成功
$("a#show-ta").click( function() {
document.addEventListener("keydown", function ( event ) {
if (event.keyCode >= 37 && event.keyCode <= 40) {
return;
}
});
document.addEventListener("keyup", function ( event ) {
if (event.keyCode >= 37 && event.keyCode <= 40) {
return;
}
});
});
where a#show-ta
is the button that shows my textarea. a#show-ta
是显示我的文本区域的按钮。
You want to prevent the keypress from bubbling up to the document where (I assume) Impress binds its handlers: 您想防止按键压到文档(我假设)Impress绑定其处理程序的位置:
$("textarea").on('keyup keydown keypress', function(e) {
e.stopPropagation();
});
If you need the event in a specific zone, such as a texarea, you should stop the propagation of the event like this : 如果您需要在特定区域(例如德克萨斯州)中进行活动,则应停止该活动的传播,如下所示:
$('textarea').keydown( function(ev) {
ev.stopPropagation();
});
If the events are necessary for the whole page but you want to exclude while you are in a textarea, for example, you could raise a flag which you would validate in the event. 例如,如果事件对于整个页面都是必需的,但是您想在文本区域中排除事件,则可以引发一个标志,以便在事件中进行验证。
var keydownActivated = true;
$('textarea').keydown( function(ev) {
if (keydownActivated) {
ev.preventDefault();
// dostuff
}
});
This will more or less get you where you are going. 这或多或少会带您前往目的地。 Create a flag that tracks whether or not the textarea has focus, and check that flag in your current key press event handlers.
创建一个标记,以跟踪文本区域是否具有焦点,并在当前按键事件处理程序中检查该标记。 I can't see all of your code, so this is just a simple example:
我看不到所有代码,所以这只是一个简单的示例:
var textareaHasFocus = false;
var textarea = document.querySelector('#yourTextarea');
textarea.addEventListener('focus', function(event) {
textareaHasFocus = true;
}, false);
textarea.addEventListener('blur', function(event) {
textareaHasFocus = false;
}, false);
document.addEventListener("keydown", function ( event ) {
if (textareaHasFocus) return true;
// your current keyboard handler
});
document.addEventListener("keyup", function ( event ) {
if (textareaHasFocus) return true;
// your current keyboard handler
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.