When I use my keybinds, they work just fine. But the minute I hold shift and try to use keybinds, my computer doesn't detect that keys are being pressed/changed. Also, when trying to detect if shift is being pressed/released, it seems like nothing happens.
package main;
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.JComponent;
import javax.swing.KeyStroke;
public class Input
{
private InputMap im;
private ActionMap am;
public Input(JComponent component)
{
im = component.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
am = component.getActionMap();
im.put(KeyStroke.getKeyStroke("released ESCAPE"), "exitGame");
am.put("exitGame", new AbstractAction()
{
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
}
public void createKey(String key, AbstractAction action)
{
String command = key + "Action";
im.put(KeyStroke.getKeyStroke(key), command);
am.put(command, action);
}
public void createKey(int key, boolean released, AbstractAction action)
{
String command = key + "Action";
im.put(KeyStroke.getKeyStroke(key, 0, released), command);
am.put(command, action);
}
}
public void keyInput(Input input)
{
input.createKey("A", new MoveAction(Direction.LEFT, false));
input.createKey("released A", new MoveAction(Direction.LEFT, true));
input.createKey("D", new MoveAction(Direction.RIGHT, false));
input.createKey("released D", new MoveAction(Direction.RIGHT, true));
//Added this for testing purposes, but without it, holding shift down seems to stop all other inputs.
input.createKey(KeyEvent.VK_SHIFT, false, new AbstractAction()
{
@Override
public void actionPerformed(ActionEvent e) {
sprint = true;
System.out.println("Test!"); //THIS LINE NEVER APPEARS.
}
});
input.createKey(KeyEvent.VK_SHIFT, true, new AbstractAction()
{
@Override
public void actionPerformed(ActionEvent e) {
sprint = false;
}
});
}
(The keyInput method is part of a seperate class.)