Sam
Sam

Reputation: 822

Java input on OS X, no key repeat, but with glut yes

I've been a few months playing with OpenGL and glut in C++ on OS X. I'm now trying to use Java (using JOGL) with OpenGL, but I can't figure out this problem.

With glut, if I press a key and keep it pressed, glut will keep receiving events and what I see in my output is a series of that letter being pressed. However, using JOGL, I'm unable to get that behaviour. Instead I get only one letter being pressed.

I know that on OS X, that's actually a system thing to prevent key repeat, but then how come glut bypasses that? And is there a way to get that sort of bypass with JOGL?

I have tried, using the following code, to get the key to be repeated, but when I press a key, one single print of the key is appearing on my terminal, instead of a serie of it like with glut.

Here's what I've tried so far

class keyTest implements KeyEventDispatcher {

    @Override
    public boolean dispatchKeyEvent(KeyEvent arg0) {
        // TODO Auto-generated method stub
        System.out.println("dada "+arg0.getKeyChar());
        return false;
    }
}

KeyboardFocusManager man = KeyboardFocusManager.getCurrentKeyboardFocusManager();
man.addKeyEventDispatcher(new keyTest());

I've also tried

glcanvas.addKeyListener(new KeyListener() {

    @Override
    public void keyTyped(KeyEvent arg0) {
        // TODO Auto-generated method stub
    }
    @Override
    public void keyReleased(KeyEvent arg0) {
        // TODO Auto-generated method stub
    }

    @Override
    public void keyPressed(KeyEvent arg0) {
        // TODO Auto-generated method stub
        System.out.println("keypressed: "+arg0.getKeyChar());               
    }
});

With the same unwanted behavior: only one key printed, instead of a serie of it.

EDIT: I've seen some terminal commands in OS X's terminal to disable the auto-repeat block, but that would defeat the portability advantages that java offers.

EDIT2: I tried using lwjgl, and I was able get a repeating key behaviour with that library. The logic is different though. Using lwjgl, I tested if (Keyboard.isKeyDown(Keyboard.getEventKey())) and then did a switch case for each key character, contrary to the event listening and reaction I'm used to with java. I'm still wondering how to do that with JOGL.

Upvotes: 0

Views: 376

Answers (1)

xranby
xranby

Reputation: 596

AWT input depend on your JVM vendors AWT implementation. Any issues with key-repeat using AWT input has to be filed with your JRE/JDK vendor.

JogAmp JOGL developers recommend you to use the NEWT input and windowing toolkit to handle key-input for JOGL applications, NEWT is also designed to let your application scale to systems without AWT. Since NEWT handle input directly allows your applications input system to work the same across systems. http://jogamp.org/jogl/doc/NEWT-Overview.html

You may switch from using a GLCanvas (AWT) to use a GLWindow (NEWT) directly or a NewtCanvasAWT (NEWT GLWindow inside an AWT application) and then implement a NEWTKeyAdapter to receive the autorepeat key events.

Auto repeat using the NEWT implementation is validated to work on OSX using the JogAmp JOGL TestNewtKeyEventAutoRepeatAWT unit-test.

If you still experience any issues with NEWT then feel free to file a bug-report with the JogAmp team: http://jogamp.org/wiki/index.php/Jogl_FAQ#Bugreports_.26_Testing

Upvotes: 1

Related Questions