java - How to make a keyListener go back to previous key -
so have keylistener, overcame keyboard input delay. have new problem, when user presses "up" arrow key move "up" move "up" long want, when user presses "down" arrow key, (while still holding "up" arrow key) move "down".
how allow them move "up" again after "down" arrow key has been released, without having press "up" key again?
(note: "up" key pressed throughout whole process.)
here code have use listening input.
private void keylisteners(){ globalobjects.frame.addkeylistener(new keylistener() { @override public void keypressed(keyevent e) { //up = 1 //down = 2 //left = 3 //right = 4 if(e.getkeycode() == 38){ system.out.println("up key pressed"); player.setplayermovement(1); }else if(e.getkeycode() == 40){ system.out.println("down key pressed"); player.setplayermovement(2); }else if(e.getkeycode() == 37){ player.setplayermovement(3); }else if(e.getkeycode() == 39){ player.setplayermovement(4); } } @override public void keyreleased(keyevent e) { if(e.getkeycode() == 38){ if(player.getplayermovement() != 1){ system.out.println("up key released, other arrow key changed playermovement different number (do nothing)"); }else{ system.out.println("up key last key pressed, therefore safe set player movement "0" (not moving)"); player.setplayermovement(0); } }else if(e.getkeycode() == 40){ if(player.getplayermovement() != 2){ }else{ player.setplayermovement(0); } }else if(e.getkeycode() == 37){ if(player.getplayermovement() != 3){ }else{ player.setplayermovement(0); } }else if(e.getkeycode() == 39){ if(player.getplayermovement() != 4){ }else{ player.setplayermovement(0); } } } @override public void keytyped(keyevent e) { } }); }
if couldn't tell, 2d rpg based game. (if helps @ all, answering question)
i have found similar question here, should solve problem. according official oracle key events demo here when hold 1 key, press 1 , release second one, releasing event of second 1 generated. dont know why, that. have remember every pressed key , provide response leatest pressed one.
edit: have noticed not , java issue (at least, not only). according documentation of keyevent, keypressed
, keyreleased
low-level methods:
"key pressed" , "key released" events lower-level , depend on platform , keyboard layout.
so such behaviour can platform dependent. curiosity lead me try actions on "run" prompt (ye, working on windows), , guess happend - same thing! not arrows letters well. when press , hold eg. 's' press , release 'k', while holding 's' text field input ending on 'k' sskkkkkkkkkkkk
.
Comments
Post a Comment