How can I listen to key press events in photoshop?

I’m trying to write a plugin that changes & keeps track of the brush size. I can get
‘set’ events when the brush size is changed, however no event is fired when I change the brush size with the bracket keys [ and ]. The only thing I can think of doing for this is to listen for those key presses, however I don’t understand what type of event I should be looking for, or frankly what function to use.

When I run this snippet:

const eventHandler = (event, descriptor) => console.log(event, descriptor)
require("photoshop").action.addNotificationListener(['all'], eventHandler);

There is no event for keypresses, and no event for the brush size changing as a result of pressing the bracket keys.

Thanks!

Perhaps I’m on the wrong track—is there something akin to MutationObserver - Web APIs | MDN in UXP where I could watch the properties of a given tool?

Mutation observer probably won’t help you, because it observes changes in the HTML DOM.

Keypresses are not Photoshop events, so you won’t be able to see them with Ps listener. They can be listened to with keydown JS event, but I very much doubt you will be able to intercept the host event (if it even produces one) in your panel. I believe, you could catch this event only if your panel is in focus :thinking:

Just contemplating here - didn’t test myself

Thanks for your reply! So sounds like my only way to reliably keep track of this is to check the value on an interval…is that right?

afaik there’s no support for global key events. it is planned as shortcut keys that can be defined in the manifest and assigned an action.

JS key events may work but need testing and if they do work they work when your panel have focus only

That’s probably for the best. I don’t really want to track keypresses anyway as that would be an unreliable way to track brush size, since someone could reassign the keybinding. I’m more so confused why there is no event fired when the brush size is changed via pressing the bracket keys and was hoping that tracking the key press would be a workaround. Is performing a simple get for brush parameters every, say, 1/4 to 1/2 a second going to cause noticeable performance issues in PS?

can’t tell the impact without testing.
but logically this solution seems like an overkill for such a simple task.
one plugin doing a non-blocking pull can be accepted. but as a practice this is very undesirable and can lead to a slower user experience.

I’d steer away from such solution if I can