如何使用 getKeyStroke(String s) 方法?

How to use the getKeyStroke(String s) method?

Link to the JavaDoc

我正在尝试在释放键时获取事件。我试过像文档中所说的那样格式化字符串,但这只会使该键无响应。这是代码的工作行:

getInputMap(IFW).put(KeyStroke.getKeyStroke("DOWN"), MOVE_DOWN);

现在我想让它在我释放密钥时做一些事情,所以尝试按照文档中的格式,我分别尝试了以下几行,但它们都使密钥无响应:

1. getInputMap(IFW).put(KeyStroke.getKeyStroke("DOWN | pressed"), MOVE_DOWN);
2. getInputMap(IFW).put(KeyStroke.getKeyStroke("DOWN | released"), MOVE_DOWN);
3. getInputMap(IFW).put(KeyStroke.getKeyStroke("DOWN | true"), MOVE_DOWN);
4. getInputMap(IFW).put(KeyStroke.getKeyStroke("DOWN | false"), MOVE_DOWN);
5. getInputMap(IFW).put(KeyStroke.getKeyStroke("DOWN, true"), MOVE_DOWN);
6. getInputMap(IFW).put(KeyStroke.getKeyStroke("VK_DOWN, false"), MOVE_DOWN);
7. getInputMap(IFW).put(KeyStroke.getKeyStroke("KeyEvent.VK_DOWN, false"), MOVE_DOWN);

您可能明白了...我不知道如何为此格式化字符串并将 运行 变成一堵砖墙。

  1. 我应该如何为此设置字符串格式?
  2. 我看到还有其他方法,我使用其中一种方法会更好吗?如何?

我觉得 getKeyStroke(char keyChar, boolean onKeyRelease) 是我最好的选择,但它说它已被弃用所以我应该完全避免使用它吗?

Its documentation 好像说的很清楚了。我们可以通过以下方式更正您的示例:

getInputMap(IFW).put(KeyStroke.getKeyStroke("pressed DOWN"), MOVE_DOWN);
getInputMap(IFW).put(KeyStroke.getKeyStroke("released DOWN"), MOVE_DOWN);

注意以下等价于"pressed DOWN"

getInputMap(IFW).put(KeyStroke.getKeyStroke("DOWN"), MOVE_DOWN);