title: Photoshop Scripting: How to add an event listener to the ScriptUI windowObj
body: <p>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 &quot;keydown&quot; to &quot;click&quot;.</p>
  <p>The way I want it to work:
  When the ScriptUI window displays, on keydown, a function is called.</p>
  <p><strong>Below is code of a simplified example of what I want to happen:</strong></p>
  <pre><code>#target Photoshop
  
  var w = new Window (&quot;dialog&quot;);
  var buttonPositions = w.add (&quot;group&quot;);
  var top = buttonPositions.add (&quot;button&quot;, undefined, &quot;Button&quot;);
  
  w.addEventListener (&quot;keydown&quot;, function (k) {handle(k)});
  
  function handle (k) {
      alert (k.keyName);
  }
  
  w.show ();
  </code></pre>
  <p><strong>Displays when script runs</strong></p>
  <p><img src="https://i.stack.imgur.com/z62NQ.png" alt="Displays when script is run" /></p>
  <p><strong>Alert box with key name displays on keydown</strong>
  <img src="https://i.stack.imgur.com/pgBx5.png" alt="Alert box with key name displays on keydown" /></p>