Skip to main content
Inspiring
June 3, 2020
Question

How to dispatch a keyboard event?

  • June 3, 2020
  • 1 reply
  • 1597 views

I am submitting this question a second time because my first question was marked as spam for some reason.

 

The tools guide (page 152) documents how to create and dispatch a keyboard event, using initKeyboardEvent. I have been unable to get that to work, and I haven't been able to find working sample on the internet.

 

// This test is in the onChanging event to eliminate lack of focus as the source of the problem
var odd = false;
editText.onChanging = function editText_onChanging() {
  // This odd is here just to prevent a looping problem if keyboard event starts to work and triggers onChanging again
  odd = !odd;
  if (!odd) {
    return;
  }
  var keyboardEvent = ScriptUI.events.createEvent('KeyboardEvent');
  // Tried with and without keydown, and with and without bubble.
  // In addition to 'r' I have tried 'R' and 0x52 (which is VK_R).
  // I have also tried using 'w' as my view (the main window).
  keyboardEvent.initKeyboardEvent('keydown', false, false, editText, 'r', keyboardEvent.DOM_KEY_LOCATION_STANDARD, '');
  keyboardEvent.initKeyboardEvent('keyup', false, false, editText, 'r', keyboardEvent.DOM_KEY_LOCATION_STANDARD, '');
  editText.dispatchEvent(keyboardEvent);
}

 

Is the keyboard dispatching stuff no longer working?

 

Thanks

This topic has been closed for replies.

1 reply

Inspiring
June 4, 2020

NT Productions recently uploaded this tutorial that might help https://www.youtube.com/watch?v=UBqdWdDmvao

Devin-Author
Inspiring
June 5, 2020

Thanks for the great reference. This person's channel looks like it will be useful. Unfortunately this particular video only addresses handling keyboard events, not firing keyboard events.

Inspiring
June 5, 2020

I don't think you can fire keyboard events otherwise you might be able to do nefarious things to peoples computers.