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.
2
Answers
I’m not too sure about the rest, but to make it so that only the numbers can be pressed, you can change your code to something like this:
After that it would be a matter of simply finding the right combination of if statements for the rest of the keys I would assume. You can find a list of key codes and their corresponding keys by searching ‘JavaScript key codes’ on Google.
Hope this helps!
Consider utilizing the following custom ScriptUI example.
It produces a simple
dialog
window that contains twoedittext
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
Explanation:
edittext
element to detectkeydown
events.onChanging
event is utilized to essentially detect whether a comma (,
) has already been input or not. If a comma does not exists when eachonChanging
event fires we set thehasComma
variable tofalse
. By utilizing theonChanging
event and thehasComma
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 customrestrictInputKeys
function and pass in a reference to the instance of theedittext
element that we want to restrict. i.e.If you wanted to enable restricted key input on the second
edittext
instance you just need to invoke therestrictInputKeys
function again and pass in a reference to the secondedittext
instance. For example: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.