54

I am developing the front end site for a coupon company, and I have a page where the user only needs to input phone number and $$ spent. We came up with a fun on-screen keyboard built in Javascript, that is easy to use, and fast. However, I am looking for a solution to stop the soft keyboard from popping when the user focuses and enters text/numbers in those fields.

I know about the "number/phone/email" type attributes that HTML5 came up with. However, at the risk of sounding crazy, I really want to just use my on-screen keyboard.

Note: this web site is mostly targeted to tablets.

Thanks.

Chris Calo
  • 6,567
  • 7
  • 44
  • 57
crodica
  • 643
  • 1
  • 5
  • 6
  • 1
    This _is_ crazy. If you use standard input fields, use the standard input methods. – egrunin Jun 07 '12 at 21:47
  • 11
    There are nevertheless many benefits of having an on-screen keyboard, besides them being fun to use and to build. For one, they are highly customizable to closely target your audience's needs, second - safety, third - screen real estate. ;-) – crodica Jun 08 '12 at 13:33
  • Ah, I see I misunderstood "front end *site*" as "front end *app*". You have more leeway in user expectations with a website. – egrunin Jun 08 '12 at 17:06

8 Answers8

55

Scott S's answer worked perfectly.

I was coding a web-based phone dialpad for mobile, and every time the user would press a number on the keypad (composed of td span elements in a table), the softkeyboard would pop up. I also wanted the user to not be able to tap into the input box of the number being dialed. This actually solved both problems in 1 shot. The following was used:

<input type="text" id="phone-number" onfocus="blur();" />
thamind
  • 670
  • 5
  • 5
  • 1
    Actually, this answer worked for me better than the answer from @ScottS, no disrespect to his answer. I suspect that the elapsed time between the html input statement and the $(document).ready() allowed the keyboard to fire, whereas the onfocus=blur(); would be faster, at least in my mind and prevented the keyboard from firing, – HumbleBeginnings May 06 '16 at 20:06
48

Since the soft keyboard is part of the OS, more often than not, you won't be able to hide it - also, on iOS, hiding the keyboard drops focus from the element.

However, if you use the onFocus attribute on the input, and then blur() the text input immediately, the keyboard will hide itself and the onFocus event can set a variable to define which text input was focused last.

Then alter your on-page keyboard to only alter the last-focused (check using the variable) text input, rather than simulating a key press.

Scott Stevens
  • 2,406
  • 1
  • 16
  • 28
  • Thank you Scott. Your suggestion worked perfectly when tested on a regular html form, without the on-screen keypad. It seems that the jquery mobile that I am implementing, or the keyboard object that I am using are creating their own focus events, that I have not yet found (the 'focus' is namespaced somewhere in the library). For now, our client loved the idea but the fact that the soft keyboard kept popping, had been a turn off, so we are ditching this feature. :( I am interested in working it out, so if anyone is interested, here is the keyboard library: https://github.com/Mottie/Keyboard – crodica Jun 08 '12 at 14:50
  • 6
    Just a thought - what would happen if you put a transparent div over each input, then use the onClick method? For instance: `
    Text:
    `. The relative (or absolute) positioning of a parent div causes an absolutely positioned child to be relative to its parent.
    – Scott Stevens Jun 08 '12 at 15:15
  • android keyboard doesn't hide after click go button? any idea for this? – sirius2013 Oct 25 '18 at 22:29
24

For further readers/searchers:

As Rene Pot points out on this topic,

By adding the attribute readonly (or readonly="readonly") to the input field you should prevent anyone typing anything in it, but still be able to launch a click event on it.

With this method, you can avoid popping up the "soft" Keyboard and still launch click events / fill the input by any on-screen keyboard.

This solution also works fine with date-time-pickers which generally already implement controls.

Jacopo Pace
  • 352
  • 4
  • 11
ndeufemia
  • 455
  • 3
  • 13
  • 2
    readonly="true" is not valid HTML. It should be readonly="readonly", or simply readonly with no attribute value – carpii Mar 31 '18 at 10:51
  • @carpii: IMHO, it is a shame W3C made such terrible decisions!!! They must have their reasons... :-( – André Caldas Jul 17 '20 at 16:49
6

Those answers aren't bad, but they are limited in that they don't actually allow you to enter data. We had a similar problem where we were using barcode readers to enter data into a field, but we wanted to suppress the keyboard.

This is what I put together, it works pretty well:

https://codepen.io/bobjase/pen/QrQQvd/

<!-- must be a select box with no children to suppress the keyboard -->
input: <select id="hiddenField" /> 
<span id="fakecursor" />

<input type="text" readonly="readonly" id="visibleField" />
<div id="cursorMeasuringDiv" />

#hiddenField {
  height:17px; 
  width:1px;
  position:absolute;
  margin-left:3px;
  margin-top:2px;
  border:none;
  border-width:0px 0px 0px 1px;
}

#cursorMeasuringDiv {
  position:absolute;
  visibility:hidden;
  margin:0px;
  padding:0px;
}

#hiddenField:focus {
  border:1px solid gray;  
  border-width:0px 0px 0px 1px;
  outline:none;
  animation-name: cursor;
  animation-duration: 1s;
  animation-iteration-count: infinite;
}

@keyframes cursor {
    from {opacity:0;}
    to {opacity:1;}
}

// whenever the visible field gets focused
$("#visibleField").bind("focus", function(e) {
  // silently shift the focus to the hidden select box
  $("#hiddenField").focus();
  $("#cursorMeasuringDiv").css("font", $("#visibleField").css("font"));
});

// whenever the user types on his keyboard in the select box
// which is natively supported for jumping to an <option>
$("#hiddenField").bind("keypress",function(e) {
  // get the current value of the readonly field
  var currentValue = $("#visibleField").val();

  // and append the key the user pressed into that field
  $("#visibleField").val(currentValue + e.key);
  $("#cursorMeasuringDiv").text(currentValue + e.key);

  // measure the width of the cursor offset
  var offset = 3;
  var textWidth = $("#cursorMeasuringDiv").width();
  $("#hiddenField").css("marginLeft",Math.min(offset+textWidth,$("#visibleField").width()));

});

When you click in the <input> box, it simulates a cursor in that box but really puts the focus on an empty <select> box. Select boxes naturally allow for keypresses to support jumping to an element in the list so it was only a matter of rerouting the keypress to the original input and offsetting the simulated cursor.

This won't work for backspace, delete, etc... but we didn't need those. You could probably use jQuery's trigger to send the keyboard event directly to another input box somewhere but we didn't need to bother with that so I didn't do it.

IKavanagh
  • 5,704
  • 11
  • 38
  • 44
Bob Jase
  • 89
  • 1
  • 3
  • 2
    I am having a similar issue in my project where I need to use barcode reader of a honeywell device to input the data. However unlike yours I dont have to show it in an input field.. What I did was bind the keyup listener to the entire document. When the barcode was read, the event used to trigger for each of the barcode characters. I pushed them in an array and then converted to string. I am using angular in my case – Aditya Sawant Jul 28 '18 at 12:09
  • Thanks So much. You had saved my day. And saved me from Java. – Hassan ALi Oct 08 '19 at 11:20
  • As far as it works for simple use its cool! It does not work for barcode scanner. – Ingus Nov 21 '19 at 09:57
3

example how i made it , After i fill a Maximum length it will blur from my Field (and the Keyboard will disappear ) , if you have more than one field , you can just add the line that i add '//'

var MaxLength = 8;
    $(document).ready(function () {
        $('#MyTB').keyup(function () {
            if ($(this).val().length >= MaxLength) {
               $('#MyTB').blur();
             //  $('#MyTB2').focus();
         }
          }); });
Sportac
  • 473
  • 6
  • 20
1

I am facing the same issue, I am able to hide android keyboard even focus is in textbox by just adding one css property

<input type="text" style="pointer-events:none" />

and it works fine...

Nikhil sHETH
  • 420
  • 4
  • 10
0

I could not use some of the suggestions provided.

In my case I had Google Chrome being used to display an Oracle APEX Application. There were some very specific input fields that allowed you to start typing a value and a list of values would begin to be displayed and reduced as you became more specific in your typing. Once you selected the item from the list of available options, the focus would still be on the input field.

I found that my solution was easily accomplished with a custom event that throws a custom error like the following:

throw "throwing a custom error exits input and hides keyboard";
Andrew Myers
  • 2,567
  • 5
  • 26
  • 36
0

I am fighting the soft keyboard on the Honeywell Dolphin 70e with Android 4.0.3. I don't need the keyboard because the input comes from the builtin barcode reader through the 'scanwedge', set to generate key events.

What I found was that the trick described in the earlier answers of:

input.blur();
input.focus();

works, but only once, right at page initialization. It puts the focus in the input element without showing the soft keyboard. It does NOT work later, e.g. after a TAB character in the suffix of the barcode causes the onblur or oninput event on the input element.

To read and process lots of barcodes, you may use a different postfix than TAB (9), e.g. 8, which is not interpreted by the browser. In the input.keydown event, use e.keyCode == 8 to detect a complete barcode to be processed.

This way, you initialize the page with focus in the input element, with keyboard hidden, all barcodes go to the input element, and the focus never leaves that element. Of course, the page cannot have other input elements (like buttons), because then you will not be able to return to the barcode input element with the soft keyboard hidden.

Perhaps reloading the page after a button click may be able to hide the keyboard. So use ajax for fast processing of barcodes, and use a regular asp.net button with PostBack to process a button click and reload the page to return focus to the barcode input with the keyboard hidden.

Roland
  • 3,391
  • 5
  • 32
  • 63