Check key for Pos1, End, Ctrl-1, Ctrl-Pos1, Ctrl-End

Hello all,

how can I check the aforementioned keys please?

Cheers!

Chrisir

I assume you mean keys presses on a keyboard but I don’t recognise them. :smile:

1 Like

my bad, I changed it

  • Pos1 jumps to start of line
  • End to its end
  • others are control related

Try testing the keyCode value like this

void setup() {
}

void draw() {
  background(230);
}

void keyPressed() {
  println(keyCode);
}

I got

36 Start of line
35 End of line
33 Start of document / section
34 End of document / section

17 for Ctrl key
1 Like

Thanks a ton, mate.

I receive 33 and 34 for Pg up and Pg down, Win 10 here.

But what about Ctrl-1, Ctrl-Pos1, Ctrl-End?

this seems to work with ctrl but it looks over-complicate.

Chrisir

final int keyCodeCtrl=17; 

String text1 = "";
boolean ctrlIsDown=false;

// -----------------------------------------------------------------

void setup() {
  size(422, 700);
}

void draw() {
  background(230);

  fill(0); 
  text(text1, 20, 20);
}

// -----------------------------------------------------------------

void keyPressed() {
  text1 = ""; 

  if (key==CODED) {
    // Coded
    checkCodedKeys();
  } else {
    // NOT Coded
    checkNotCodedKeys();
  } // NOT Coded
  // 
  println(keyCode);
} // func 

void keyReleased() {
  if (keyCode==keyCodeCtrl) {
    ctrlIsDown=false;
  }
} // func 

// -----------------------------------------------------------------

void checkNotCodedKeys() {
  // NOT Coded
  if (ctrlIsDown) {
    // with control
    switch(key) {
    case '1':
      text1="Ctrl-1";
      break;
    }
    //
  } else {
    // without control
    switch(key) {
    case '1':
      text1="1";
      break;
    }
  }//else
}//func 

void checkCodedKeys() {
  // Coded

  if (ctrlIsDown) {
    // with control
    if (keyCode==36) {
      text1 = "Ctrl-Pos1";
    } else if (keyCode==35) {
      text1 = "Ctrl-End";
    } else if (keyCode==33) {
      text1 = "Ctrl-Pg Up";
    } else if (keyCode==34) {
      text1 = "Ctrl-Pg Down";
    } else if (keyCode==keyCodeCtrl) {
      text1 = "Ctrl";
      ctrlIsDown=true;
    }
  } else {
    // without control
    if (keyCode==36) {
      text1 = "Pos1";
    } else if (keyCode==35) {
      text1 = "End";
    } else if (keyCode==33) {
      text1 = "Pg Up";
    } else if (keyCode==34) {
      text1 = "Pg Down";
    } else if (keyCode==keyCodeCtrl) {
      text1 = "Ctrl";
      ctrlIsDown=true;
    }
  }//else
}//func 
//


The answer is to hook directly into Processing’s key event system like this

void setup() {
  size(400, 400);
  this.registerMethod("keyEvent", this);
}

public void keyEvent(KeyEvent ke) {
  if (ke.getAction() == KeyEvent.PRESS) {    
    boolean ctrlDown = (ke.getModifiers() & KeyEvent.CTRL) == KeyEvent.CTRL;
    String desc = ctrlDown ? "Ctrl + " : "";
    switch(ke.getKeyCode()) {
    case 36:
      desc += "Start of line";
      break;
    case 35:
      desc += "End of line";
      break;
    case 33:
      desc += "Start of Document";
      break;
    case 34:
      desc += "End of Document";
      break;
    default:
      desc = "";
    }
    println(desc);
  }
}

void draw() {
  background(255);
}

You can still use keyPressed, keyReleased and keyTyped methods in the same sketch :smile:

Wait, is it different in P3D???

I get 2 and 3 for Pos1 and End?

oh boy…

can I let my program check automatically whether we are in P3D or P2D?

:slight_smile:

Sketch isKeyDown()
Method is3D()

1 Like

P2D and P3D use OpenGL so the sketch opens in a GLWindow whereas, in JAVA mode (default) it uses a JFrame. The type of events and the event handling mechanism is different for each. Processing is supposed to handle this under the hood and provide a common interface. Unfortunately it doesn’t always work.

I raised a couple of issues on github several years ago about problems with key event handling and they remained open for ages. In fact I never received notification that they were fixed or even closed but they seem to have disappeared. I suspect they have been simply deleted.

2 Likes

Thanks for that

I am not even sure the key==CODED is always the same.

Which means you cannot write a class using those keys that works for java and P3D…

If we know the keyCode for both renderers we can decide which 1 to use by checking is3D(). :keyboard:

1 Like