title: Cannot detect key event for Android Barcode Scanner input, JavaScript
body: <h2>Scope</h2>
  
  <p>I am attempting to detect character input from a barcode scanner integrated into a hand-held Android device in a simple web application using JavaScript. Since most barcode scanners behave like a keyboard wedge I am using detecting events <code>keypress</code> and/or <code>keyup</code> to interpret and handle the character input.</p>
  
  <p>I don't want the character input to populate a text input, so I have assigned the event handler to <code>document</code>, however, I know I could also assign it to the <code>window</code>. </p>
  
  <h2>Sample Code</h2>
  
  <p>My sample code for completing the task looks like this; <em>This sample doesn't differentiate between keyboard input and scanner input, but I have already solved that problem.</em></p>
  
  <pre><code>var barcodeString = "";
  
  $(document).keypress(function (event) {
      // If character is carriage-return prevent submit.
      if (event.which === 13) {
          event.preventDefault();
      }
      // Append other characters to barcodeString.
      barcodeString += event.key;
  });
  </code></pre>
  
  <h2>Problem</h2>
  
  <p>My problem is that many devices do not return any readable characters at all. When I monitor the character codes using <code>event.keyCode</code> I receive two codes: 229 and 13. This article <a href="http://lists.w3.org/Archives/Public/www-dom/2010JulSep/att-0182/keyCode-spec.html" rel="nofollow noreferrer">W3 keyCode in key events</a> and this StackOverflow answer <a href="https://stackoverflow.com/a/25509350/5184889">Is it OK to ignore keydown events with keyCode = 229?</a> outlines that the 229 code means the Input Monitor is busy. I can only assume this is either because:</p>
  
  <ul>
  <li>The barcode scanner locks the Input Monitor upon inserting the characters.</li>
  <li>The speed of the character input causes the Input Monitor to become too busy to detect character input.</li>
  </ul>
  
  <p>Interestingly enough, if I detect <code>event.keyCode</code> while scanning the text into a text input, the two character codes: 229 and 13 are detected and <em>only</em> afterward the text is then inserted into the text input.</p>
  
  <h2>Devices</h2>
  
  <p>The devices I am attempting to implement this on are Nautiz X2 made by the Handheld Group. And another Android device, which I am unable to identify the brand of but seems slightly more generic with different barcode scanning software.</p>
  
  <h2>Workarounds</h2>
  
  <h3>1</h3>
  
  <p>The Handheld Group do provide a JavaScript API for interacting with the Handheld Scanner. The way this works is they provide an APK for a custom browser named: Kiosk Browser and implement the interpretation of the JavaScript specific to the scanner there. Documentation can be found here (it's exceedingly short and not a long read): <a href="https://developer.handheldgroup.com/Kiosk_Browser/JavaScript-Scanner-Interface.html" rel="nofollow noreferrer">JavaScript Scanner Interface</a></p>
  
  <h3>2</h3>
  
  <p>The more generic Android device has the ability to 'slow-down' character input on the scanner to make the scanner more compatible with third-party applications. Behold, toggling this option allows me to detect character input. Although <code>event.key</code> always returns code 229 (this time from an unreadable character), but <code>String.fromCharCode(event.which)</code> will somewhat solve the problem (it doesn't deal with special characters). The Nautiz X2 does not have the option to 'slow-down' input in the software settings.</p>
  
  <h3>3</h3>
  
  <p>The alternative workaround I have considered is putting a text input onto the web page with the style <code>display: none</code> to hide it and use JavaScript to continually set focus on the input. The I can <strong>attempt</strong> to monitor <code>onChange</code> and handle the character input. I would really like to avoid this approach.</p>
  
  <h2>Summary</h2>
  
  <p>The workarounds I have presented seem awful and the first is not device independent. I was wondering if there is a better place to "dump" text input from the barcode scanner so I can handle it in my application? Or, is there an event I can monitor to detect the character input?</p>