1

First thing i want to do is to apologize for my english, i will try to write as understanable as possible. Also, i have already tried to search the solution for this problem, but i didnt find one until now..

The problematic part of the code is the following:

    //eventmanagement for the bat
    scene.setOnKeyPressed (new EventHandler <KeyEvent> () {
        public void moveBat(double speed) {
            if ((speed > 0) && ((bat.getLayoutX() + bat.getWidth())<scene.getWidth())){
                bat.setLayoutX(bat.getLayoutX() + speed);
            }
            if ((speed < 0) && ((bat.getLayoutX() > 0))){
                bat.setLayoutX(bat.getLayoutX() + speed);
            }
        };

        @Override
        public void handle(KeyEvent event){
            if (event.getCode().toString() == "RIGHT"){
                this.moveBat(batVelocity);          
        }
            if (event.getCode().toString() == "LEFT"){
                this.moveBat(-batVelocity);
        }
    }
    });

This thing works, but if i press the LEFT key for example, and i dont unpress it, so just let it remain pressed, then the "bat" will move left once, then delay for about 1 second, and then continue moving in the left direction.

I want to have a continuos movement in the left direction for the time the LEFT button remains pressed. Anyone has an idea how to fix this??

Thank you very much for your time and answers!!

Crutz

Crutz
  • 21
  • 4
  • See https://stackoverflow.com/questions/21331519/how-to-get-smooth-animation-with-keypress-event-in-javafx and https://stackoverflow.com/questions/28749737/javafx-key-interruptions – James_D Oct 17 '17 at 17:39
  • 1
    And: [don't use == to compare strings](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java), that is wrong in every way. Compare for keypresses as follows `event.getCode() == KeyCode.RIGHT` – M. le Rutte Oct 17 '17 at 18:52
  • Alright, thank you very much!! :) – Crutz Nov 03 '17 at 00:49

2 Answers2

0

So, OnKeyPressed fires when a key is first pressed and then every second or so after. If you want something to happen continuously, instead of having it happen when the OnKeyPressed event fires, consider having OnKeyPressed and OnKeyReleased control some sort of boolean keyIsPressed, and using that in a while loop of some sort. So OnKeyPressed would set keyIsPressed=true and OnKeyReleased would do the opposite

MMAdams
  • 1,508
  • 15
  • 29
  • Not a while loop, that will lock the application. Use a `Timeline` with the drawing as the key frame's event, play on key pressed and stop on release. – Mordechai Oct 18 '17 at 03:49
  • @MouseEvent thanks, you're right, I wasn't thinking, I typed this up really fast. – MMAdams Oct 18 '17 at 13:46
  • this one helped me out, thank you for the timeline comment!! – Crutz Nov 03 '17 at 00:38
0

So what you wanna do is save your KeyCode to a List and check it in your AnimationTimer:

Fields:

    private List<KeyCode> input = new ArrayList<>();

and your listeners:

    scene.setOnKeyPressed((KeyEvent event) -> {
        input.add(event.getCode());
    });
    scene.setOnKeyReleased((KeyEvent event) -> {
        input.remove(event.getCode());
    });

And in your AnimationTimer:

    if (input.contains(KeyCode.RIGHT)) {
        //do something
    }
    if (input.contains(KeyCode.LEFT)) {
        //do something else
    }
Aaron Stein
  • 526
  • 7
  • 18
  • @Crutz did my answer helped you? If not let me know. If: then please mark it as the answer! – Aaron Stein Oct 31 '17 at 17:49
  • Hi, i tried the answer out, but i still have the same problem! It didnt change anything sadly..if i keep the RIGHT Button pressed, it still waits a second until it moves constantly right. – Crutz Nov 03 '17 at 00:25