KeyPress eventListener fails

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Tom, Kukurykus

undavide

KeyPress eventListener fails

Post by undavide »

Hello, I'm wondering why the following script (which catches the Shift key-press: up/down keys add/subtract 1, Shift + up/down add/subtract +10):

Code: Select allvar w = new Window ("dialog");
var e1 = w.add ("edittext", undefined, "1");
var e2 = w.add ("edittext", undefined, "1"); e1.characters = e2.characters = 3; e1.active = true;
function handle_key (key, control) {
var step;
key.shiftKey ? step = 10 : step = 1; switch (key.keyName)
{
case "Up": control.text = String(Number(control.text)+step); break; case "Down": control.text = String(Number(control.text)-step);
}
} // handle_key
e1.addEventListener ("keydown", function (k) {handle_key (k, this);});
e2.addEventListener ("keydown", function (k) {handle_key (k, this);}); w.show();

Works in ESTK but fails in PS (CS6). Any idea?
Thanks in advance

Davide

Professional AI Audio Generation within Adobe Premiere Pro - Download Free Plugin here

Mike Hale

KeyPress eventListener fails

Post by Mike Hale »

Not sure about CS6 but mouse events are busted in Photoshop CS5.
undavide

KeyPress eventListener fails

Post by undavide »

Thanks Mike, do you happen to know if there are alternatives?

davide
Mike Hale

KeyPress eventListener fails

Post by Mike Hale »

No, I haven't found a way to get mouseEvents working in CS5 nor have I seen anyone post a workaround.

As far as I can tell the only mouse or keyboard handlers that still work in CS5( not sure about CS6 ) are the type like are used in some Adobe scripts. That is to say they just check the key and cancel the event handling if it's not a desired key. They don't interact with the control itself.
Mike Hale

KeyPress eventListener fails

Post by Mike Hale »

It's not a nice as what you were trying to do but this works in Photoshop CS5.

Add an onClick event handler and in that handler check the ScriptUI.environment.keyboardState to see if there is a meta key pressed. By using a combination of shift, control,and alt keys you can step the value in the text up or down with scaling.

Code: Select allvar w = new Window ("dialog");
var e1 = w.add ("edittext", undefined, "1");
var e2 = w.add ("edittext", undefined, "1");
e1.characters = e2.characters = 3;
e1.active = true;
e1.onClick = handle_click;
e2.onClick = handle_click;

function handle_click() {
   var step;
   ScriptUI.environment.keyboardState['shiftKey'] ? step = 10 : step = 1;
   if(ScriptUI.environment.keyboardState['altKey']) this.text = Number(this.text)+step;
   if(ScriptUI.environment.keyboardState['ctrlKey']) this.text = Number(this.text)-step;
}
w.show();
Mike Hale

KeyPress eventListener fails

Post by Mike Hale »

Or this that doesn't require a mouse click.
Code: Select allvar w = new Window ("dialog");
var e1 = w.add ("edittext", undefined, "1");
var e2 = w.add ("edittext", undefined, "1"); e1.characters = e2.characters = 3; e1.active = true;
function handle_key (event) {
   var step;
   ScriptUI.environment.keyboardState['shiftKey'] ? step = 10 : step = 1;
   if(event.keyName == 'Minus'){
      this.text = Number(this.text)-step;
      event.preventDefault();
   }
   if(event.keyName == 'Equal'){
      this.text = Number(this.text)+step;
      event.preventDefault();
   }
}
e1.addEventListener ("keydown", handle_key );
e2.addEventListener ("keydown",  handle_key );
   
w.show();

It seems that Photoshop is trapping the arrow keys so you need to use different keys( or work out what Photoshop is doing and disable that for the control )
undavide

KeyPress eventListener fails

Post by undavide »

Thanks for the code, Mike!
The more I dig into ScriptUI, the more bugged it looks to my eyes (I've stopped to complain about Flex, which in comparison is heaven).
Kind regards
Davide
Mike Hale

KeyPress eventListener fails

Post by Mike Hale »

Yes, it seems that everything to do with scripting is buggy. It's funny that you should feel that way about Flex, I stopped developing Flex/Flash panels because is it's bugs.
undavide

KeyPress eventListener fails

Post by undavide »

Mike Hale wrote:I stopped developing Flex/Flash panels because is it's bugs.

Actually, in the middle of the project I'm working on (a ScriptUI window hosting a Flex interface) I've moved to full ScriptUI just because of Flex 3.6 bugs with components - which didn't occur on Flex 4.5.
Yet you're right too - CS SDK (at least the part involved with PS) could be more robust. I'm going to let my wife (a former IBM AS400 programmer) read your comment - she always criticize me when I complain ("it's not possible that the full environment is buggy!")
Cheers

Davide
Mike Hale

KeyPress eventListener fails

Post by Mike Hale »

Have her read the thread on my links panel for all the bug reports. And that was after I spent many hours working around bugs while developing it. The frustrating part is they are not bugs in my code - they are in the framework.

Too bad really, I can see the appeal of a non-modal user interface. I can only hope that at some point users panels will be more robust.