首页 > 解决方案 > 在“edittext”框中输入期间是否可以锁定键盘中的某些键 - Photoshop脚本CS6?

问题描述

我只想启用键的键盘输入:+ - [0-9] ,并禁用框中的其余键输入edittext
此外,如果edittext已经有任何,字符,我想阻止任何进一步的键,输入。

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();

提前致谢。

标签: javascriptphotoshopextendscriptphotoshop-scriptadobe-scriptui

解决方案


考虑使用以下自定义ScriptUI示例。

它生成一个包含两个元素的简单dialog窗口。edittext

  1. 第一个edittext元素有限制输入。它只允许输入一个逗号,字符/键,以及一个或多个以下字符键:

    0 1 2 3 4 5 6 7 8 9 + -

  2. 第二个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事件主要用于检测是否,已经输入了逗号( )。如果每个事件触发时逗号存在,我们将变量设置为. 通过利用事件和变量,它为我们提供了一种机制来限制只输入一个逗号 ( ) 键。onChanginghasCommafalseonChanginghasComma,

用法:

  • 请注意,在文件的“使用”部分,example.jsx我们通过调用自定义函数启用受限键/字符输入,并传入对我们要限制restrictInputKeys的元素实例的引用。edittextIE

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

    如果您想在第二个edittext实例上启用受限键输入,您只需restrictInputKeys再次调用该函数并传入对第二个edittext实例的引用。例如:

    restrictInputKeys(edittext2); //<-- Enable restricted input for the second instance.
    

注意我测试了 PhotoShop CS5 中提供的示例,它运行成功。它尚未在 PhotoShop CS6 中进行测试 - 可能需要进行一些更改。



推荐阅读