如何添加Javascript侦听器以捕获从蓝牙条码扫描器到iPad的输入? [英] How can I add a Javascript listener to capture input from bluetooth barcode scanner to iPad?

查看:180
本文介绍了如何添加Javascript侦听器以捕获从蓝牙条码扫描器到iPad的输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在iPad上的javascript中记录按键操作。以下脚本适用于Chrome和Safari,但不适用于iPad Safari。蓝牙条码扫描器发送12位数作为击键,然后发送返回字符。有没有人有任何想法?

I'm having trouble logging keystrokes in javascript on the iPad. The following script works on Chrome and Safari, but not iPad Safari. The bluetooth barcode scanner sends 12 digits as keystrokes, then sends a return character. Does anyone have any ideas?

我想你需要一台iPad来试试这个:)

I think you will need an iPad to try this out :)

谢谢,
Mark

Thanks, Mark

$(document).ready(function(){
 $(document).keypress(function(e){
  if( e.keyCode == 13){
   alert($('#barcode').attr('value'));
   $('#barcode').attr('value','');
  }
  else{
   var key = String.fromCharCode(e.which);
   var new_val = $('#barcode').attr('value') + key;
   $('#barcode').attr('value',new_val);
  }
 });
});


推荐答案

适用于iOS的Safari不会触发DOM上的键盘事件不是表单组件的元素。这包括通常用于捕获页面上任何位置的击键的文档和正文。

Safari for iOS doesn't trigger keyboard events on DOM elements that are not components of a form. This includes the document and body which are usually used to capture keystrokes anywhere on the page.

触发页面文档或正文上击键事件的唯一方法是在输入或文本区域中触发它。在这种情况下,事件将正确地冒泡到正文和文档。

The only way to trigger a keystroke event on document or body of a page is to trigger it in an input or textarea. In that case, the event will correctly 'bubble' to the body and document.

然而,这可能是一个问题,因为Safari for iOS不允许我们给出来自javascript的元素焦点。

However, this might be a problem because Safari for iOS doesn't allow us to give an element focus from javascript.

目前,我们正在使用一个解决方案,用户必须在开始第一次扫描之前单击输入字段,输入字段为然后移出屏幕但保留焦点。

At the moment, we are using a solution where user has to click on an input field before starting the first scan, and the input field is then moved off-screen but retains focus.

如果有人有更好的解决方案,请分享。

If someone has a better solution, please share.

这篇关于如何添加Javascript侦听器以捕获从蓝牙条码扫描器到iPad的输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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