Highlighted

How to dispatch a keyboard event?

Explorer ,
Jun 03, 2020

Copy link to clipboard

Copied

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

TOPICS
Error or problem, FAQ, How to, Scripting, SDK

Views

409

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

How to dispatch a keyboard event?

Explorer ,
Jun 03, 2020

Copy link to clipboard

Copied

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

TOPICS
Error or problem, FAQ, How to, Scripting, SDK

Views

410

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jun 03, 2020 0
Explorer ,
Jun 04, 2020

Copy link to clipboard

Copied

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

Learn how to detect keyboard input using an event listener, and even detect whether the ctrl, alt, or shift keys are being pressed simultaneously! ----------...

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 04, 2020 0
Explorer ,
Jun 04, 2020

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 04, 2020 0
Explorer ,
Jun 05, 2020

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 05, 2020 0
Devin- LATEST
Explorer ,
Jun 05, 2020

Copy link to clipboard

Copied

According to documentation, the keyboard events have targets, like Script UI controls, not necessarily the system as a whole. What is mysterious is if it weren't possible, why are methods like initKeyboardEvent documented?

If Nefarious things were a concern, they wouldn't have made scripts have direct access to the file system, or make it possible to enable Chromium extensions with NodeJS, or to allow calls to system.callSystem()...

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 05, 2020 0