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

[JS] UI button: enter from keyboard

Community Beginner ,
Feb 06, 2020 Feb 06, 2020

I create a modal dialog with UI.
Inside some elements and two button (Cancel and Confirm).
It is possible to press Enter on the keyboard to execute the command on the button Confirm.... simulate pressing the button by Enter on the keyboard?

 

Any idea?

 

Thanks

TOPICS
Scripting
784
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 , Feb 06, 2020 Feb 06, 2020

it's possible, but only from certain elements as far as i know. For example, if you have an text input box, you can add an event listener that checks for the enter key being pressed. But I don't believe you can add that event listener to the dialog itself. 

 

so, assuming you're working with an editText or similar, you'll apply an event listener to the edittext directly, then check that the key pressed was "Enter", like so:

 

var w = new Window("dialog");
var inputField = w.add("edittext", undefined
...
Translate
Adobe
Community Expert ,
Feb 06, 2020 Feb 06, 2020

it's possible, but only from certain elements as far as i know. For example, if you have an text input box, you can add an event listener that checks for the enter key being pressed. But I don't believe you can add that event listener to the dialog itself. 

 

so, assuming you're working with an editText or similar, you'll apply an event listener to the edittext directly, then check that the key pressed was "Enter", like so:

 

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

//define the submit/validate function
function validateDialog()
{
	var valid = true;
	//do some validation
	if(valid)
	{
		alert("success");
		w.close();
	}
	else
	{
		alert("error. try again");
	}
}


//add the event listeners
cancelButton.onClick = function()
{
    w.close();
}

submitButton.onClick = function()
{
	validateDialog();
}

inputField.addEventListener("keydown",function(k)
{
    if(k.keyName === "Enter")
    {
        validateDialog();
    }
}
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
LEGEND ,
Aug 30, 2021 Aug 30, 2021

This script does not work for me. There is a syntax error in this section, but unable to fix it:

inputField.addEventListener("keydown",function(k)
{
    if(k.keyName === "Enter")
    {
        validateDialog();
    }
}

EDIT:
Able to validate the code and run the dialog with these changes. Still unable to get "Enter" to trigger "Submit":

var w = new Window("dialog");
var inputField = w.add("edittext", undefined, "Say Something...");
inputField.characters = 20;
var btnGroup = w.add("group");
var cancelButton = btnGroup.add("button", undefined, "Cancel");
var submitButton = btnGroup.add("button", undefined, "Submit");
w.show();
//define the submit/validate function
function validateDialog() {
var valid = true;
//do some validation
if (valid) {
alert("success");
w.close();
} else {
alert("error. try again");
}
}
//add the event listeners
cancelButton.onClick = function () {
w.close();
}
submitButton.onClick = function () {
validateDialog();
}
inputField.addEventListener("keydown", function (k) {
if(k.keyName === "Enter") {
validateDialog();
}
});
 
I'm trying to trigger a dialog button with a different key (p key). I learned the keyCode is 80 but don't know how to utilize it in a dialog:

if (event.keyCode === 80) {
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 ,
Aug 30, 2021 Aug 30, 2021

I suspect the issue is with the placement of w.show(); This statement shold be the last statement. The following code works for me

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

//define the submit/validate function
function validateDialog() {
    var valid = true;
    //do some validation
    if (valid) {
        alert("success");
        w.close();
    } else {
        alert("error. try again");
    }
}
//add the event listeners
cancelButton.onClick = function () {
    w.close();
}
submitButton.onClick = function () {
    validateDialog();
}
inputField.addEventListener("keydown", function (k) {
    if (k.keyName === "Enter") {
        validateDialog();
    }
})

w.show();

-Manan

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
LEGEND ,
Aug 31, 2021 Aug 31, 2021
LATEST

Thank you. That 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