-1

我不知道处理如何知道用户正在同时按下Ctrl某个字符。

只能同时使用多个按钮。可能吗?

例如:( Ctrl+r)。

4

3 回答 3

2

您必须首先检查是否已按下 Ctrl。如果它已被按下,那么您将布尔值保存为真。下次按下按钮时,检查按钮是否是您想要的按钮(即“r”)以及布尔值是否为真。如果两者都是真的,那么处理知道......

这是一个演示:

boolean isCtrlPressed = false;
boolean isRPressed = false;
void draw() {
  background(0);
  fill(255);
  if (isCtrlPressed) background(255, 0, 0);
  if (isRPressed) background(0, 255, 0);
  if (isCtrlPressed && isRPressed) background(255, 255, 0);
}
void keyPressed() {
  if (keyCode == CONTROL && isCtrlPressed == false) isCtrlPressed = true;
  if (char(keyCode) == 'R') isRPressed = true;

}
void keyReleased() {
  if (keyCode == CONTROL) isCtrlPressed = false;
  if (char(keyCode) == 'R') isRPressed = false;
}
于 2014-01-27T13:04:04.617 回答
2

我知道这是一个非常古老的提要,但我有一些东西可以帮助每个人按多个按键。这适用于 Processing 的 Python 模式,但我确信它可以以某种方式用于其他模式。

import string

#string.printable is a pre-made string of all printable characters (you can make your own)
keys = {}
for c in string.printable:
    #set each key to False in the keys dictionary
    keys[c] = False

def keyPressed():
    #If key is pressed, set key in keys to True
    keys[key] = True

def keyReleased():
    #If key is released, set key in keys to False
    keys[key] = False

然后使用多个 if 语句,您可以检查字典是否按下了键。

if keys['w'] == True:
   #Do something
if keys['s'] == True:
   #Do something
if keys['a'] == True:
   #Do something
if keys['d'] == True:
   #Do something
if keys[' '] == True:
   #Do something

等等。希望这可以帮助!

于 2019-02-16T20:29:16.273 回答
0

您还可以覆盖 keyPressed(KeyEvent) 方法并使用 KeyEvent.isControlDown() 方法:

void keyPressed(KeyEvent ke) {
  println(ke.isControlDown());
}

void draw(){
  //need draw() method for keyPressed() to work
}
于 2014-01-27T15:21:24.113 回答