KeyCode issue

Hi all, first post here. New game programmer in general.

I’m working on a chat bar currently for a simple game project, and have run into an issue with KeyEvent processing.

Here is a snippet of relevant code:


public void keyTyped(KeyEvent e) {
        switch(e.getKeyCode()) {
            case KeyEvent.VK_LEFT:
                if(textCursorIndex > 0)
                    setTextCursorIndex(textCursorIndex-1);
                break;
            case KeyEvent.VK_RIGHT:
                if(textCursorIndex < text.length())
                    setTextCursorIndex(textCursorIndex+1);
                break;
            case KeyEvent.VK_BACK_SPACE:
                if(textCursorIndex > 0) {
                    textCursorIndex--;
                    removeChar();
                }
            case KeyEvent.VK_DELETE:
                if(textCursorIndex < text.length()) {
                    removeChar();
                }
                break;
            default:
                insertChar(text, e.getKeyChar(), textCursorIndex);
                break;
        }
    }

This works just fine on my Vaio, however, I am primarily working on an iMac with a nonstandard keyboard, and my “delete” key does not send a VK_DELETE or VK_BACKSPACE event. The value for e.getKeyCode() when I press “delete” during debugging is “0”, which is shared with spacebar. If somebody could tell me how to account for nonstandard keyboards in key handling code, I would greatly appreciate it.

Thanks!

Quoted from KeyEvent API

I haven’t worked on Mac OS. You can try getKeyChar() method and if see if they give different results.

That’s odd, I haven’t seen that issue before (and I dev on Mac). You can try using JInput, which works differently so would likely avoid this issue.

Thanks. Yeah, I’ll try JInput.

You can try to print what keycode the delete button sends out. At the initialization of the game, check for OS and bind keys accordingly.

Edit: …Or JInput :persecutioncomplex: