سؤال

I have no idea how Processing knows that a user is pressing Ctrl and some character at the same time.

Multiple buttons at same time only. Is it possible?

ex: (Ctrl+r).

هل كانت مفيدة؟

المحلول

You have to first check if Ctrl has been pressed. If it has been pressed then you save a boolean as true. The next time you press a button you check if the button is the button you want (i.e. 'r') and if the boolean is true. If both are true then Processing knows...

Here's a demonstration:

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;
}

نصائح أخرى

I know this is a very old feed, but I have something that might help everyone with multiple key presses. This is for Processing's Python Mode, but I'm sure it can be implemented in some way for other modes.

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

And then using multiple if statements, you can check the dictionary for whether the key is pressed or not.

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

And so on. Hope this helps!

You could also override the keyPressed(KeyEvent) method and use the KeyEvent.isControlDown() method:

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

void draw(){
  //need draw() method for keyPressed() to work
}

you can find out the "key" codes of any combination of "ctrl + *" or "Shift + *" by typing, but this method is not suitable for multi-clicking to control the game. combination search code

void setup()
{
  textAlign(CENTER,CENTER);
}

void draw()
{
  background(0);
  text(int(key),50,50);
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top