Java Key Binding Not Working -
i have code:
frame = new jframe(); frame.addwindowlistener(new windowadapter() { @override public void windowclosing(windowevent e) { isrunning = false; } }); frame.setdefaultcloseoperation(windowconstants.do_nothing_on_close); frame.setsize(width * scale, height * scale); frame.setresizable(false); frame.setvisible(true); jpanel panel = new jpanel(); frame.add(panel); panel.setfocusable(true); panel.requestfocusinwindow(); // keybinds keymanager.start(panel); and in keymanager:
public static void start(jcomponent comp) { comp.requestfocus(); comp.requestfocusinwindow(); comp.getinputmap(jcomponent.when_in_focused_window).put(keystroke.getkeystroke(keyevent.vk_w, 0), "w"); comp.getactionmap().put("w", new abstractaction() { public void actionperformed(actionevent e) { pressedkeys.put(keystroke.getkeystroke("w"), true); } }); but code works sometimes. there problem somewhere? there line of code need add?
well, fixed problem. used hashmap (keystroke, boolean) store key values. read using keystroke.getkeystroke(string key) not idea, changed hashmap using (integer, boolean) integer being keyevent.vk_(key). works :)
Comments
Post a Comment