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

Javascript UI Button onClick with ALT-key pressed

Engaged ,
Feb 13, 2022 Feb 13, 2022

Hello all,
I would like to detect in a user interface when a button is pressed whether the Alt key was pressed at the same time. So onClick or onClick+Alt.
In an input field you can detect keys with addEventListener("keydown"...).
But how do I do that with a button?

Here is my test javascript which does not work for the "OK" button.

 

var vXX = ""; // for protocol
var w = new Window("dialog");
var inputField = w.add("edittext", undefined, ""); inputField.characters = 20;
var btnGroup = w.add("group");
var cancelButton = btnGroup.add("button", undefined, "Cancel");
var submitButton = btnGroup.add("button", undefined, "Submit");

cancelButton.onClick = function () { vXX += "\n" + "Cancle"; w.close(); }
submitButton.onClick = function () {
  submitButton.addEventListener("keydown", function (k) {
    if (k.keyName === "Alt") { vXX += "\n" + "onClick OK ALT"; } // don't work
  } )
  vXX += "\n" + "OK"; w.close();
}
inputField.addEventListener("keydown", function (k) {
  if (k.keyName === "Alt") { vXX += "\n" + "Input ALT"; } // it works
} )
submitButton.addEventListener("keydown", function (k) {
  if (k.keyName === "Alt") { vXX += "\n" + "OK ALT"; } // don't work
} )

w.show();
alert(vXX);

 

Looking forward to your answers
- j.

TOPICS
Scripting
1.6K
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

Guide , Feb 13, 2022 Feb 13, 2022

 

var w1 = new Window("dialog");
    var button1 = w1.add("button", undefined, "text");
    var doSomething = function (k) {
        if (k.altKey == true) {
            alert("Mouse clicked and Alt pressed.");
            w1.close();
        }
    }
    button1.addEventListener("click", doSomething);
w1.show();

 

Translate
Adobe
Community Expert ,
Feb 13, 2022 Feb 13, 2022
var altKeyPressed = k.altKey

It's only "k" because your event is passed in the k argument. It's more common to use "ev" or "e", but doesn't matter.

- Mark 

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 ,
Feb 13, 2022 Feb 13, 2022

Hi Mark,

thanks for the quick answer. But I don't understand how to use your tip.

I've change my script. The problem is still there. The submitButton.addEventListener("keydown", function (k) {...} is not triggered with a "keydown". May be that's the problem?

 

// Version 2
var vXX = ""; // for protocol
var w = new Window("dialog");
var inputField = w.add("edittext", undefined, ""); inputField.characters = 20;
var btnGroup = w.add("group");
var cancelButton = btnGroup.add("button", undefined, "Cancel");
var submitButton = btnGroup.add("button", undefined, "Submit");

cancelButton.onClick = function () { vXX += "\n" + "Cancle"; w.close(); }
submitButton.onClick = function () {
  submitButton.addEventListener("keydown", function (k) {
    if (k.keyName === "Alt") { vXX += "\n" + "onClick OK ALT"; } // don't work
  } )
  vXX += "\n" + "OK"; w.close();
}
inputField.addEventListener("keydown", function (k) {
var altKeyPressed = k.altKey;
  if (altKeyPressed) { vXX += "\n" + altKeyPressed + " " + k.keyName + " " + "Input ALT"; } // it works
} )
submitButton.addEventListener("keydown", function (k) {
  var altKeyPressed = k.altKey;
  vXX += "\n" + k.altKey + " " + k.keyName + " " + "in OK-button";
  if (altKeyPressed) { vXX += "\n" + k.altKey + " " + k.keyName + " " + "OK ALT"; } // don't work
} )

w.show();
alert(vXX);
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
Guide ,
Feb 13, 2022 Feb 13, 2022

 

var w1 = new Window("dialog");
    var button1 = w1.add("button", undefined, "text");
    var doSomething = function (k) {
        if (k.altKey == true) {
            alert("Mouse clicked and Alt pressed.");
            w1.close();
        }
    }
    button1.addEventListener("click", doSomething);
w1.show();

 

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 ,
Feb 13, 2022 Feb 13, 2022

Thak you very much, that works. 🙂

Have a good sunday.

– j.

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
Community Expert ,
Feb 13, 2022 Feb 13, 2022

Thanks @femkeblanco. 🙂  I didn't notice Parts4Arts had bound to the "keyDown" event and not the "click" event.

@Parts4Arts the problem is that I don't think buttons trigger the keyDown even.

- Mark

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
Community Expert ,
Feb 13, 2022 Feb 13, 2022

here's another version without eventListeners

 

var w1 = new Window("dialog");
var button1 = w1.add("button", undefined, "text");

button1.onClick = function() {
    if ( ScriptUI.environment.keyboardState.altKey ) {
        alert("Mouse clicked and Alt pressed.");
        w1.close();
    }
}

w1.show();
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
Participant ,
Jun 23, 2022 Jun 23, 2022

@CarlosCanto @femkeblanco Thank you.

This is perfect and works fine.

However, JSXBIN scripts don't work.


EXAMPLE:

var w1 = new Window("dialog");
var button1 = w1.add("button", undefined, "text");

button1.onClick = function() {
    if ( ScriptUI.environment.keyboardState.altKey ) {
@JSXBIN@ES@2.0@MyBbyBn0ABJAnAEjzFjBjMjFjSjUBfRBFegeiNjPjVjTjFhAjDjMjJjDjLjFjEhAj
BjOjEhAiBjMjUhAjQjSjFjTjTjFjEhOff0DzACByB
        w1.close();
    }
}

w1.show();

 Is there a solution?

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
Guide ,
Jun 23, 2022 Jun 23, 2022

Sorry, I have never used JSXBIN. 

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
Community Expert ,
Jun 23, 2022 Jun 23, 2022

I'm not very knowledgeable on jsxbin but I think you have to eval, eg.

eval('@JSXBIN@ES@2.0@MyBbyBn0ABJAnAEjzFjBjMjFjSjUBfRBFegeiNjPjVjTjFhAjDjMjJjDjLjFjEhAjBjOjEhAiBjMjUhAjQjSjFjTjTjFjEhOff0DzACByB');

-Mark

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
Participant ,
Jun 24, 2022 Jun 24, 2022
LATEST

Thank you @m1b . It works.

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