gpt4 book ai didi

java swing 键绑定(bind) - 缺少释放键的操作

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:55:56 26 4
gpt4 key购买 nike

注册了“SPACE”和“released SPACE”的键绑定(bind),当空格是唯一按下/释放的键时,它的工作方式与广告一样,我注意到按下空格,然后按下 ctrl(或任何其他修改键),然后释放空间最后释放 ctrl 将导致执行与“SPACE”关联的操作,但不会执行与“released SPACE”关联的操作。

一旦不再按下空格(或同时按下修改键),导致执行操作的首选方法是什么?我只在 64 位 Windows 7 上试过这个。

import javax.swing.SwingUtilities;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.AbstractAction;
import javax.swing.KeyStroke;
import java.awt.event.ActionEvent;
import java.awt.Cursor;

class Bind extends JPanel {
{
getInputMap().put(KeyStroke.getKeyStroke("SPACE"), "pressed");
getInputMap().put(KeyStroke.getKeyStroke("released SPACE"), "released");
getActionMap().put("pressed", new AbstractAction() {
@Override public void actionPerformed(ActionEvent e) {
System.out.println("pressed");
setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
}
});
getActionMap().put("released", new AbstractAction() {
@Override public void actionPerformed(ActionEvent e) {
System.out.println("released");
setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
});
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override public void run() {
JFrame f = new JFrame("Key Bindings");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new Bind());
f.setSize(640, 480);
f.setVisible(true);
}
});
}
}

更新:这是避免在释放空间之前意外按下 ctrl、alt 或 shift 时粘滞空间的方法:

import javax.swing.SwingUtilities;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.AbstractAction;
import javax.swing.KeyStroke;
import java.awt.event.ActionEvent;
import java.awt.Cursor;

class Bind extends JPanel {
{
getInputMap().put(KeyStroke.getKeyStroke("SPACE"), "pressed");
getInputMap().put(KeyStroke.getKeyStroke("released SPACE"), "released");
getInputMap().put(KeyStroke.getKeyStroke("ctrl released SPACE"), "released");
getInputMap().put(KeyStroke.getKeyStroke("shift released SPACE"), "released");
getInputMap().put(KeyStroke.getKeyStroke("shift ctrl released SPACE"), "released");
getInputMap().put(KeyStroke.getKeyStroke("alt released SPACE"), "released");
getInputMap().put(KeyStroke.getKeyStroke("alt ctrl released SPACE"), "released");
getInputMap().put(KeyStroke.getKeyStroke("alt shift released SPACE"), "released");
getInputMap().put(KeyStroke.getKeyStroke("alt shift ctrl released SPACE"), "released");
getActionMap().put("pressed", new AbstractAction() {
@Override public void actionPerformed(ActionEvent e) {
System.out.println("pressed");
setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
}
});
getActionMap().put("released", new AbstractAction() {
@Override public void actionPerformed(ActionEvent e) {
System.out.println("released");
setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
});
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override public void run() {
JFrame f = new JFrame("Key Bindings");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new Bind());
f.setSize(640, 480);
f.setVisible(true);
}
});
}
}

最佳答案

released SPACE 事件在按住 Control 键时不会被触发是有道理的。我希望触发 control released SPACE 事件。

将以下内容添加到您的代码中:

getInputMap().put(KeyStroke.getKeyStroke("control released SPACE"), "released");

出于同样的原因,如果您首先按住 Control 键,则 SPACE 事件不会触发。因此,您还需要为 control SPACE 添加绑定(bind)。

您需要对所有修改键执行此操作,这可能是也可能不是比跟踪键事件更简单的解决方案。

关于java swing 键绑定(bind) - 缺少释放键的操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8524874/

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