gpt4 book ai didi

java - Java 中的 KeyBinds 在按住 Shift 时会中断

转载 作者:行者123 更新时间:2023-12-02 02:38:30 25 4
gpt4 key购买 nike

当我使用按键绑定(bind)时,它们工作得很好。但是当我按住 Shift 并尝试使用按键绑定(bind)时,我的计算机没有检测到按键被按下/更改。另外,当尝试检测是否按下/释放 Shift 时,似乎什么也没有发生。

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;
}
});
}

(keyInput 方法是单独类的一部分。)

最佳答案

击键由多个部分组成。生成事件的按键代码,零个或多个修饰符,可以更改按键代码的含义以及是否按下或释放。

根据修饰符的不同,单个击键可能具有不同的含义,例如 Shift 会将 a 更改为 A1 !

这使得 Shift+a 与仅 a 不同。当您还认为您还有 CtrlAltWndCmd 并且它们可以组合在一起时,这为每个键赋予了大量额外的可能含义。

按键绑定(bind)很有趣。您可以定义单个操作并将其应用于多个击键配置

例如,我们可以从...开始

InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);
ActionMap am = getActionMap();

am.put("left", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent arg0) {
label.setText("left");
}
});
am.put("right", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent arg0) {
label.setText("right");
}
});
am.put("released", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent arg0) {
label.setText("---");
}
});

这定义了三个可以执行的 Action ,释放。每个都只是将 JLabel 的文本更改为某种状态,但您明白了。

然后您可以将这些操作应用于多个击键,例如...

im.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, 0, false), "left");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, KeyEvent.SHIFT_DOWN_MASK, false), "left");

因此,按 aShift+a 将触发向左操作

im.put(KeyStroke.getKeyStroke(KeyEvent.VK_D, 0, false), "right");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_D, KeyEvent.SHIFT_DOWN_MASK, false), "right");

dShift+d将触发操作

im.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, 0, true), "released");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, KeyEvent.SHIFT_DOWN_MASK, true), "released");

im.put(KeyStroke.getKeyStroke(KeyEvent.VK_D, 0, true), "released");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_D, KeyEvent.SHIFT_DOWN_MASK, true), "released");

释放其中任何四个键将生成released操作

可运行示例...

import java.awt.Dimension;
import java.awt.EventQueue;
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.JLabel;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

public class Test {

public static void main(String[] args) {
new Test();
}

public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class TestPane extends JPanel {

private JLabel label = new JLabel("---");

public TestPane() {
setLayout(new GridBagLayout());
add(label);

InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);
ActionMap am = getActionMap();

am.put("left", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent arg0) {
label.setText("left");
}
});
am.put("right", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent arg0) {
label.setText("right");
}
});
am.put("released", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent arg0) {
label.setText("---");
}
});

im.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, 0, false), "left");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, KeyEvent.SHIFT_DOWN_MASK, false), "left");

im.put(KeyStroke.getKeyStroke(KeyEvent.VK_D, 0, false), "right");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_D, KeyEvent.SHIFT_DOWN_MASK, false), "right");

im.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, 0, true), "released");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, KeyEvent.SHIFT_DOWN_MASK, true), "released");

im.put(KeyStroke.getKeyStroke(KeyEvent.VK_D, 0, true), "released");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_D, KeyEvent.SHIFT_DOWN_MASK, true), "released");
}

@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}

}

}

关于java - Java 中的 KeyBinds 在按住 Shift 时会中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57193272/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com