【发布时间】: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