Creating a KeyboardEvent and sending it to a ScriptUI Control Object with dispatchEvent()
Copy link to clipboard
Copied
So, I looked all over the internet for an example of how to send a KeyboardEvent from within ScriptUI to another control object and didn't really find a solid answer. There are plenty mentions of the technique but no actual code, at least I couldn't find it. I was finally able to sort it out today so I am sharing it here in case anyone else needs to accomplish something similar.
// create an event
var kbEvent = ScriptUI.events.createEvent("KeyboardEvent");
// initialize the event using the format described in the docs
kbEvent.initKeyboardEvent("keydown", true, true, listBox, kd.keyName, 0, "");
// send the event to another control object with the `dispatchEvent()` method
SomeScriptUIControlObject.dispatchEvent(kbEvent);
As you can, it is pretty straight forward, you just need to initialize the event with your own values using the format below.
eventObj.initKeyboardEvent (eventName, bubble, isCancelable, view, keyID, keyLocation, modifiersList)
To help figure it out, I captured the details of some key events. Here are the attributes from pressing the "Down" key for reference.
ExtendScript Keydown Event
keyName: Down
keyIdentifier: Down
keyLocation: 0
ctrlKey: false
altKey: false
shiftKey: false
metaKey: false
DOM_KEY_LOCATION_STANDARD: 0
DOM_KEY_LOCATION_LEFT: 1
DOM_KEY_LOCATION_RIGHT: 2
DOM_KEY_LOCATION_NUMPAD: 3
view: [control object]
detail: 1
captures: true
bubbles: true
cancelable: true
currentTarget: [control object]
eventPhase: target
target: [control object]
timeStamp: Thu Nov 16 2023 12:30:29 GMT-0500
type: keydown
NOT_DISPATCHING: 0
CAPTURING_PHASE: 1
AT_TARGET: 1
BUBBLING_PHASE: 3
Reference Material:
- KeyboardEvent: https://extendscript.docsforadobe.dev/user-interface-tools/event-handling.html?highlight=keyboardeve...
- initKeyboardEvent(): https://extendscript.docsforadobe.dev/user-interface-tools/event-handling.html?highlight=initKeyboar...
- ControlObject.dispatchEvent(): https://extendscript.docsforadobe.dev/user-interface-tools/control-objects.html?highlight=dispatchEv...
Cheers ✌️
Explore related tutorials & articles
Copy link to clipboard
Copied
This is great info @jduncan. I'll try this out when I get the chance. Thanks for putting in the time to work it out!
- Mark

