Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Virtual Keyboard API hide() isn't working

For a webpage running on Android Chrome, I'm attempting to implement the VirtualKeyboard API on an input field:

      <input
        id="my-input"
        placeholder="Click to edit"
        inputmode="text"
        virtualkeyboardpolicy="manual"
      />

The Javascript/jQuery part

      $("#my-input").on("focus", () => {
        navigator.virtualKeyboard.show();
      });

      $("#my-input").on("blur", () => {
        navigator.virtualKeyboard.hide();
      });

The show() method works fine, but the hide() methods doesn't work at all. Why is the hide() method being ignored?

Live demo with codesandbox

like image 216
Avi Kaminetzky Avatar asked May 14 '26 15:05

Avi Kaminetzky


1 Answers

From https://developer.mozilla.org/en-US/docs/Web/API/VirtualKeyboard/hide

This method only works if the currently-focused element's virtualKeyboardPolicy attribute is set to manual and inputmode isn't set to none.

blur is called only after losing the focus while hide works only if the input is focused.

$("#my-input").on("blur", () => {
  $("#my-input").is(":focus"); // FALSE!
  navigator.virtualKeyboard.hide();
});

You need to find another event (ideally before blur or focusout) and try to close the keyboard from there while the input still has focus.

like image 170
jokuskay Avatar answered May 17 '26 05:05

jokuskay



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!