IE не всегда отвечает на событие keyup в одном из моих сценариев.Отключить ключ, выпущенный без использования события keyup - Javascript
Я искал альтернативный способ определить, был ли выпущен ключ.
Учитывая, что удерживаемая клавиша повторяет событие keydown с интервалами (кроме клавиш модификатора на Mac), я думал, что можно будет увеличивать переменную и прослушивать точку, в которой она перестала увеличиваться. Когда он перестает увеличиваться, ключ был выпущен?
К сожалению, в некоторых случаях (не всегда) мой скрипт обнаруживает конец приращения, пока ключ все еще удерживается. Он имеет тенденцию терпеть неудачу, если ключ удерживается для повторных коротких интервалов. Я тестировал с IE и FF.
Я пропустил 2 секунды между проверкой каждого приращения. Установка моей панели управления Windows на самые медленные настройки клавиатуры, вероятно, будет 1 секунда.
<!DOCTYPE html>
<html>
<head>
<title>Detect keyup not using keyup event using Javascript</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
// opening variables
var keyDownCount = 0;
var nextLastTimeout1 = false;
var nextLastTimeout2 = false;
var lastCount = false;
var nextCount = false;
// function to compare the last two outcomes for keyDownCount by assigning them to variables lastCount and nextCount
function nextLastCount() {
if (lastCount) {
nextCount = keyDownCount;
if (lastCount === nextCount) {
// clear any outstanding timeouts
clearTimeout(nextLastTimeout1);
clearTimeout(nextLastTimeout2);
// they match, display the count in the html
document.getElementById('matched-next-last').innerHTML = keyDownCount;
} else {
// clear any outstanding timeouts
clearTimeout(nextLastTimeout1);
clearTimeout(nextLastTimeout2);
// reset variable
lastCount = false;
// they don't match, call the function again after allowing sufficient time for the key repetition rate to increment the keyDownCount
nextLastTimeout1 = self.setTimeout("nextLastCount()", 2000);
}
} else {
lastCount = keyDownCount;
if (lastCount === nextCount) {
// clear any outstanding timeouts
clearTimeout(nextLastTimeout1);
clearTimeout(nextLastTimeout2);
// they match, display the count in the html
document.getElementById('matched-next-last').innerHTML = keyDownCount;
} else {
// clear any outstanding timeouts
clearTimeout(nextLastTimeout1);
clearTimeout(nextLastTimeout2);
// reset variable
nextCount = false;
// they don't match, call the function again after allowing sufficient time for the key repetition rate to increment the keyDownCount
nextLastTimeout2 = self.setTimeout("nextLastCount", 2000);
}
}
}
// keydown listener
document.addEventListener('keydown', function(e) {
if (!e) e = window.event;
// listen for alt key down
if (e.altKey) {
if (keyDownCount === 0) {
// call nextLastCount() to start comparing the last two outcomes for keyDownCount
// allow sufficient time for the key repetition rate to increment keyDownCount
setTimeout("nextLastCount()", 2000);
}
// increment the counter on each keydown repeat
keyDownCount++;
// display the current count in the html
document.getElementById('display-count').innerHTML = keyDownCount;
}
});
// keyup listener
document.addEventListener('keyup', function(e) {
if (!e) e = window.event;
// listen for alt key released
if (!e.altKey) {
// clear any outstanding timeouts
clearTimeout(nextLastTimeout1);
clearTimeout(nextLastTimeout2);
// reset the counter and the html fields when the keys are released
keyDownCount = 0;
document.getElementById('display-count').innerHTML = keyDownCount;
document.getElementById('matched-next-last').innerHTML = "";
}
});
</script>
</head>
<body>
<p>Hold down the alt key to start the counter, relese to reset.</p>
<p>keyDownCount is: <span id="display-count"></span>
</p>
<p>Matching next and last detected on key count of: <span style="color:blue;" id="matched-next-last"></span>
</p>
</body>
</html>
Это, возможно, звучит как [проблема XY] (http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Если ваше окно теряет фокус, оно больше не будет принимать ключевые события *. EG нажмите клавишу вниз, вы получите keydown, теперь щелкните в другое приложение и отпустите ключ - вы не видите keyup, так как это событие произошло в другом приложении. Вернитесь немного назад и, возможно, переосмыслите, для чего вы хотите получить это событие? –