【问题标题】:Keyboard input is displaying twice in Processing键盘输入在处理中显示两次
【发布时间】:2015-06-02 11:20:47
【问题描述】:

我正在整理一个处理草图,其中用户通过键盘输入文本,文本被保存到文本文件并打印,然后被处理并以 PNG 格式保存到 Tumblr 帐户。我有一个 keyPressed 事件初始化保存为文本文件等。当用户按下 CONTROL 键时,它会做它的事情并循环到下一个屏幕。

我想将其更改为使用 RETURN 或 ENTER 键,因为我认为它会更直观。由于 RETURN 不是编码键,所以我拿出部分来检查按下的键是否编码,从那时起我键入的每个键都会显示两次,但 RETURN 可以保存文本文件和前进等。

如果我离开检查键是否已编码。文本显示正常,但按 RETURN 键不会初始化保存为文本文件等。这可能非常简单,我已经看了太久了 - 但我真的看不出问题出在哪里。任何帮助将不胜感激。谢谢。

以下相关代码:

    void keyPressed() {
      if (keyCode == BACKSPACE) {
        if (yourText.length() > 0) {
          yourText = yourText.substring(0, yourText.length()-1);
        }
          } else if (keyCode == DELETE) {
            yourText = "";
          } else if (keyCode != SHIFT && keyCode != CONTROL && keyCode != RETURN && keyCode != ENTER && keyCode != ALT) {
            myText = "";
            yourText = yourText + key;
          }

      // If the Return key is pressed save the String and write it to text file
        //if (key == CODED) 
        //{
        if (key == RETURN || key == ENTER)  {
          savedText = yourText;
          textFile = createWriter("stories/"+timestamp()+".txt");
          textFile.println(savedText);
          textFile.flush();
          textFile.close();
          rect (0,0,width, height); //PROBLEM sometimes visible when screen is switched.
          noStroke ();
          currentScreen++;
      if (currentScreen > 2) { currentScreen = 0; } //switches to next screen

      } 

     else {
          // Otherwise, concatenate the String
          // Each character typed by the user is added to the end of the String variable.
          yourText = yourText + key;
        }
      //}
    }

【问题讨论】:

    标签: keyboard processing keypress


    【解决方案1】:

    对您的问题的简短回答是您应该使用key 变量来检查BACKSPACETABENTERRETURNESCDELETE,而不是@ 987654330@ 变量就像你正在做的那样。

    要获得更长的解释,这里是您的程序的简化 MCVE

    void draw() {
      background(0);
    }
    
    void keyPressed() {
      if (keyCode == BACKSPACE) {
        println("1: backspace");
      } else if (keyCode == DELETE) {
        println("2: delete");
      } else if (keyCode != SHIFT && keyCode != CONTROL && keyCode != RETURN && keyCode != ENTER && keyCode != ALT) {
        println("3: " + key);
      }
    
      if (key == RETURN || key == ENTER) {
        println("4: return");
      } else {
        println("5: " + key);
      }
    }
    

    运行它,你会看到一个类似的问题,你会明白为什么会这样:你为大多数键输入了两个单独的条件,因为你确定不同键的逻辑是错误的。

    你的逻辑的前半部分是这样的:

    Is the keyCode BACKSPACE? If so do something.
    Otherwise, is the keyCode DELETE? If so do something.
    Otherwise, is the keyCode ANYTHING ELSE other than SHIFT, CTRL, RETURN, ENTER, or ALT? If so do something.
    

    问题是,任何未编码的键都会进入第三个 else/if 语句。

    然后你的逻辑的后半部分打印出 key 变量的值,即使你已经在上面的 if 语句中处理了它。

    解决方法是正确区分编码和未编码的键,然后使用key变量检查ENTERRETURNBACKSPACE等值:

    void draw() {
      background(0);
    }
    
    void keyPressed() {
    
      if (key==CODED) {
        if (keyCode == SHIFT){println("shift");}
        else if(keyCode == CONTROL){println("ctrl");}
        else if(keyCode == ALT) {println("alt");}
      } else {
        if (key == BACKSPACE) {
          println("1: backspace");
        } else if (key == DELETE) {
          println("2: delete");
        } else if (key == RETURN || key == ENTER) {
          println("4: return");
        } else {
          println("5: " + key);
        }
      }
    }
    

    如需更多信息,请随时查看the reference

    【讨论】:

    • 感谢@Kevin Workman。你是绝对正确的。另外,我有'yourText = yourText + key;'两次 - 一旦没有对密钥进行编码,就不需要最后的 else 语句。
    猜你喜欢
    • 2020-10-26
    • 2012-07-12
    • 1970-01-01
    • 2010-09-23
    • 1970-01-01
    • 2010-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多