Xkynar
Xkynar

Reputation: 953

How to distinguish the commonly printed keyboard keys from the rest

I want to collect keyboard input and append it together in a java StringBuilder, but using LWJGL's Keyboard event, i end up fetching more than I wish, like Shift, CapsLock, Escape, F1 to F12, Enter, even punctuation etc.. These keys also have key ID's, but by appending them, they are printed as a square (unrecognized character i believe).

My goal is to ignore these non-printable keys without having to create a giant array with all these unwanted keys. Is there any way to do so?

P.S. Mind that i wish the common symbols like \,.-< etc. to still be considered into the string, like any text editor would.

Upvotes: 0

Views: 270

Answers (2)

Xkynar
Xkynar

Reputation: 953

Thanks to Mark W I just found out that the ASCII range from 32 to 126 and 128 to 255 covers, i believe, every single character that is commonly printed in the everyday text-editors. Thanks a bunch :)

Here is a minimal code chunk that might be useful for someone using lwjgl

private StringBuilder text;

    private void updateInput()
    {
        while (Keyboard.next())
        {
            if (Keyboard.getEventKeyState())
            {
                // get key info
                int key = Keyboard.getEventKey();
                char ch = Keyboard.getEventCharacter();
                int ascii = (int) ch;

                // delete case
                if(key == Keyboard.KEY_BACK)
                    text.setLength(Math.max(0, text.length() - 1));

                // append if common char
                if((ascii >= 32 && ascii <= 126) || (ascii >= 128 && ascii <= 255))
                    text.append(ch);
            }
        }
    }

Upvotes: 0

Thibstars
Thibstars

Reputation: 1083

Here's an example of what you could use. This example is then used as an inner class and you use it in stead of a regular ActionListener on a component. This example catches the keycode of the event (using KeyEvent). I placed some examples you asked in your questions, I'm sure you'll find more if needed.

You should append a custom string to your existing string in every case of the switch statement.

public class CustomListener extends KeyAdapter {
        @Override
        public void keyPressed(KeyEvent e) {
            try {
                int keyCode = e.getKeyCode();
                switch (keyCode) {
                        case KeyEvent.VK_SHIFT:
                            //Append a string to your existing string
                            break;
                        case KeyEvent.VK_F1:
                            //Append a string to your existing string
                            break;
                        case KeyEvent.VK_CAPS_LOCK:
                            //Append a string to your existing string
                            break;
                        case KeyEvent.VK_ENTER:
                            //Append a string to your existing string
                            break;
                    }
                }
            } catch (NullPointerException e1) {
                e1.printStackTrace();
            }
        }
    }

Upvotes: 0

Related Questions