Hi,

I've used keylisteners multiple times, but I am having trouble with this situation. I want the keylistener to recognize a series of characters. For example, if 'e' is typed, I want the program to listen for 'l', then 'e', etc. (spelling "elephant", if you wanted to know). It recognizes the first 'e', but does not recognize the 'l' or any different letter afterwards. I'm stumped...

Any help is greatly appreciated. I've searched the internet for an answer, but I haven't found an answer (hense why I'm here). If you find an answer, feel free to just post the link to the site.

Thanks in advanced!

it would be easier if you included some code in your post and the exact error message you are getting along with the line at which you are getting it.

There is more code than this, but this is the method I am using.

private class thehandler2 implements KeyListener {

        @Override
        public void keyTyped(KeyEvent ke) {
        }

        @Override
        public void keyPressed(KeyEvent ke) {


            int k = ke.getKeyCode();
            if (k == 37) {
                System.out.println("left");
                    k = ke.getKeyCode();
                if (k == 38) {
                    System.out.println("up");
                }
            }  
            else if (k == 39) {
                kx = kx + kspeed;
                System.out.println("right");
            } 
            else if (k == 40) {
                ky = ky + kspeed;
                System.out.println("down");
            }    
        }

        @Override
        public void keyReleased(KeyEvent ke) {
        }
    }

My error is happening in this code:

            int k = ke.getKeyCode();
                if (k == 37) {
                    System.out.println("left");
                        k = ke.getKeyCode();
                    if (k == 38) {
                        System.out.println("up");
                    }
                }

I want up to print if it is immediately followed by left. This code doesn't even print up.

Your listener will be called once for each key, so you can't look fr both keys in just one call. You need to create one or more variables outside the listener that you can use to keep track of what keys have already been pressed

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.