java - Wait for a key to be pressed inside loop -
i have java application , want wait key pressed perform action. till have found:
public void keypressed(keyevent e) { if (e.getkeycode() == keyevent.vk_right ) { //right arrow key code } }
but want inside loop or waits until key matches.
please help.
update:
i want program waiting until key pressed , when pressed want trigger action. example:
public void something(){ //do wait until key pressed if( key pressed arrow key){ something(); }else{ wait key pressed } } public void dootherthing(){ //do other thing }
i working in swing , don't want gui irresponsible. i.e. when call dootherthing click of button. should , waiting should over.
swing (and guis) event driven environments. is, happens, react it.
having loop waiting kind of action kind of counter intuitive (imho).
generally speaking, should avoid keylistener
s if can. have issues key focus. key bindings api has ways on come limitation.
it allows register keystroke
against action
, allows program sit , wait till happens...
import java.awt.borderlayout; import java.awt.color; import java.awt.dimension; import java.awt.eventqueue; import java.awt.graphics; import java.awt.graphics2d; import java.awt.gridbagconstraints; import java.awt.gridbaglayout; import java.awt.event.actionevent; import java.awt.event.keyevent; import javax.swing.abstractaction; import javax.swing.actionmap; import javax.swing.inputmap; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.keystroke; import javax.swing.uimanager; import javax.swing.unsupportedlookandfeelexception; import javax.swing.border.lineborder; public class keybindings { public static void main(string[] args) { new keybindings(); } public keybindings() { eventqueue.invokelater(new runnable() { @override public void run() { try { uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname()); } catch (classnotfoundexception | instantiationexception | illegalaccessexception | unsupportedlookandfeelexception ex) { } jframe frame = new jframe("testing"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setlayout(new borderlayout()); frame.add(new testpane()); frame.pack(); frame.setlocationrelativeto(null); frame.setvisible(true); } }); } public class testpane extends jpanel { public testpane() { setlayout(new gridbaglayout()); gridbagconstraints gbc = new gridbagconstraints(); gbc.gridx = 1; gbc.gridy = 0; add(new keypane(keyevent.vk_up, 0), gbc); gbc.gridy = 2; add(new keypane(keyevent.vk_down, 0), gbc); gbc.gridx = 0; gbc.gridy = 1; add(new keypane(keyevent.vk_left, 0), gbc); gbc.gridx = 2; add(new keypane(keyevent.vk_right, 0), gbc); } } public class keypane extends jpanel { public keypane(int keycode, int modifier) { setborder(new lineborder(color.dark_gray)); inputmap im = getinputmap(when_in_focused_window); actionmap = getactionmap(); im.put(keystroke.getkeystroke(keycode, modifier, false), "keypressed"); im.put(keystroke.getkeystroke(keycode, modifier, true), "keyreleased"); am.put("keypressed", new abstractaction() { @override public void actionperformed(actionevent e) { setbackground(color.red); } }); am.put("keyreleased", new abstractaction() { @override public void actionperformed(actionevent e) { setbackground(uimanager.getcolor("panel.background")); } }); } @override public dimension getpreferredsize() { return new dimension(20, 20); } } }
Comments
Post a Comment