gpt4 book ai didi

linux 上的 java keylistener

转载 作者:太空宇宙 更新时间:2023-11-04 11:04:23 26 4
gpt4 key购买 nike

我正在尝试在 Linux 上用 java3d 编写游戏,为此我需要一个合适的 KeyListener。你们有人知道怎么做吗?我目前正在使用以下代码,我在网上的某个地方找到了。它工作得很好,只按住一个键,但是当我按下多个键(比如空格和 w)时,它会做一些意想不到的事情......

public class RepeatingReleasedEventsFixer implements AWTEventListener {

private final HashMap<Integer, ReleasedAction> _map = new HashMap<Integer, ReleasedAction>();

public void install() {
Toolkit.getDefaultToolkit().addAWTEventListener(this, AWTEvent.KEY_EVENT_MASK);
}

public void remove() {
Toolkit.getDefaultToolkit().removeAWTEventListener(this);
}

@Override
public void eventDispatched(AWTEvent event) {
assert event instanceof KeyEvent : "Shall only listen to KeyEvents, so no other events shall come here";
assert assertEDT(); // REMEMBER THAT THIS IS SINGLE THREADED, so no need for synch.

// ?: Is this one of our synthetic RELEASED events?
if (event instanceof Reposted) {
// -> Yes, so we shalln't process it again.
return;
}

// ?: KEY_TYPED event? (We're only interested in KEY_PRESSED and KEY_RELEASED).
if (event.getID() == KeyEvent.KEY_TYPED) {
// -> Yes, TYPED, don't process.
return;
}

final KeyEvent keyEvent = (KeyEvent) event;

// ?: Is this already consumed?
// (Note how events are passed on to all AWTEventListeners even though a previous one consumed it)
if (keyEvent.isConsumed()) {
return;
}

// ?: Is this RELEASED? (the problem we're trying to fix!)
if (keyEvent.getID() == KeyEvent.KEY_RELEASED) {
// -> Yes, so stick in wait
/**
* Really just wait until "immediately", as the point is that the subsequent PRESSED shall already have been
* posted on the event queue, and shall thus be the direct next event no matter which events are posted
* afterwards. The code with the ReleasedAction handles if the Timer thread actually fires the action due to
* lags, by cancelling the action itself upon the PRESSED.
*/
final Timer timer = new Timer(2, null);
ReleasedAction action = new ReleasedAction(keyEvent, timer);
timer.addActionListener(action);
timer.start();

_map.put(Integer.valueOf(keyEvent.getKeyCode()), action);

// Consume the original
keyEvent.consume();
}
else if (keyEvent.getID() == KeyEvent.KEY_PRESSED) {
// Remember that this is single threaded (EDT), so we can't have races.
ReleasedAction action = _map.remove(Integer.valueOf(keyEvent.getKeyCode()));
// ?: Do we have a corresponding RELEASED waiting?
if (action != null) {
// -> Yes, so dump it
action.cancel();
}
// System.out.println("PRESSED: [" + keyEvent + "]");
}
else {
throw new AssertionError("All IDs should be covered.");
}
}

/**
* The ActionListener that posts the RELEASED {@link RepostedKeyEvent} if the {@link Timer} times out (and hence the
* repeat-action was over).
*/
private class ReleasedAction implements ActionListener {

private final KeyEvent _originalKeyEvent;
private Timer _timer;

ReleasedAction(KeyEvent originalReleased, Timer timer) {
_timer = timer;
_originalKeyEvent = originalReleased;
}

void cancel() {
assert assertEDT();
_timer.stop();
_timer = null;
_map.remove(Integer.valueOf(_originalKeyEvent.getKeyCode()));
}

@Override
public void actionPerformed(@SuppressWarnings ("unused") ActionEvent e) {
assert assertEDT();
// ?: Are we already cancelled?
// (Judging by Timer and TimerQueue code, we can theoretically be raced to be posted onto EDT by TimerQueue,
// due to some lag, unfair scheduling)
if (_timer == null) {
// -> Yes, so don't post the new RELEASED event.
return;
}
// Stop Timer and clean.
cancel();
// Creating new KeyEvent (we've consumed the original).
KeyEvent newEvent = new RepostedKeyEvent((Component) _originalKeyEvent.getSource(),
_originalKeyEvent.getID(), _originalKeyEvent.getWhen(), _originalKeyEvent.getModifiers(),
_originalKeyEvent.getKeyCode(), _originalKeyEvent.getKeyChar(), _originalKeyEvent.getKeyLocation());
// Posting to EventQueue.
Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(newEvent);
// System.out.println("Posted synthetic RELEASED [" + newEvent + "].");
}
}

/**
* Marker interface that denotes that the {@link KeyEvent} in question is reposted from some
* {@link AWTEventListener}, including this. It denotes that the event shall not be "hack processed" by this class
* again. (The problem is that it is not possible to state "inject this event from this point in the pipeline" - one
* have to inject it to the event queue directly, thus it will come through this {@link AWTEventListener} too.
*/
public interface Reposted {
// marker
}

/**
* Dead simple extension of {@link KeyEvent} that implements {@link Reposted}.
*/
public static class RepostedKeyEvent extends KeyEvent implements Reposted {
public RepostedKeyEvent(@SuppressWarnings ("hiding") Component source, @SuppressWarnings ("hiding") int id,
long when, int modifiers, int keyCode, char keyChar, int keyLocation) {
super(source, id, when, modifiers, keyCode, keyChar, keyLocation);
}
}

private static boolean assertEDT() {
if (!EventQueue.isDispatchThread()) {
throw new AssertionError("Not EDT, but [" + Thread.currentThread() + "].");
}
return true;
}
}

我不可能是唯一一个仍然遇到这个问题的人 - 同时 15 岁。 - 问题并且不想使用计时器...

编辑:此代码所做的是修复任何 Linux 发行版上的已知问题,您可以在其中添加一个简单的 KeyListener,它处理 keyDown,但会重复调用 keyReleased 事件。在这里澄清我的问题一个简单的例子

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;

public class Test5 extends JFrame{

public Test5() {
addKeyListener(new KeyListener() {
boolean keydown = false;
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub

}

@Override
public void keyReleased(KeyEvent arg0) {
keydown = false;
System.out.println("keyup");
}

@Override
public void keyPressed(KeyEvent arg0) {
if (keydown){
System.out.println("key is down");
} else {
System.out.println("key not down");
}
keydown = true;
}
});

setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(400, 400);
setVisible(true);
//new RepeatingReleasedEventsFixer().install(); // This line will fix it for one key pressed
}
public static void main(String[] args) {
new Test5();
}

}

没有被注释掉的行的输出:

key not down
keyup
key not down
keyup
key not down
keyup
key not down
keyup
key not down
keyup

否则:

key not down
key is down
key is down
key is down
key is down
key is down
key is down
key is down
key is down
key is down
keyup

顺便说一句。怎么现在还没修好?

编辑:我按照建议尝试了 KeyBindings,遇到这些问题时:

public class Test5 extends JFrame{
long timestamp = 0;
public Test5() {
((JComponent)getComponent(0)).getInputMap().put(KeyStroke.getKeyStroke('a'), "a");
((JComponent)getComponent(0)).getActionMap().put("a", new AbstractAction() {

@Override
public void actionPerformed(ActionEvent e) {
System.out.println("time: "+(System.currentTimeMillis()-timestamp));
timestamp = System.currentTimeMillis();
}
});

((JComponent)getComponent(0)).getInputMap().put(KeyStroke.getKeyStroke('s'), "s");
((JComponent)getComponent(0)).getActionMap().put("s", new AbstractAction() {

@Override
public void actionPerformed(ActionEvent arg0) {
System.out.println("s");
}
});

((JComponent)getComponent(0)).getInputMap().put(KeyStroke.getKeyStroke('d'), "d");
((JComponent)getComponent(0)).getActionMap().put("d", new AbstractAction() {

@Override
public void actionPerformed(ActionEvent arg0) {
System.out.println("d");
}
});
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(400, 400);
setVisible(true);
new RepeatingReleasedEventsFixer().install(); // This line will fix it for one key pressed
}

/**
* @param args
*/
public static void main(String[] args) {
new Test5();
}

按住“a”会得到以下输出:

time: 4171
time: 501
time: 30
time: 30
time: 30

第二次才是真正的问题所在。大约需要 470 毫秒。
按住“s”然后按“d”会给我输出:

s
s
s
s
d
d
d
d
d

所以我不能同时处理两个 Action ,所以我不能使用KeyBindings

最佳答案

这不是答案,它是带有图片和一些解释的长评论。

我使用了您的 Test5(没有 RepeatingReleasedEventsFixer)来按住 a 并测量时间响应。输出的形式是

time: t1
time: t2
time: t3
time: t3
time: t3
...

t1 没有意义,因为它取决于当前时间,与响应时间无关(你似乎也忽略了它)。

t2 是操作系统意识到您按住了重复输入键所需的时间。

t3 是保持键的“采样时间”,或输入的离散化。

我使用的 Windows 具有以下控制面板选项:

enter image description here

重复延迟 允许我将 t2 设置在 ~257(短)和 ~1050(长)之间。

重复率 允许我将 t3 设置在 ~407(慢)和 ~37(快)之间。

对于 Linux,如果您还不知道如何更改这些值,则必须向某人/某处咨询如何更改这些值。

关于使用多个 key ,请参阅 this question and answer以及其中的优秀链接(尤其是“按下多个键的 Action ”部分)。这是关于键绑定(bind)和键监听器的简短教程和分析,类似于我在本网站上发送给您的教程。

键绑定(bind)将始终优先于键监听器,除非您可能想要做一些非常低级别的事情。

关于linux 上的 java keylistener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25827652/

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