Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Form input events for Macbook touch bar

Are there any events that trigger on form elements when a user enters input via the MacBook Touch Bar?

Here is a trivial example:

<textarea id="textarea"></textarea>

(function($) {
  $('#textarea')
    .on('keyup', function() {
      console.log('keyup');
    })
    .on('keydown', function() {
      console.log('keydown');
    })
    .on('keypress', function() {
      console.log('keypress')
    });
})(jQuery);

On Safari, when I "type" using the Touch Bar (e.g. tapping on emojis or autosuggested text), I don't see any events in the web inspector console. However, the regular keyboard will fire the keydown, keypress, and keyup events as expected.

like image 260
Robert Chen Avatar asked Feb 20 '17 22:02

Robert Chen


Video Answer


1 Answers

It doesn't look like the touch bar triggers key events.

An alternative would be to listen to the input event.

As stated by the relevant MDN documentation, the event is fired whenever the value is changed, which means that it will work when the touch bar changes the input/textarea value.

The DOM input event is fired synchronously when the value of an <input> or <textarea> element is changed.

Usage:

document.querySelector('textarea').addEventListener('input', function (event) {
    // ...
});

or...

$('textarea').on('input', function (event) {
  // ...
});
like image 74
Josh Crozier Avatar answered Oct 16 '22 10:10

Josh Crozier