对于任何带有onKeyUp的按键,iPad蓝牙键盘都会返回0的键码 [英] iPad Bluetooth keyboard returns keycode of 0 for any key with onKeyUp

查看:162
本文介绍了对于任何带有onKeyUp的按键,iPad蓝牙键盘都会返回0的键码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有些客户在使用iPad蓝牙键盘将文字输入我们的某个内部网站时报告了问题。在屏幕键盘上使用桌面或iPad时,主要按输入某个输入可以正常工作,但使用连接到iPad的蓝牙键盘时则不行。

Some clients have been reporting issues when using their iPad Bluetooth keyboards for entering text into one of our internal sites. Mainly pressing enter on a certain input would work fine when using desktop or the iPad on screen keyboard, but not when using a Bluetooth keyboard connected to the iPad.

经过调查,当连接到蓝牙键盘时, onKeyUp 的任何输入都会返回0作为键码iPad。该演示工作正常,但是当使用屏幕键盘时它不起作用,因为键码返回0. 我创建了这个jsFiddle示范。它在Chrome和Safari for iPad上进行了测试,结果与 onKeyPress 一样正常,但只返回0 onKeyUp

Upon investigation it appears that any input to an onKeyUp returns 0 as the keycode when connected to a Bluetooth keyboard on the iPad. The demo works fine, however when using the onscreen keyboard it doesn't work because of the keycode returning 0. I created this jsFiddle to demonstrate. It was tested on both Chrome and Safari for iPad with the same results of working fine with onKeyPress but returning only 0 with onKeyUp.

$('#inputKeyUp').keyup(function (event){
	$("#outputKeyUp").text("Key Up Key: " + event.which);
});

$('#inputKeyPress').keypress(function (event){
	$("#outputKeyPress").text("Key Press Key: " + event.which);
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea id="inputKeyUp">keyup</textarea>
<div id="outputKeyUp">Key Up Key:</div>
<b/>
<textarea id="inputKeyPress">keypress</textarea>
<div id="outputKeyPress">Key Press Key:</div>

编辑:刚向Apple报告错误。我们将看看是否有任何结果。

just reported the bug to Apple. We will see if anything comes of it.

推荐答案

测试研究



我刚刚对此进行了一些测试,并发现在iOS Safari上使用蓝牙键盘的 keyUp 事件中,唯一能够提供任何适当反馈的键属性 e.key e.charCode e.keyCode e.which 是以下键:

Testing study

I did some testing on this just now and discovered that on the keyUp event when using a Bluetooth keyboard on iOS Safari, the only keys that give any sort of proper feedback in terms of the properties e.key, e.charCode, e.keyCode and e.which are the following keys:


  • 逃生

  • 向上箭头

  • 向左箭头

  • 向右箭头

  • 向下箭头

  • Escape
  • Up arrow
  • Left arrow
  • Right arrow
  • Down arrow

所有其他密钥返回以下内容:

All other keys return the following:

{
    key: "Dead",
    charCode: 0,
    keyCode: 0,
    which: 0
}

这些特殊键(转义键和箭头键)仅在 e.key 属性,根据语法 UIKeyInput {PascalCasedKeyName}

These special keys (escape and arrow keys) only return a different value on the e.key property according to the syntax UIKeyInput{PascalCasedKeyName}:


  • UIKeyInputEscape

  • UIKeyInputUpArrow

  • UIKeyInputLeftArrow

  • UIKeyInputRightArrow

  • UIKeyInputDownArrow

  • UIKeyInputEscape
  • UIKeyInputUpArrow
  • UIKeyInputLeftArrow
  • UIKeyInputRightArrow
  • UIKeyInputDownArrow

在iOS上,基于我的快速学习,您可以在 keyUp 事件中识别的唯一键是 Escape 和四个箭头键,在 e.key 财产。这些值也出现在 keyDown 事件中。

On iOS, the only keys you can identify on the keyUp event, based on my quick study, are Escape and the four Arrow keys, by matching their name on the e.key property. These values also appear on the keyDown event.

如果您仍需要等到 keyUp 为您的应用程序触发事件,并且您需要匹配除这些特殊键之外的其他键,我能想到的唯一解决方案是使用 keyDown 用于捕获密钥的事件,但随后在 keyDown keyUp c 内的事件 c>事件如此:

If you still need to wait until the keyUp event fires for your applications, and you need to match keys other than these special ones, the only solution I can come up with is to use a keyDown event for capturing the key, but then listen for the keyUp event inside that keyDown event like so:

el.addEventListener("keydown", e => {
    if (e.which === 13) // Enter key, or which ever key code you'd like
        el.addEventListener("keyup", function keyUp(e) {
            el.removeEventListener("keyup", keyUp, false) // Memory clean-up

            // Your code here
        }, false)
}, false)



此外



快速搜索UIKeyInput后,我发现 UIKeyInput 是UIResponder的子类使用的一组方法o实现简单的文本输入。 ( Apple的开发人员文档)这将解释这些关键名称的特殊语法。

Furthermore

After a quick search for "UIKeyInput" I discovered that UIKeyInput is "a set of methods a subclass of UIResponder uses to implement simple text entry". (Apple's Developer Documentation) This would explain the special syntax of these key names.

这篇关于对于任何带有onKeyUp的按键,iPad蓝牙键盘都会返回0的键码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆