Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Program not recognising key presses

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    4
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Program not recognising key presses

    Hey guys, new on here but I did post an introduction in the new members place

    OK, so my problem, I'm working on what is to be a bit of a basic game engine, not really for much in particular, just an assignment I have for my final year at university. A lot of what I have so far works OK, but for some reason key presses are not being recognised, maybe because I'm doing it in a silly way I realise or I just made a simple error, and its one of those things I've sat staring at for so long now that I cant see what's wrong.

    This is the keyAdapter I made, I used it as a bit of an interface so I could add different control schemes later on
    @Override
        public void keyPressed(KeyEvent e)
        {
            keys.add(e);
        }
     
        @Override
        public void keyReleased(KeyEvent e)
        {
            remove(e);
        }
     
        private void remove(KeyEvent e)
        {
            for(int i = 0; i < keys.size(); i++)
            {
                if(keys.get(i).getKeyCode() == e.getKeyCode())
                {
                    keys.remove(i);
                }
            }
        }

    Here is a part of a controls class that actually states what keys are and what they do, I did try using if(keys.contains(KeyEvent.VK_MINUS)) to just check if that keyEvent is stored but that didn't work for some reason
    if(isPressed(KeyEvent.VK_MINUS))
            {
                GlobalVariables.numBirds--;
            }
     
        private boolean isPressed(int e)
        {
            ArrayList<KeyEvent> keys = Input.get().getKeys();
     
            for(int i = 0; i < keys.size(); i++)
            {
                if(keys.get(i).getKeyCode() == e)
                {
                    return true;
                }
            }
     
            return false;
        }

    hmm, cant think what else may be the problem, if anyone else has any ideas please say and Ill get whatever code you wish to see

    Thank you for your time regardless of whether you know what my problem is or not


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,137
    Thanks
    48
    Thanked 2,156 Times in 2,129 Posts

    Default Re: Program not recognising key presses

    Please make a small, complete program that compiles, executes and shows the problem. What you posted can not be compiled and executed to show the problem.

    Using key listeners can have a problem with which component has the focus. Key binding is a better way to catch key presses.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    4
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Program not recognising key presses

    OK I think rather stupidly I left out the part where I actually add the key listener... I'm working on it now because that's what I know and with lack of time it may take a while to change things but Ill get around to it at a later date, while it was my stupidity causing this error, thank you norm for sorta making me remember somehow

Similar Threads

  1. KeyListener - how to make program react to the key that was pressed twice?
    By scorpas in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: November 28th, 2012, 07:19 AM
  2. [SOLVED] Get a new key in HashMap with values of another key.
    By Purple01 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: September 21st, 2012, 04:33 AM
  3. [SOLVED] restricting a particular key stroke or key code
    By chronoz13 in forum AWT / Java Swing
    Replies: 2
    Last Post: April 22nd, 2011, 11:19 AM
  4. Listening to 2 Key Presses
    By aussiemcgr in forum Java Theory & Questions
    Replies: 3
    Last Post: January 18th, 2011, 05:09 PM
  5. video game problem - delay in response to arrow key presses
    By gib65 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 17th, 2010, 07:39 PM