Skip to main content
Participant
July 19, 2019
Question

Invoke function through Key Press Event

  • July 19, 2019
  • 1 reply
  • 1861 views

In Photoshop, it is possible to assign up to about 44 actions and 34 scripts to shortcuts. Once they have shortcuts, they can be acessed through AutoHotkey easily, but those without shortcuts cannot. To work around this, I have been trying to produce JavaScripts that would stay open permanently, with specific functions being invoked upon keypress events. It seems it could be done with eventListener, but I found nothing on how to implement it in Photoshop successfully. Here is a general format:

(...).addEventListener("keydown", function(event) {
  
if (event.keyCode == 65) {
  app.doAction("actionName", "actionGroup");
;
  
}
});

This topic has been closed for replies.

1 reply

JJMack
Community Expert
Community Expert
July 20, 2019

I do know Javascript or Photoshop scripting well I only hack at it and modify script code the others write.  I steal(reuse) their code to do what I want to automat in Photoshop.  I create a script Paste Image Roll.  The dialog has  input fields for numeric data.  I stole some code from A script I believe Michel Hale posted here years ago the  install and event listener for keyboard input to onle allowed  0 through 9 and I think "."  and some special keys. http://www.mouseprints.net/old/dpr/PasteImageRoll.jsx

The listener event is added in the scripts Dialog and  there is code that handles the keydown events. In these dialog fields Invalid input is rejected with a beep.

Re: Script Request??

To stay open I believe you would need to make it an Photoshop extension.   I have more or less done that without creating a extension.  I created a Pallet Window via ScriptUI and launch the script via the Photoshop extension JSX Launcher.  The script  run seems to stay open because it was launch from a Photoshop extension And Photoshop UI is available for use.

var p = new Window( "palette" ,"JJMack PS Palette");

p.btn = p.add( "button", undefined,'Actual' );

p.btb = p.add( "button", undefined,'Zoom out' );

p.btc = p.add( "button", undefined,'Zoom in' );

p.cls = p.add( "button", undefined, "Close Palette");

customEventManager = {

  events: {},

  addListener: function( evt, handler, context ) {

    if ( !this.events[ evt ] ) {

      this.events[ evt ] = [];

    }

    this.events[ evt ].push( { "handler": handler, "context": context } );

  },

  trigger: function( evt ) {

    if ( this.events[ evt ] ) {

      for ( var i = 0; i < this.events[ evt ].length; i++ ) {

        this.events[ evt ][ i ].handler.call( this.events[ evt ][ i ].context, evt );

      }

    }

  }

}

p.btn.onClick = function() {

  p.evt.trigger( "Actual" );

}

p.btb.onClick = function() {

   p.evt.trigger( "Out" );

}

p.btc.onClick = function() {

   p.evt.trigger( "In" );

}

p.cls.onClick = function(){

   p.close();

};

aHandler = function( evt) {

if (documents.length!=0) {runMenuItem(app.charIDToTypeID("ActP"));}

}

bHandler = function( evt) {

if (documents.length!=0) {runMenuItem(app.charIDToTypeID("ZmOt"));}

}

cHandler = function( evt) {

if (documents.length!=0) {runMenuItem(app.charIDToTypeID("ZmIn"));}

//alert( evt );

}

p.evt = customEventManager;

p.evt.addListener( "Actual", aHandler );

p.evt.addListener( "Out", bHandler );

p.evt.addListener( "In", cHandler );

 

p.onShow = function(){ 

  var ww = p.bounds.width; 

  var hh = p.bounds.height; 

  PaletteDisplay = 0;                                         // Photoshop's First Display

  //PaletteDisplay = $.screens.length-1;                        // Photoshop's Last Display

  PaletteTop   = $.screens[PaletteDisplay].top + 100;         // Relative to Top of display

  PaletteLeft  = $.screens[PaletteDisplay].right - ww - 46 ;  // Relative to Right of side not Left side of display

  p.bounds.x  = PaletteLeft; 

  p.bounds.y  = PaletteTop; 

  p.bounds.width  = ww; 

  p.bounds.height  = hh; 

}  

//alert($.screens)

p.show();

JJMack