reading CTRL-z in keyPressed()

edited March 2017 in How To...

hello all,

how can I read CTRL-z in keyPressed() ?

thank you !

Best, Chrisir ;-)

Answers

  • edited December 2015

    CTRL+Z is key 26, 032 or 0x1A.
    https://en.Wikipedia.org/wiki/Control-Z

  • edited December 2015

    doesn't work.

    do you mean

    switch (keyCode) {
    
        case 26:
    
  • edited December 2015

    ah, I got it - I have to use key

    thanks, mate!

  • edited December 2015 Answer ✓
    • keyCode only cares for individual keyboard keys.
    • Neither key combos nor lowercase letters can be distinguished by keyCode.
    • For such cases we need to rely on key instead.
  • Thank you!

  • ...and how can I read F1, F2, F3........ please?

    Thank you!

  • edited September 2017

    You can copy, paste & run this sketch in Processing to find out all keyCode values by pressing them:
    http://studio.ProcessingTogether.com/sp/pad/export/ro.9cfU11egvzT6G

    Or you can look up their corresponding constants in KeyEvent's class ref doc:
    http://docs.Oracle.com/javase/8/docs/api/java/awt/event/KeyEvent.html#field.summary

    For example, VK_F1 is 112 as keyCode: ;)

    import static java.awt.event.KeyEvent.*;
    println(VK_F1); // 112
    exit();
    
  • Without using import:

    when I just use keyCode == 112

    It seems keyCode gives 2 values directly after another?

  • Answer ✓

    It works just fine when I hit F1 and other F keys when using the online sketch in the PDE.

    Can you post your problematic sketch then? ~:>

  • edited March 2017 Answer ✓

    @Chrisir -- re:F1 key etc., check out the thread:

  • Thanks everybody, I appreciate it

  • edited March 2017

    @Chrisir

    It seems keyCode gives 2 values directly after another?

    Can you elaborate more?

  • edited March 2017

    @Chrisir Your question gave me an idea to make a Varargs function, which is something I'we wanted to do but didn't have an idea for what it would do!

    No one should use this function for keyboard shortcut's as @GoToLoop 's solution is simple, robust and has the wider support.

    I don't know about anything that requires arbitrary key press combos other than fighting games, and this isn't good for that, as it's too crude and just written for practicing varargs.
    And most keyboards aren't good for it either.

    void setup() {}
    void draw() {
      background(keycombo(70,71,72)?255:0); // white if F+G+H is pressed, otherwise black
    }
    /* keycombo(int... takes N number of keycodes and returns true if ALL of those are held, irrregardles of order pressed, or if other keys are held as well*/
    boolean[] keysheld = new boolean[128]; /* not sure if 128 is the right number */
    void keyPressed() { keysheld[keyCode]=true; /* println(keyCode); */}
    void keyReleased(){ keysheld[keyCode]=false; }
    boolean keycombo(int...k){
      for (int i=0; i<k.length; i++)
      { if (!keysheld[k[i]]){ return false;} }
      return true;
    }
    
  • Thanks!

    This code works for me for F1

    void setup() {
      size(222, 300);
    }
    
    void draw() {
    }
    
    void keyPressed() {
      if (key==CODED) {
        // coded 
        if (keyCode == java.awt.event.KeyEvent.VK_F1) { // 112
          println("F1 -----------");
        } else {
          println(keyCode);
        }
      }
    }
    
  • and this doesn't work because of P3D

    it's a bug !!!! And I was thinking I was mixing things up a bit.

    see also here https://github.com/processing/processing/issues/4654

    void setup() {
      size(222, 300, P3D);
    }
    
    void draw() {
    }
    
    void keyPressed() {
      if (key==CODED) {
        // coded
        if (key=='g') 
          println ("not possible."); 
        keyPressed2();
      }
    }
    
    void keyPressed2() {
      if (key==CODED) {
        // coded 
        if (keyCode == java.awt.event.KeyEvent.VK_F1) { // 112
          println("F1 -----------");
        } else {
          println(keyCode);
        }
      }
    }
    
Sign In or Register to comment.