Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I focus on a text input without showing on-screen keyboard

I'm making a chat service, and I want to support mobile devices well.

On a screenshot below, there is an input field. It allows typing text, and to avoid issues like "if the text field is unfocused typing does nothing" it automatically gets focused when clicking outside of it - this does improve the experience on desktop computers. However, on devices with software keyboard, this causes on-screen keyboard to appear on mobile devices which is distracting.

enter image description here

Considering clicking anywhere focuses the text input, is it somehow possible to make on-screen keyboard only appear when the text field is pressed? Or alternatively, somehow detect devices with software keyboard enabled and disable this feature for them. Preferably without explicitly trying to detect mobile devices, touch screen or whatever as there exist touch screen devices with hardware keyboards.

This issue I was able to reproduce in Google Chrome and Opera Mobile on Android, and apparently it happens on iPhones, although I have no device to test it on.

Here is a rather simple example of an issue. If you touch the pink rectangle, it will cause touch keyboard to appear, which I don't want.

<input type=text id=f>
<div style="background: pink; height: 200px; width: 200px" onclick="f.focus()">
like image 567
Konrad Borowski Avatar asked Apr 06 '18 11:04

Konrad Borowski


People also ask

How do you hide soft keyboard and keep input on focus?

You put your input in readOnly = true before the focus. The keyboard will not appear. And you put back the input in readOnly = false but not immediately. So you can use your scanner without having the keyboard popping up.

How do I keep my mobile keyboard from covering HTML input?

Adding a padding to the bottom of the page that is large enough for the keyboard enables content to be displayed as desired when the keyboard is visible. Using some javascript listeners and a CSS class, this padding can be added only when the keyboard is displayed.


1 Answers

The short answer is that this is a built in function and you can't stop it, but there are a couple of options to consider.

1) use the onFocus event to immediately trigger the blur event to hide the keyboard again.

2) set readonly="true" on the element, later remove it and trigger the focus event.

3) create a fake input element with div's and css, when you want to trigger the keyboard focus on a hidden input field and copy out the value of the input on the keyup event as the user types.

Hope these suggestions were helpful to you.

like image 51
digital-pollution Avatar answered Oct 23 '22 19:10

digital-pollution