key & keyCode return wrong values in P2D and P3D

edited September 2015 in Using Processing

Tested in Processing 3.0b5 on Windows 7 (64bit). Use the following code to replicate the behavior:

void setup() {
    size(200, 200, P2D); // Comment out to test with the default renderer
}

void draw() { }

void keyPressed() {
    println("Key: " + (int)key);
    println("KeyCode: " + keyCode);
}

At least INSERT, HOME, PAGE_UP, DELETE, END, PAGE_DOWN and F1 - F12 are affected.

Edit: Thanks for moving, I wasn't sure about where to post bug reports (as I have no github account).

Answers

  • edited September 2015

    I guess that NEWT's KeyEvent returns other key and keyCode values than AWT's KeyEvent. But as it could be connected to the OS, could someone confirm that this is also happening on other platforms..? @codeanticode @PhiLho @quark @GoToLoop

    Just test the output for the DELETE key. In the default renderer (JAVA2D) it prints (as expected):

    Key: 127
    KeyCode: 127
    

    While in P2D and P3D it will output:

    Key: 0
    KeyCode: 147
    

    At least on my PC.

  • edited September 2015

    Can't do any testing as away on holiday. Processing wraps up the native event object in its own class processing.event.KeyEvent

    The keyCode and key values should be the same for all renderers and OS since PS is supposed to be cross platform, so this sounds like a bug.

  • edited September 2015

    Oh, have a nice trip @quark. ;)

    Jub, the wrapping takes place in the nativeKeyEvent methods from PSurfaceAWT and PSurfaceJOGL.

    Looking at PSurfaceJOGL's nativeKeyEvent, it seems like only a few NEWT key codes get actually converted to AWT key codes...

  • edited September 2015

    Just adding a simple switch statement to convert from NEWT to AWT key codes solves the problem:

    import com.jogamp.newt.event.KeyEvent;
    
    void setup() {
        size(200, 200, P2D);
    }
    
    void draw() { }
    
    void keyPressed() {
    
        // Convert from NETW to AWT key codes/chars
        switch(keyCode) {
            case com.jogamp.newt.event.KeyEvent.VK_DELETE:
                keyCode = PConstants.DELETE;
                key = (char)PConstants.DELETE;
                break;
            case com.jogamp.newt.event.KeyEvent.VK_PAGE_UP:
                keyCode = java.awt.event.KeyEvent.VK_PAGE_UP;
                key = (char)PConstants.CODED;
                break;
            case com.jogamp.newt.event.KeyEvent.VK_PAGE_DOWN:
                keyCode = java.awt.event.KeyEvent.VK_PAGE_DOWN;
                key = (char)PConstants.CODED;
                break;
            case com.jogamp.newt.event.KeyEvent.VK_INSERT:
                keyCode = java.awt.event.KeyEvent.VK_INSERT;
                key = (char)PConstants.CODED;
                break;
            case com.jogamp.newt.event.KeyEvent.VK_END:
                keyCode = java.awt.event.KeyEvent.VK_END;
                key = (char)PConstants.CODED;
                break;
            case com.jogamp.newt.event.KeyEvent.VK_HOME:
                keyCode = java.awt.event.KeyEvent.VK_HOME;
                key = (char)PConstants.CODED;
                break;
            /* ... */
            default:
                if(keyCode >= com.jogamp.newt.event.KeyEvent.VK_F1 && keyCode <= com.jogamp.newt.event.KeyEvent.VK_F12) {
                    keyCode = java.awt.event.KeyEvent.VK_F1 + (keyCode - com.jogamp.newt.event.KeyEvent.VK_F1);
                    key = (char)PConstants.CODED;
                }
        }
    
        println("Key: " + (int)key);
        println("KeyCode: " + keyCode);
    
    }
    

    Could someone with an github account report this bug and the given solution? :)

  • edited September 2015

    The following sketch will compare the virtual key codes of NEWT and AWT and print out all of the differences:

    import com.jogamp.newt.event.KeyEvent;
    import java.lang.reflect.Field;
    
    void setup() {
        println("VK_KEY: NEWT -> AWT");
        println("-------------------");
        for(Field newtField : com.jogamp.newt.event.KeyEvent.class.getDeclaredFields())
            if(java.lang.reflect.Modifier.isStatic(newtField.getModifiers())) {
                String fieldName = newtField.getName();
                if(fieldName.startsWith("VK_")) {
                    try {
                        Field awtField = java.awt.event.KeyEvent.class.getField(fieldName);
                        int newtKeyCode = newtField.getShort(null) & 0xffff;
                        int awtKeyCode = awtField.getInt(null);
                        if(newtKeyCode != awtKeyCode) // Remove to see all key codes in camparison
                            println(fieldName + ": " + newtKeyCode + " -> " + awtKeyCode);
                    } catch(Exception e) { }
                }
            }
    }
    

    This could be used to create a map for a more efficient (and cleaner?) key code conversion (than if-else-chains or switchs).

  • edited September 2015

    Update: NEWT/AWT keyCode inconsistency is still an issue in Processing 3.0 b7.

    List of inconsistent keyCodes:

    VK_KEY: NEWT -> AWT
    -------------------
    VK_HOME: 2 -> 36
    VK_END: 3 -> 35
    VK_FINAL: 4 -> 24
    VK_PRINTSCREEN: 5 -> 154
    VK_PAGE_DOWN: 11 -> 34
    VK_ENTER: 13 -> 10
    VK_SHIFT: 15 -> 16
    VK_PAGE_UP: 16 -> 33
    VK_ALT_GRAPH: 19 -> 65406
    VK_PAUSE: 22 -> 19
    VK_SCROLL_LOCK: 23 -> 145
    VK_CANCEL: 24 -> 3
    VK_INSERT: 26 -> 155
    VK_EXCLAMATION_MARK: 33 -> 517
    VK_QUOTEDBL: 34 -> 152
    VK_NUMBER_SIGN: 35 -> 520
    VK_DOLLAR: 36 -> 515
    VK_AMPERSAND: 38 -> 150
    VK_QUOTE: 39 -> 222
    VK_LEFT_PARENTHESIS: 40 -> 519
    VK_RIGHT_PARENTHESIS: 41 -> 522
    VK_ASTERISK: 42 -> 151
    VK_PLUS: 43 -> 521
    VK_COLON: 58 -> 513
    VK_LESS: 60 -> 153
    VK_GREATER: 62 -> 160
    VK_AT: 64 -> 512
    VK_CIRCUMFLEX: 94 -> 514
    VK_UNDERSCORE: 95 -> 523
    VK_BACK_QUOTE: 96 -> 192
    VK_F1: 97 -> 112
    VK_F2: 98 -> 113
    VK_F3: 99 -> 114
    VK_F4: 100 -> 115
    VK_F5: 101 -> 116
    VK_F6: 102 -> 117
    VK_F7: 103 -> 118
    VK_F8: 104 -> 119
    VK_F9: 105 -> 120
    VK_F10: 106 -> 121
    VK_F11: 107 -> 122
    VK_F12: 108 -> 123
    VK_F13: 109 -> 61440
    VK_F14: 110 -> 61441
    VK_F15: 111 -> 61442
    VK_F16: 112 -> 61443
    VK_F17: 113 -> 61444
    VK_F18: 114 -> 61445
    VK_F19: 115 -> 61446
    VK_F20: 116 -> 61447
    VK_F21: 117 -> 61448
    VK_F22: 118 -> 61449
    VK_F23: 119 -> 61450
    VK_F24: 120 -> 61451
    VK_SEPARATOR: 127 -> 108
    VK_NUMPAD0: 128 -> 96
    VK_NUMPAD1: 129 -> 97
    VK_NUMPAD2: 130 -> 98
    VK_NUMPAD3: 131 -> 99
    VK_NUMPAD4: 132 -> 100
    VK_NUMPAD5: 133 -> 101
    VK_NUMPAD6: 134 -> 102
    VK_NUMPAD7: 135 -> 103
    VK_NUMPAD8: 136 -> 104
    VK_NUMPAD9: 137 -> 105
    VK_DECIMAL: 138 -> 110
    VK_ADD: 139 -> 107
    VK_SUBTRACT: 140 -> 109
    VK_MULTIPLY: 141 -> 106
    VK_DIVIDE: 142 -> 111
    VK_DELETE: 147 -> 127
    VK_NUM_LOCK: 148 -> 144
    VK_LEFT: 149 -> 37
    VK_UP: 150 -> 38
    VK_RIGHT: 151 -> 39
    VK_DOWN: 152 -> 40
    VK_CONTEXT_MENU: 153 -> 525
    VK_WINDOWS: 154 -> 524
    VK_META: 155 -> 157
    VK_COMPOSE: 157 -> 65312
    VK_BEGIN: 158 -> 65368
    VK_STOP: 159 -> 65480
    VK_INVERTED_EXCLAMATION_MARK: 161 -> 518
    VK_EURO_SIGN: 8364 -> 516
    VK_CUT: 63609 -> 65489
    VK_COPY: 63610 -> 65485
    VK_PASTE: 63611 -> 65487
    VK_UNDO: 63612 -> 65483
    VK_AGAIN: 63613 -> 65481
    VK_FIND: 63614 -> 65488
    VK_PROPS: 63615 -> 65482
    VK_INPUT_METHOD_ON_OFF: 63632 -> 263
    VK_CODE_INPUT: 63633 -> 258
    VK_ROMAN_CHARACTERS: 63634 -> 245
    VK_ALL_CANDIDATES: 63635 -> 256
    VK_PREVIOUS_CANDIDATE: 63636 -> 257
    VK_ALPHANUMERIC: 63637 -> 240
    VK_KATAKANA: 63638 -> 241
    VK_HIRAGANA: 63639 -> 242
    VK_FULL_WIDTH: 63640 -> 243
    VK_HALF_WIDTH: 63642 -> 244
    VK_JAPANESE_KATAKANA: 63643 -> 259
    VK_JAPANESE_HIRAGANA: 63644 -> 260
    VK_JAPANESE_ROMAN: 63645 -> 261
    VK_KANA_LOCK: 63647 -> 262
    
Sign In or Register to comment.