Results 1 to 4 of 4
  1. #1
    afraidofdark is offline Member
    Join Date
    Mar 2010
    Posts
    9
    Rep Power
    0

    Default continuous key press

    hello, I am trying to achieve a continuous key press behavior, since key listeners only allow me to listen toggles of keys I tried to hold an array to watch key states, to see whether a specific key is pressed or released, when for example up key is pressed I make this; keys[1] = true; when it released keys[1] = false, then I only check the key states, this seems working but there is a problem whit this approach as well. Java periodically sends key released events even if I keep pressing the key. So I am running the application at 60fps and I relay on key states, when java calls a keyreleased event it stops my application at 1 frame and this cause bad things. How can I fix this ?
    thank you for your suggestions.

  2. #2
    paul pasciak is offline Senior Member
    Join Date
    Jul 2008
    Posts
    125
    Rep Power
    0

    Default

    Java periodically sends key released events even if I keep pressing the key.

    My interpretation of this statement is:
    Java periodically sends key released events if I keep a key pressed.

    If this is what you mean, then my response is:

    I don't think this is true.
    The problem is that most keyboards go into
    auto-repeat when a key is held down.

    There may be a way to turn off the auto-repeat
    feature in a keyboard through your computer's
    system settings.

    Long ago, auto-repeat was an unusual feature,
    and a speacial key was added to the keyboard
    to cause auto-repeat.

    Maybe I have misinterpreted your problem, if
    so, thanks for your patience.

  3. #3
    afraidofdark is offline Member
    Join Date
    Mar 2010
    Posts
    9
    Rep Power
    0

    Default

    I am sorry for my English, I tried to say this, even though I hold key "UP" down, java sends a key released event, it really does this. To solve the problem I used a gear mechanism which start at 3 then count back if a fake release happens it count back 1 and for the next frame java immediately sends a key pressed event then gear become 3 again but if I realy release the key at the next frame gear become 1 which means a real key released event.

    if the event is a fake event java immediately sends a key pressed event after the fake release so, this fake event only stops my program for 1 frame

    my assumption about this situation is,
    In order to create a toggle key, java wait for about 0.5 second. At the end of the period it sends a key released event to capture sequentially pressed keys, this is just an assumption, source of the problem can be my keyboard too.

  4. #4
    afraidofdark is offline Member
    Join Date
    Mar 2010
    Posts
    9
    Rep Power
    0

    Default

    to test the code, put a debug point at this line "System.out.print("right key is not being pressed");" then press and hold right key

    Java Code:
    protected boolean[] keys = {false, false, false, false, false};
    
        // key listener
        class kl implements KeyListener {
    
            public void keyPressed(KeyEvent event) {
                switch (event.getKeyCode()) {
                    // up
                    case 38:
                        keys[0] = true;
                        break;
                    // down
                    case 40:
                        keys[1] = true;
                        break;
                    // left
                    case 37:
                        keys[2] = true;
                        break;
                    // right
                    case 39:
                        keys[3] = true;
                        break;
                    case 32:
                        keys[4] = true;
                        break;
                    default:
                        System.out.println(event.getKeyCode());
                }
            }
    
            // handle release of any key
            public void keyReleased(KeyEvent event) {
                switch (event.getKeyCode()) {
                    // up
                    case 38:
                        keys[0] = false;
                        break;
                    // down
                    case 40:
                        keys[1] = false;
                        break;
                    // left
                    case 37:
                        keys[2] = false;
                        break;
                    // right
                    case 39:
                        System.out.print("right key is not being pressed"); // PUT A DEBUG POINT HERE AND TEST THE CODE ! 
                        keys[3] = false;
                        break;
                }
            }
    
            // handle press of an action key
            public void keyTyped(KeyEvent event) {
            }
        }
    
        // continuous key press
        boolean isPressed(int keyCode) {
            switch (keyCode) {
                // up
                case 38:
                    return keys[0];
                // down
                case 40:
                    return keys[1];
                // left
                case 37:
                    return keys[2];
                // right
                case 39:
                    return keys[3];
                default:
                    return false;
            }
        }

Similar Threads

  1. Replies: 1
    Last Post: 04-03-2009, 07:17 AM
  2. continuous playback of chunks
    By arnab321 in forum CLDC and MIDP
    Replies: 0
    Last Post: 12-11-2008, 08:46 PM
  3. press any key to continue
    By dotnet007 in forum New To Java
    Replies: 3
    Last Post: 05-11-2008, 05:19 AM
  4. Press any key to continue/press enter
    By JT4NK3D in forum New To Java
    Replies: 1
    Last Post: 11-17-2007, 09:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •