[Editbox] Please tell me how to allow dot.

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Tom, Kukurykus

lee ky
Posts: 22
Joined: Tue Mar 03, 2020 3:59 am

[Editbox] Please tell me how to allow dot.

Post by lee ky »

----------------------------------------------------------------
Please tell me how to allow dot.
--------------------------------------------------------------------

#target photoshop;



/**

* @fileoverview Shows how to create a key restricted custom 'edittext' with

* ScriptUI. This example permits only one comma `,` to be input and one or

* more of following characters: `[0-9]` `+` `-`

*/



$.level=0;



//------------------------------------------------------------------------------

// Usage

//------------------------------------------------------------------------------



var mainWindow = new Window("dialog");





// 1. Example `edittext` element with restricted key input.

var label = mainWindow.add('statictext', undefined, 'Restricted input:');

label.alignment = 'left';



var edittext = mainWindow.add('edittext', undefined, 0);

edittext.characters = 40;

restrictInputKeys(edittext); //<-- Enable restricted input.





// 2. Example `edittext` element with unrestricted key input.

var label2 = mainWindow.add('statictext', undefined, 'Unrestricted input:')

label2.alignment = 'left';



var edittext2 = mainWindow.add('edittext', undefined, 0);

edittext2.characters = 40;





mainWindow.show();



//------------------------------------------------------------------------------

// Helpers

//------------------------------------------------------------------------------



/**

* Determines whether an array includes a certain value among its elements.

* @param {String} valueToFind - The value to search for.

* @param {Array} arrayToSearch - The array to search in.

* @returns {Boolean} true if the value valueToFind is found within the array

*/

function inArray(valueToFind, arrayToSearch) {

for (var i = 0, max = arrayToSearch.length; i < max; i++) {

if (arrayToSearch === valueToFind) {

return true;

}

}

return false;

}



/**

* Restricts the character keys permitted in a `edittext` element.

* @param {Object} editTextInstance - Reference to `edittext` ScriptUI element.

*/

function restrictInputKeys(editTextInstance) {



if (editTextInstance.constructor.name !== 'EditText') {

throw new Error ('Invalid class. Expected `EditText` class.')

}



var hasComma = false;



var permissibleKeys = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9',

'Minus', 'Comma', 'Escape', 'Backspace'];

/*

* Add a listener to the given `edittext` element to detect `keydown` events.

* In the bddy of its handler function we detect each key pressed to determine

* whether the key is permissible or impermissible.

*/

editTextInstance.addEventListener('keydown', function (key) {

var keyName = key.keyName;

var shiftKeyIsDown = key.shiftKey;

var altKeyIsDown = key.altKey;



if (shiftKeyIsDown && keyName === 'Equal') {

return;

}



if ((shiftKeyIsDown || altKeyIsDown) && inArray(keyName, permissibleKeys)) {

key.preventDefault();

return;

}



if (! hasComma && keyName === 'Comma') {

hasComma = true;

return;

}



if (hasComma && keyName === 'Comma') {

key.preventDefault();

return;

}



if (! inArray(keyName, permissibleKeys)) {

key.preventDefault();

}

});



/*

* The `onChanging` event is utilized to detect whether a comma already exists.

* If a comma DOES NOT exist set the `hasComma` variable to `false`.

*/

editTextInstance.onChanging = function() {

if (this.text.indexOf(',') === -1) {

hasComma = false;

}

}

}
User avatar
Kukurykus
Posts: 528
Joined: Mon Jul 25, 2016 12:36 pm

Re: [Editbox] Please tell me how to allow dot.

Post by Kukurykus »

Probably somewhere instead (or beside) of 'Comma' you want to use 'Period'.
lee ky
Posts: 22
Joined: Tue Mar 03, 2020 3:59 am

Re: [Editbox] Please tell me how to allow dot.

Post by lee ky »

Thank you very much.
What's the number pad dot?
User avatar
Kukurykus
Posts: 528
Joined: Mon Jul 25, 2016 12:36 pm

Re: [Editbox] Please tell me how to allow dot.

Post by Kukurykus »

Decimal
lee ky
Posts: 22
Joined: Tue Mar 03, 2020 3:59 am

Re: [Editbox] Please tell me how to allow dot.

Post by lee ky »

Thank you.
Thank you very much.