Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Keyboard shortcut key down duration

Engaged ,
Nov 27, 2021 Nov 27, 2021

Let's say Photoshop has 10 opened documents and a one-line custom script with the command app.activeDocument.close()

 

When the script is triggered using a keyboard shortcut the active document is closed.

When the keyboard shortcut is pressed down continuously all 10 documents will close in a row.

 

What needs to take place so that only one document is closed at a time when the script runs?

Independently of the keyboard shortcut key down duration?

 

PS2022

OSX Catalina

 

 

 

TOPICS
Actions and scripting
894
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
People's Champ ,
Nov 27, 2021 Nov 27, 2021

Haven't tested it, but check it out.

app.activeDocument.close()
while (ScriptUI.environment.keyboardState.keyName) { app.refresh(); }
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Nov 27, 2021 Nov 27, 2021

Thanks r-bin. I try the snipet with the F2 key and still all open docs close when the F2 key is pressed for 1or 2 seconds.

app.activeDocument.close()
while (ScriptUI.environment.keyboardState.F2) { app.refresh(); }

Is ther a differnt way to identify the key name?  and would you handle a keybord short cut combination like option+command+F2?

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Nov 27, 2021 Nov 27, 2021

Why did you replace keyName with F2?

keyName is a property of the object (variable).

 

The code is ready, nothing needs to be changed. Suitable for any key.

 

P.S. ~(_8^(|)

 

~(_8^(|)

 

~(_8^(|)

 

~(_8^(|)
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Nov 27, 2021 Nov 27, 2021

The code still closes all the open docuemnts when the keyboard shortcut  is presssed for more thant a fraction of a second. I thougthat by changing the keyName I would fix that but it did not.

On a mac system with osx Catalina, the code closes all the open documents when the key ir presses down fro 1 sec.  

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Nov 28, 2021 Nov 28, 2021
quote

The code still closes all the open docuemnts when the keyboard shortcut  is presssed for more thant a fraction of a second. I thougthat by changing the keyName I would fix that but it did not.

On a mac system with osx Catalina, the code closes all the open documents when the key ir presses down fro 1 sec.  


By @Polycontrast

 

This is probably such a feature of the OS. I cannot reproduce. It works for me without any additional checks.
 
Idea.
The script must remember the current time into a variable via set\getCustomOptions. If the current call went through earlier than in 1-5 seconds, then do nothing.
 
P.S. No time to write code.
 
 
 
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Nov 28, 2021 Nov 28, 2021

Yesterday when I saw the topic, I wrote the following code. But your solution with reading the key codes seemed to me more suitable

if (!$.getenv('timer') || ((new Date()).getTime()-$.getenv('timer')) >5000) {app.activeDocument.close(); $.setenv('timer', (new Date()).getTime())}

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Nov 28, 2021 Nov 28, 2021
LATEST

Thank you Jazzy and r-bin for your help wit solving this keyboard shortcut issue.

The timer idea works well in a mac system. In the OS Catalina the script UI environemtns didn't work as expected but I get the impression that it works in the windows environment.

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Nov 28, 2021 Nov 28, 2021
quote

The code still closes all the open docuemnts when the keyboard shortcut  is presssed for more thant a fraction of a second. I thougthat by changing the keyName I would fix that but it did not.

On a mac system with osx Catalina, the code closes all the open documents when the key ir presses down fro 1 sec.  


By @Polycontrast

 

Also try this option. I wonder how it works for you.

if (ScriptUI.environment.keyboardState.keyName) app.activeDocument.close();
while (ScriptUI.environment.keyboardState.keyName) app.refresh();

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Nov 27, 2021 Nov 27, 2021

I am thinking a for loop can allow to exit the script after the active docuemnt is closed.

How can I modify this loop so that it loops in reverse from the total numeber of open docs to 0?

For example if 3 docs are open 

The loop returns

2

1

 

for(var i=0; i < app.documents.length; i++) {

alert(i)

}

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Nov 27, 2021 Nov 27, 2021

луп не поможет.

скрипт закроет все документы если его вызовут 100 раз из-за того, что клавиша запала.

 

P.S. нажимайте ctrl+w (или задайте свою комбинацию).

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines