I was kind of unsure how to ask this question since my code is almost right, but anyway I am implementing the usual WASD keys for movement on a robot I have. For now I am simply working on the keybindings to a button. So my code is supposed to display the key when I hit it and display the not equivalent when it is released, but the way it displays it seems more like the program is polling the keyboard rather than interrupting it when the key is hit. So when I hit a key and hold it it rapidly hits the button over and over again, but I want it to simply hit the button once when I hit a W A S D key and hold it, and then release the button when i release the Key. The release works fine, but when I hold it, for example if I hit and hold the W key it displays " w w w w w w w w w w w" then "w!" when it is released and I want it to display " w", then "w!" when released. Do I need to use a different method to bind the keys, or is there a better way to implement the buttons? My code is relatively short. Here is my full code:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import java.awt.event.KeyEvent;
import javax.swing.JToggleButton;
public class RobotWASDKeys
{
private static JFrame mainFrame;
private static JToggleButton wButton;
private static JToggleButton aButton;
private static JToggleButton sButton;
private static JToggleButton dButton;
private static JPanel mainPanel;
private static Action wAction;
private static Action wNotAction;
private static Action aAction;
private static Action aNotAction;
private static Action sAction;
private static Action sNotAction;
private static Action dAction;
private static Action dNotAction;
private static ButtonListener buttonListener;
public static void main( String[] args )
{
mainFrame = new JFrame( "Buttons" );
mainFrame.add( makePanel() );
mainFrame.setLocationRelativeTo( null );
mainFrame.setSize( 300, 200 );
mainFrame.setResizable( false );
mainFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
mainFrame.setVisible( true );
mainFrame.getContentPane().validate();
mainFrame.getContentPane().repaint();
}
//this is my panel
static JPanel makePanel()
{
mainPanel = new JPanel();
buttonListener = new ButtonListener();
wButton = new JToggleButton("w");
aButton= new JToggleButton("a");
sButton= new JToggleButton("s");
dButton= new JToggleButton("d");
wButton.addActionListener(buttonListener);
aButton.addActionListener(buttonListener);
sButton.addActionListener(buttonListener);
dButton.addActionListener(buttonListener);
wAction = new WAction();
wNotAction = new WNotAction();
aAction = new AAction();
aNotAction=new ANotAction();
sAction = new SAction();
sNotAction=new SNotAction();
dAction = new DAction();
dNotAction=new DNotAction();
//binds w a s d keys to an action
mainPanel.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_W, 0,true),"forwardNot");
mainPanel.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_W,0,false),"forward");
mainPanel.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_A,0,false),"left");
mainPanel.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_A,0,true),"leftNot");
mainPanel.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_S,0,false),"backward");
mainPanel.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_S,0,true),"backwardNot");
mainPanel.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_D,0,false),"right");
mainPanel.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_D,0,true),"rightNot");
mainPanel.getActionMap().put("forward", wAction);
mainPanel.getActionMap().put("forwardNot", wNotAction);
mainPanel.getActionMap().put("left", aAction);
mainPanel.getActionMap().put("leftNot",aNotAction);
mainPanel.getActionMap().put("backward", sAction);
mainPanel.getActionMap().put("backwardNot", sNotAction);
mainPanel.getActionMap().put("right", dAction);
mainPanel.getActionMap().put("rightNot",dNotAction);
mainPanel.add(wButton);
mainPanel.add(aButton);
mainPanel.add(sButton);
mainPanel.add(dButton);
return mainPanel;
}
//prints a string to the command prompt when a key is hit.
static class WAction extends AbstractAction
{
public void actionPerformed( ActionEvent tf )
{
System.out.println( "w" );
wButton.doClick();
}
}
static class WNotAction extends AbstractAction
{
public void actionPerformed( ActionEvent tf )
{
System.out.println( "w!" );
wButton.doClick();
}
}
static class AAction extends AbstractAction
{
public void actionPerformed( ActionEvent tf )
{
System.out.println( "a" );
aButton.doClick();
}
}
static class ANotAction extends AbstractAction
{
public void actionPerformed( ActionEvent tf )
{
System.out.println( "a!" );
aButton.doClick();
}
}
static class SAction extends AbstractAction
{
public void actionPerformed( ActionEvent tf )
{
System.out.println( "s" );
sButton.doClick();
}
}
static class SNotAction extends AbstractAction
{
public void actionPerformed( ActionEvent tf )
{
System.out.println( "s!" );
sButton.doClick();
}
}
static class DAction extends AbstractAction
{
public void actionPerformed( ActionEvent tf )
{
System.out.println( "d" );
dButton.doClick();
}
}
static class DNotAction extends AbstractAction
{
public void actionPerformed( ActionEvent tf )
{
System.out.println( "d!" );
dButton.doClick();
}
}
static class ButtonListener implements ActionListener
{
public void actionPerformed( ActionEvent bp )
{
mainPanel.requestFocusInWindow();
}
}
}