gpt4 book ai didi

java - 击键不能捕捉到左/右但能够捕捉到上/下和所有其他键

转载 作者:行者123 更新时间:2023-11-29 09:26:03 24 4
gpt4 key购买 nike

我在我的 Java 应用程序中将键绑定(bind)到事件,但是,我遇到了一个非常奇怪的错误,我希望你们能帮助我解决。

        KeyStroke up = KeyStroke.getKeyStroke("W");
KeyStroke down = KeyStroke.getKeyStroke("S");
KeyStroke right = KeyStroke.getKeyStroke("D");
KeyStroke left = KeyStroke.getKeyStroke("A");
KeyStroke p2_up = KeyStroke.getKeyStroke("UP");
KeyStroke p2_down = KeyStroke.getKeyStroke("DOWN");
KeyStroke p2_left = KeyStroke.getKeyStroke("LEFT");
KeyStroke p2_right = KeyStroke.getKeyStroke("RIGHT");
/*KeyStroke key_1 = KeyStroke.getKeyStroke("1");
KeyStroke key_2 = KeyStroke.getKeyStroke("2");
KeyStroke key_3 = KeyStroke.getKeyStroke("3");
KeyStroke key_4 = KeyStroke.getKeyStroke("4");
KeyStroke key_5 = KeyStroke.getKeyStroke("5");
KeyStroke key_6 = KeyStroke.getKeyStroke("6");
KeyStroke key_7 = KeyStroke.getKeyStroke("7");
KeyStroke key_8 = KeyStroke.getKeyStroke("8");
KeyStroke key_9 = KeyStroke.getKeyStroke("9");
KeyStroke key2_1 = KeyStroke.getKeyStroke("F1");
KeyStroke key2_2 = KeyStroke.getKeyStroke("F2");
KeyStroke key2_3 = KeyStroke.getKeyStroke("F3");
KeyStroke key2_4 = KeyStroke.getKeyStroke("F4");
KeyStroke key2_5 = KeyStroke.getKeyStroke("F5");
KeyStroke key2_6 = KeyStroke.getKeyStroke("F6");
KeyStroke key2_7 = KeyStroke.getKeyStroke("F7");
KeyStroke key2_8 = KeyStroke.getKeyStroke("F8");
KeyStroke key2_9 = KeyStroke.getKeyStroke("F9");*/

InputMap inputMap = content
.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);

inputMap.put(up, "up");
inputMap.put(down, "down");
inputMap.put(right, "right");
inputMap.put(left, "left");

inputMap.put(p2_up, "up2");
inputMap.put(p2_down, "down2");
inputMap.put(p2_right, "right2");
inputMap.put(p2_left, "left2");

Bindings KbBinds = new Bindings(this.currentBigBox);

content.getActionMap().put("up", KbBinds.moveUp);
content.getActionMap().put("down", KbBinds.moveDown);
content.getActionMap().put("right", KbBinds.moveRight);
content.getActionMap().put("left", KbBinds.moveLeft);

content.getActionMap().put("up2", KbBinds.p2MoveUp);
content.getActionMap().put("down2", KbBinds.p2MoveDown);
content.getActionMap().put("right2", KbBinds.p2MoveRight);
content.getActionMap().put("left2", KbBinds.p2MoveLeft);

基本上除了 LEFT/RIGHT 之外的所有键都被捕获并绑定(bind)。如果我更换这个

        KeyStroke p2_left = KeyStroke.getKeyStroke("LEFT");

与 KeyStroke p2_left = KeyStroke.getKeyStroke("UP");

在注释掉 p2_up 时,p2_left 按预期工作。

有什么线索吗?

最佳答案

最可能的解释是某些东西正在捕获这些击键。最有可能的候选对象是滚动 Pane 或某种输入文本框。

关于java - 击键不能捕捉到左/右但能够捕捉到上/下和所有其他键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1645361/

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