考虑使用以下自定义ScriptUI示例。
它生成一个包含两个元素的简单dialog
窗口。edittext
第一个edittext
元素有限制输入。它只允许输入一个逗号,字符/键,以及一个或多个以下字符键:
0 1 2 3 4 5 6 7 8 9 + -
第二个edittext
元素具有不受限制的输入。它允许输入任何字符/键,即按照默认功能。
例子.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;
}
}
}
解释:
- 这实质上为给定元素添加了一个事件侦听
edittext
器以检测keydown
事件。
- 在事件侦听器处理函数的主体中,我们检测每个按下的键并确定该键是允许的还是不允许的。
- 该
onChanging
事件主要用于检测是否,
已经输入了逗号( )。如果每个事件触发时逗号不存在,我们将变量设置为. 通过利用事件和变量,它为我们提供了一种机制来限制只输入一个逗号 ( ) 键。onChanging
hasComma
false
onChanging
hasComma
,
用法:
请注意,在文件的“使用”部分,example.jsx
我们通过调用自定义函数启用受限键/字符输入,并传入对我们要限制restrictInputKeys
的元素实例的引用。edittext
IE
restrictInputKeys(edittext); //<-- Enable restricted input.
如果您想在第二个edittext
实例上启用受限键输入,您只需restrictInputKeys
再次调用该函数并传入对第二个edittext
实例的引用。例如:
restrictInputKeys(edittext2); //<-- Enable restricted input for the second instance.
注意我测试了 PhotoShop CS5 中提供的示例,它运行成功。它尚未在 PhotoShop CS6 中进行测试 - 可能需要进行一些更改。