shiftKey in Safari on iOS

shiftKey in Safari on iOS

本文关键字:iOS on Safari in shiftKey      更新时间:2023-09-26

有没有办法在javascript中确定移动键盘上是否按下了移位键,并将其与大写锁定(两次按下移位键)区分开来

一些事实

首先,让我们看看关于iOS键盘的一些事实,我想你已经知道了:

  • 当您进入键盘模式时,shift键始终处于激活状态
  • Caps Lock必须手动激活(我想这并没有被广泛使用)

iPhone Shift键处理

我对这个问题进行了一些调查,以下是我的发现:

  • shift密钥触发无密钥事件

  • 除了在iOS应用程序(duh)中,没有特殊的iPhone浏览器API来检测是否按下了换档键

  • iOS触发的keydownkeypresskeyup事件看起来很正常,只是它们没有指示shiftKey的使用,除了它们的时间戳和类型无法区分之外。

  • 由于此问题,您无法在iOS中手动调度键盘事件,keyCodewhich是只读的,并且始终设置为0。重新触发键盘事件以获得有关换档键仍处于打开状态的指示是不可能的。

  • 实际上,iPhone将shift密钥视为某种Short Term Caps Lock密钥。不同的是,通常情况下,你激活它一次,它就会自动停用。

可以做什么

我假设您想在输入字段上指示用户是否应该小心按下Shift/Caps Lock(例如,密码字段)。我想出了一些变通办法,但我认为总比什么都没有好。

你也可以在这里测试jsfiddle。

DOM设置

<div id="wrapper">
  <label for="test">ENTER SOMETHING HERE</label>
  <input type="text" name="test" id="test"/>
</div>
<div id="warning"></div>​

Javascript

这会检查用户是否输入了大写的输入,如果输入了两个大写字母,则假设用户使用的是caps lock

var isCaps = false,
isUppercase = false,
str = '',
test = document.getElementById('test'),
warning = document.getElementById('warning');
function capsDetection(e) {
    // Since where on iOS, we at least don't have to care 
    // about cross-browser stuff
    var s = String.fromCharCode(e.which);
    isCaps = isUppercase;
    // if the char doesn't match its lower case friend, and the shift key is 
    // not pressed (which is always the case on iOS, but we leave it there 
    // for readability), we have uppercase input
    isUppercase = (s.toUpperCase() === s && s.toLowerCase() !== s && !e.shiftKey);
    // if its the second uppercase input in a row, we may have caps lock input
    isCaps = isCaps && isUppercase;
    // set the warning
    if (isUppercase && !isCaps) {
        str = 'You where using the shift key';
    }
    else if (isCaps) {
        str = 'Caps lock seems to be activated';
    } else {
        str = '';
    }
    warning.innerHTML = str;
}
// the right event properties are only available on keypress
test.addEventListener('keypress', capsDetection);

正如我所说,总比什么都没有好,但如果你需要知道在没有用户进行任何输入的情况下按下了shift键,这不是一个解决方案。现在看来这是不可能的。

我认为这在Javascript中是不可能的。不过,目标C是可能的。

http://developer.apple.com/library/safari/#documentation/UserExperience/Reference/TouchEventClassReference/TouchEvent/TouchEvent.html