52

I'm wondering if there's a way to capture the iPhone's virtual keyboard's done button event, using JavaScript?

Basically, I just want to be able to call a JS function when the user clicks done.

insertusernamehere
  • 23,204
  • 9
  • 87
  • 126
Dan
  • 2,649
  • 4
  • 26
  • 22

10 Answers10

11

I was unable to track the 'done' button being clicked. It didn't register any clicks or keypresses. I had to addEventListeners for change, focusout and blur using jquery (because the project already was using jquery).

Jason Lydon
  • 7,074
  • 1
  • 35
  • 43
7

You need to do some kind of this:

$('someElem').focusout(function(e) {
    alert("Done key Pressed!!!!")
});

It worked for me, hope it will help you as well.

Krunal
  • 725
  • 7
  • 15
6

After searching and trying this solution basically is say:

 document.addEventListener('focusout', e => {});

tested on IPhone 6s

oron tech
  • 87
  • 1
  • 2
2

This question is kinda old, but I've found a hacky way recently to make this working.

The problem with the 'blur', 'focusout' events is that they fire even if user just tapped outside the input/textarea, and did not press the 'Done' button, in my case, UI should behave differently depending on what exactly have happened.

So to implement it, I've done the next thing:

  1. After showing the keyboard (the input received the focus), add click handler on the window via the addEventListener function. When user clicks on the window, remember the timestamp of the click in the variable (let's call it lastClick = Date.now())
  2. In the blur event handler, set a timeout for 10-20 ms to allow other events happening. Then, after the timeout, check if the blur event happened in a time difference lower for example than 50-100 ms than the lastClick (basically Date.now() - lastClick < 50). If yes, then consider it as a 'Done' button click and do corresponding logic. Otherwise, this is a regular 'blur' event.

The key here is that tapping on keyboard controls (including Done button) does not trigger the click event on the window. And the only other way to make keyboard hide is basically tap on other element of the page and make the textarea lose focus. So by checking when the event happened, we can estimate whether that's a done button click or just blur event.

MrOnlineCoder
  • 333
  • 3
  • 15
1

At the risk of my response not being an answer to the question as asked... I'd still like to pose an alternate way of thinking here. We may be misunderstanding the intended purpose of "done".

Let's say that our input is a search input...

I don't think that "done" is intended to be the "Do the Search" / "Go" action.

I understand why the confusion is there, especially when not providing a suitable inputmode to the search field.

Image: Keyboard WITHOUT inputmode

Adding inputmode="search" to your input will give your keypad a very prominent [ GO ] button that we can use to execute a search (in this example).

Image: Keyboard WITH inputmode

I feel that with the presence of the [ GO ] button, enter keyup detection , and possibly a blur event for taps outside the keyboard, the behavior of "Done" as "collapse keyboard" makes more sense and reduces (possibly eliminates) the need to think of that as the action button for our input. In effect "done" now becomes the close button for the native keypad (divorced from the input), not the action button, which I believe is as intended.

0

The answer by oron tech using an event listener is the only one that works cross platform.

 document.getElementById("myID").addEventListener("focusout", blurFunction);

 function blurFunction() { // Do whatever you want, such as run another function
const myValue = document.getElementById("myID").value;
myOtherfunction(myValue);
}
Kelvin Aitken
  • 433
  • 6
  • 18
0

When you press the Done button, it essentially just changes the height of the screen:

  const prevHeight = useRef(window.innerHeight);

  useEffect(() => {
    function checkHeight() {
      const currentHeight = window.innerHeight;

      if (currentHeight > prevHeight.current) {
        onBlur();
      }

      prevHeight.current = currentHeight;

      requestAnimationFrame(checkHeight);
    }

    const animationFrameId = requestAnimationFrame(checkHeight);

    return () => {
      cancelAnimationFrame(animationFrameId);
    };
  }, []);
John B
  • 1
-1

attach a blur event to the text box in question. The done fire will fire this event.

Dan
  • 2,649
  • 4
  • 26
  • 22
  • 9
    Your idea is far from beign ideal solution. The matter is that blur event on a form control may be triggered in many other ways, not just by hitting DONE, e.g. tapping elsewhere on the page or on a neighbouring control. – mcmlxxxiii Jun 01 '13 at 00:01
  • 1
    Why this answer has upvotes? It does not even relate to the question and is in fact an inappropriate answer – writeToBhuwan Jun 03 '15 at 08:01
-1

"Change" event works fine

document.querySelector('your-input').addEventListener('change',e=>
    console.log('Done button was clicked')
);
-11

The done key is the same as the enter key. So you can listen to a keypress event. I'm writing this using jQuery and i use it in coffee script so I'm trying to convert it back to js in my head. Sorry if there is an error.

$('someElem').bind("keypress", function(e){
   // enter key code is 13
   if(e.which === 13){
     console.log("user pressed done");
    } 
})
Alex Reynolds
  • 6,264
  • 4
  • 26
  • 42
  • 5
    I've checked your approach and it is not working for me with textarea. – mcmlxxxiii Jun 01 '13 at 00:00
  • Sorry I didn't see mention of textarea. I know it works for inputs. What do you mean text area? are you referring to UITextAreas which are native or HTML inputs? – Alex Reynolds Jun 03 '13 at 22:11
  • Well, I meant old plain HTML . – mcmlxxxiii Jun 04 '13 at 01:11
  • Rude to downvote when it is a solution to the posed problem. If you have an issue please comment so the askers knows why this answer isn't the best. – Alex Reynolds Feb 19 '14 at 00:29
  • 14
    The done button is not the same as enter. The "Go" button is the equivalent to "Enter" / key code 13. – smj2393 Aug 25 '15 at 11:37
  • 2
    I've tried your jsfiddle on mobile safari and it doesn't trigger an alert when you press "done". I also tried to implement it myself and to improve on your jsfiddle. I think you just confused the blue "Done" at the top with the "Go" or "Return" at the bottom. – Ruben Aug 03 '16 at 13:47
  • 1
    keypress code 13 is for the return or enter key. As you can see my answer is posted in 2013. If safari has changed the return/enter/return key of the ios software keyboard since 2013, then it no longer works. Please keep in mind code changes over time. Listen to the keypress events in fiddle and whatever code is fired when you hit the desired key, that is what you use. I don't do JS anymore so keypress event may not still be used. It is 2016 – Alex Reynolds Aug 09 '16 at 10:43
  • 4
    @AlexReynolds it wouldn't hurt to update your answer since it's out of date. It's currently selected as the correct answer. Since SO strives to be canonical it would be valuable to indicate as such. – Mark Fox Sep 19 '16 at 23:35
  • 1
    @MarkFox I haven't been a JS dev in 3 years so I don't know the updated answer. If you have one, it would be great to edit the answer or post a new one. That would be amazing. Thanks – Alex Reynolds Sep 20 '16 at 12:49
  • I'd like to know the answer to this question. This answer is not entirely correct! – mesqueeb Mar 10 '17 at 02:08
  • This is a real bummer because Google search sends you to this answer and all the other ones are either closed (and labeled as duplicate of this SO) or unanswered. This should no longer be the correct answer if it is outdated. – Francis Jun 26 '18 at 18:32