Copy link to clipboard
Copied
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
1 Correct answer
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
...
Explore related tutorials & articles
Copy link to clipboard
Copied
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();
}
}
Copy link to clipboard
Copied
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":
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Thank you. That works.

