I want to add an event listener to the windowObj that on keydown, calls a function. I can not get this to work on the window object; however, I can get it working after a child of the window object (a button for example), has been clicked. I've also tried clicking on the window area around the button, thinking that maybe the window needed to be active, but this did not work. Oddly enough, this test worked when I changed "keydown" to "click".
The way I want it to work: When the ScriptUI window displays, on keydown, a function is called.
Below is code of a simplified example of what I want to happen:
#target Photoshop
var w = new Window ("dialog");
var buttonPositions = w.add ("group");
var top = buttonPositions.add ("button", undefined, "Button");
w.addEventListener ("keydown", function (k) {handle(k)});
function handle (k) {
alert (k.keyName);
}
w.show ();
Displays when script runs
Alert box with key name displays on keydown
tl;dr: Set the
active
property of any control that accepts keystrokes and is a descendent in the registered element's hierarchy totrue
:The
Window
object isn't designed to detect keydown events. This can be demonstrated by interminglingpanel
,statictext
, andgroup
elements with controls such asradiobutton
,button
, andcheckbox
. Pressing the tab key skips any elements that ignore keydown events, and sets the focus to the first control in line that accepts keydown events. The first control residing in the listener's hierarchy that receives focus will trigger your callback on the next keypress.Per the Photoshop Scripting Reference (emphasis mine):
Keydown events can propagate through a
Window
(orPanel
, orGroup
element, for that matter) as part of the event registration and capture phase, but to trigger a keydown event, the actual target needs to accept that type of event.More info at: event callbacks/listeners and control objects.