Toggle Key in Java -
basically i'm trying toggle boolean every time f1 key pressed. in main class i've got update method called 60 times second , coded this:
main.class:
private keyboard key = new keyboard; // keyboard object boolean debug; // boolean control visibility of debug screen private boolean toggle; // used toggle boolean private void update() { // update called 60/s if (key.iskeypressed(keyboard.f1) && !toggle) { debug = !debug; toggle = true; } else if (!key.iskeypressed(keyboard.f1)) toggle = false; }
keyboard.class:
public class keyboard implements keylistener { private boolean[] keys = new boolean[65536]; public static final int f1 = keyevent.vk_f1; // key code of f1 key public void keypressed(keyevent e) { keys[e.getkeycode()] = true; } public void keyreleased(keyevent e) { keys[e.getkeycode()] = false; } public void keytyped(keyevent e) { } public boolean iskeypressed(int key) { return keys[key]; }
and way works perfect, if create similar method in keyboard.class not work:
update method in main class:
private void update() { key.toggle(keyboard.f1, debug); // toggle boolean debug if f1 key pressed, but.. not working!! whyyy!!? }
keyboard.class:
private boolean toggle; public void toggle(int key, boolean b) { if (iskeypressed(key) && !toggle) { b = !b; toggle = true; } else if (!iskeypressed(key)) toggle = false; }
and question is: why second way not working , how can fix work? thank you!
thank madprogrammer, you're right! ;d
main.class:
private void update() { debug = key.toggle(keyboard.f1, debug); }
keyboard.class:
private boolean toggle; public boolean toggle(int key, boolean b) { if (iskeypressed(key) && !toggle) { b = !b; toggle = true; } else if (!iskeypressed(key) && toggle) { toggle = false; } return b; }
now returns value wanted, man, forgot that. there better/easier way that?
Comments
Post a Comment