ユーザーkeyCode
のkeyDown
. _ _
モバイル ブラウザーの場合、仮想キーボードをトリガーする必要があるため、click
イベントでトリガーされる CSS によって非表示の入力を使用します。これはうまく機能しますが、fennec (モバイル Firefox) でキーコードをリッスンしようとすると、奇妙な動作が発生します。
を聞くために使用する関数は次のとおりkeycode
です。
document.querySelectorAll('input')[0].addEventListener('keydown', handler);
function handler(e) {
e.preventDefault();
var k = (e.which) ? e.which : e.keyCode;
document.getElementById('log').innerHTML = k;
this.style.backgroundColor = "#FFAAFF";
}
<input type="text" />
<span id="log"></span>
Android 版 Firefox (v34 から v37)では、ユーザーが return を入力するまで起動しません。⏎</kbd>.
Actually, I found that if the value of the input is not empty, it works well, at least on load. So I thought of a workaround like this one :if(this.value=='')this.value='*';
Seems to work but if you spam it, the backspace ⌫</kbd> isn't blocked, so when the input is cleared, the bug comes back : the workaround doesn't fire either.
+This a ugly workaround, which I'm sure will create other bugs in other browsers.
document.querySelectorAll('input')[0].addEventListener('keydown', handler); function handler(e) { if(this.value=='')this.value='*'; e.preventDefault(); var k = (e.which) ? e.which : e.keyCode; document.getElementById('log').innerHTML = k; this.style.backgroundColor = "#FFAAFF"; }
<input type="text" value="*"/> <span id="log"></span>
B2G (デバイス上の Firefox OS 1.3 またはエミュレートされた 2.0) では、動作はさらに奇妙です:関数はバックスペース
のみを読み取ります。keyCode
⌫</kbd>(keycode8
) or return⏎</kbd>(keyCode13
) keys. Any other key will return0
.
それで、私の質問は、すべての主要なブラウザー、デスクトップまたはモバイル、および fennecで動作する、より良いクロスブラウザーの方法を知っていますか?keyCode
Ps: バニラ js でアプリを書いたとしても、どのライブラリでも解決策を確認できます。