简体   繁体   中英

How to catch keyboard input without visible input field in JavaScript

Situation:

  • HTML5 with jQuery Mobile on iPad ( EDIT : HTML5 page in a PhoneGap app)
  • external keyboard attached
  • text input field is hidden, no visible input field
  • no focus on text input field -> no soft keyboard shown

Goal:

  • catch keyboard input without showing & focusing on a text input field
  • Keyboard input is are variable phrases (eg name1, object2, phrase3)
  • Just start typing and the page should capture that typing

--> How can I detect the keyboard input into the hidden input field?

I know the keydown / keypress / keyup events, however they seem to require a visible input field

You can bind the event to the document like this:-

$(document).bind('keyup', function(e) {
    alert('testing');
});

You can also simply position the element off-canvas, without needing to alter its DOM/CSS visibility state.

<style> .inputValue{position:absolute;left:-99999px} </style>
<input type="text" class="inputValue" autofocus="autofocus"/>

And then at any given point when you require the input's value you call the below script.

<script> $('.inputValue').val(); </script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM