简体   繁体   English

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

[英]Keystroke not catching LEFT/RIGHT but able to catch UP/DOWN and all other keys

I'm binding keys to events in my Java app, however, I have encountered a really strange bug that I hope you guys can help me solve. 我正在将键绑定到Java应用程序中的事件,但是,我遇到了一个非常奇怪的错误,希望大家能帮助我解决。

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

Basically all keys except LEFT/RIGHT are caught and binded. 基本上,除LEFT / RIGHT外的所有键都被捕获并绑定。 If I replace this 如果我更换这个

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

with KeyStroke p2_left = KeyStroke.getKeyStroke("UP"); 使用KeyStroke p2_left = KeyStroke.getKeyStroke(“ UP”);

while commenting out p2_up, p2_left works as expected. 在注释掉p2_up时,p2_left可以按预期工作。

Any clue? 有什么线索吗?

I didn't down vote it but I would have. 我没有否决它,但我会的。 The reason is 90% of the code you posted is useless in providing information about your problem. 原因是您发布的代码中有90%无法提供有关您的问题的信息。 We don't know the context of how you are using the code or what component you are adding the binding to... 我们不知道您如何使用代码或要将绑定添加到哪个组件的上下文...

I've used "up, down, left, right" with no problem when doing key bindings. 在进行键绑定时,我使用“上,下,左,右”没有问题。

If you need more help post your SSCCE . 如果您需要更多帮助,请发布SSCCE

The most likely explanation is that something is capturing those key strokes. 最可能的解释是某些东西正在捕获这些按键。 The most likely candidates are a scroll pane or an input text box of some kind. 最可能的候选对象是滚动窗格或某种输入文本框。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM