Skip to main content
Participating Frequently
April 21, 2021
Question

Photoshop Event Manager: Listen for key pressed to execute script

  • April 21, 2021
  • 2 replies
  • 708 views

I am trying to write a script that lets me switch between the lasso tool and eyedropper by hitting the SHIFT key. This is my try so far (code taken from Adobe forums and modified, I myself have minimal programming experience):

 

#target photoshop
if(ScriptUI.environment.keyboardState['shiftKey']){
if (getTool() == 'lassoTool') {
    setTool('eyedropperTool');
    alert("Your message here...")

} else if (getTool() == 'eyedropperTool') {
    setTool('lassoTool');
}
}

function getTool(){  
    var ref = new ActionReference();   
    ref.putEnumerated( charIDToTypeID("capp"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );   
    var cTool = typeIDToStringID(executeActionGet(ref).getEnumerationType(stringIDToTypeID('tool')));  
    return cTool;  
}

function setTool(tool) {
    var desc9 = new ActionDescriptor();
    var ref7 = new ActionReference();
    ref7.putClass( app.stringIDToTypeID(tool) );
    desc9.putReference( app.charIDToTypeID('null'), ref7 );
    executeAction( app.charIDToTypeID('slct'), desc9, DialogModes.NO );
}

 

I have added this script to the Photoshop Event Manager. I have set the Photoshop Event that triggers the script to "everything" in the dropdown menu. Unfortunately, the script only executes when I do a selection while pressing SHIFT. Instead, I would like the script to run every time I press shift. Is that possible?

 

Thanks!

This topic has been closed for replies.

2 replies

Kukurykus
Legend
April 21, 2021

An event manager isn't needed to detect Held shift. Just run the script and hit Escape when you want to quit it:

function tool(v) {
	if (!v) return; (ref = new ActionReference()).putClass(sTT(v + 'Tool'))
	dsc.putReference(sTT('null'), ref), executeAction(sTT('select'), dsc)
}

sTT = stringIDToTypeID, dsc = new ActionDescriptor(), env = ScriptUI.environment
cTs = {lassoTool: 'eyedropper', eyedropperTool: 'lasso'}; sleep = true; while(sleep) {
	try{refresh()}catch(err){sleep = false} if (env.keyboardState.shiftKey) tool(cTs[currentTool])
}

 

jaschagAuthor
Participating Frequently
April 21, 2021

I really like that approach!

Could I just nest the code I posted in the question in a while-Loop that exits once escape has been pressed? Like this:

while(!ESC) {
    if(ScriptUI.environment.keyboardState['shiftKey'] && ScriptUI.environment.keyboardState['q']){
        if (getTool() == 'lassoTool') {
            setTool('eyedropperTool');
    //alert("Your message here...")

    } else if (getTool() == 'eyedropperTool') {
    setTool('lassoTool');
    }
}

I am not sure how to check if the escape key has been pressed. Could you tell me? (I know it's in your script but I am not able to identify that part of the code).

Kukurykus
Legend
April 21, 2021

ESC is not included in script. It's Photoshop break functionality to the script that's looping.

 

Instead of shiftKey you can use keyName that should detect other keys. Check it yourself...

JJMack
Community Expert
Community Expert
April 21, 2021

You concept of changing Photoshop current active tool every time shift is press would render Photoshop  unusable shift is user for some many reasons. Switching always to the eyedropper or lasso with each shift press would change Photoshop operation.

JJMack