I want to enable only keyboard input of keys: +
-
[0-9]
,
and disable rest of keys input in edittext
box.
Also if edittext
has already any ,
character, I want to block any further key ,
input.
var mainWindow = new Window("dialog");
var edittext = mainWindow.add("edittext", undefined, 0);
edittext.characters = 40;
edittext.onChanging = function() {
//enabling only input keys [0-9,-+] from keyboard
}
mainWindow.show();
Thanks in advance.
Consider utilizing the following custom ScriptUI example.
It produces a simple dialog
window that contains two edittext
elements.
The first edittext
element has restricted input. It permits one comma , character/key to be input, and one or more of the following character keys only:
0 1 2 3 4 5 6 7 8 9 + -
The second edittext
element has unrestricted input. It permits any character/key to be input, i.e. it's as per default functionality.
example.jsx
#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[i] === 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;
}
}
}
Explanation:
edittext
element to detect keydown
events.onChanging
event is utilized to essentially detect whether a comma (,
) has already been input or not. If a comma does not exists when each onChanging
event fires we set the hasComma
variable to false
. By utilizing the onChanging
event and the hasComma
variable it provides us with a mechanism for restricting the input of only one comma (,
) key.Usage:
Note in the "Usage" section of the example.jsx
file we enable restricted key/character input by invoking the custom restrictInputKeys
function and pass in a reference to the instance of the edittext
element that we want to restrict. i.e.
restrictInputKeys(edittext); //<-- Enable restricted input.
If you wanted to enable restricted key input on the second edittext
instance you just need to invoke the restrictInputKeys
function again and pass in a reference to the second edittext
instance. For example:
restrictInputKeys(edittext2); //<-- Enable restricted input for the second instance.
Note I tested the example provided in PhotoShop CS5 and it worked successfully. It hasn't been tested in PhotoShop CS6 - some changes may be necessary.