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

ScriptUI: Improvement of the problem that the event was executed twice

Community Expert ,
Oct 15, 2025 Oct 15, 2025

The following is a simplified version of the ScriptUI code I am working on.

var win = new Window("dialog{\
e:EditText{characters: 4, text:'0'}}");

win.e.addEventListener ("keydown", function(e){
    if(e.keyName == "Up"){
        var num = this.text - 0;
        this.text = ++num;
    }
});

win.show();

EditText aims to allow you to enter text freely as usual, and if the text is a number, press the UP ARROW on the keyboard to add 1.

However, when I execute this code, 2 will be added when I press theUP ARROW.

 

What I tried.

Insert “e.stopPropagation();”  ...There was no change in the result.

Insert “e.preventDefault();”  ...The point where 2 is added at a time has improved, but text can no longer be entered.

Insert “$.writeln(e.timeStamp); $.sleep(1000);” ...A timestamp that was offset by 1 second was output.

Change the event to “keyup” ...The event handler is now executed 3 times (I'm laughing).

 

Is there a good solution?

Regards.

TOPICS
ExtendScript
72
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

correct answers 1 Correct answer

Community Expert , Oct 15, 2025 Oct 15, 2025

Sorry, I asked ChatGPT. The cause is vague, but it's solved..

// Create a local variable for a timestamp
var lastTime = 0;

var win = new Window("dialog{\
e:EditText{characters: 4, text:'0'}}");

win.e.addEventListener ("keydown", function(e){
    var now = new Date().getTime();
    if (now - lastTime < 50) return; // 50ms
    lastTime = now;
    if(e.keyName == "Up"){
        var num = this.text - 0;
        this.text = ++num;
    }
});

win.show();

 Thank you, Mr. Code, who was used as an AI s

...
Translate
Community Expert ,
Oct 15, 2025 Oct 15, 2025
LATEST

Sorry, I asked ChatGPT. The cause is vague, but it's solved..

// Create a local variable for a timestamp
var lastTime = 0;

var win = new Window("dialog{\
e:EditText{characters: 4, text:'0'}}");

win.e.addEventListener ("keydown", function(e){
    var now = new Date().getTime();
    if (now - lastTime < 50) return; // 50ms
    lastTime = now;
    if(e.keyName == "Up"){
        var num = this.text - 0;
        this.text = ++num;
    }
});

win.show();

 Thank you, Mr. Code, who was used as an AI source.

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