php小编小新今天来给大家介绍如何从actionmap中获取key。在编写php程序时,我们经常会使用到关联数组(也称为字典),而actionmap就是一种常见的关联数组。通过使用key来获取对应的值,我们可以实现更灵活的数据处理和逻辑控制。下面我们将详细介绍从actionmap中获取key的方法,帮助大家更好地理解和应用这一技巧。
我想知道如何从操作映射中的 AbstractAction 获取关键代码。
for (int key = 37; key <= 122; key++) { this.key = key; if(!KeyEvent.getKeyText(key).contains("Unknown keyCode")) { if(KeyInputManager.getInputManager().getOrDefault(key, null) == null) KeyInputManager.getInputManager().put(key, true); component.getActionMap().put(KeyEvent.getKeyText(key), new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { int keyCode = // TODO: GET KEY CODE HERE KeyInputManager.getInputManager().replace(keyCode, true); } }); component.getInputMap().put(KeyStroke.getKeyStroke(key, 0, false), KeyEvent.getKeyText(key)); } }
我试图将所有按键的状态(按下或未按下)存储在HashMap中,该KeyInputManager是通过getInputManager访问的。要检查是否按下了某个键,我们可以使用 KeyInputManager.getKeyDown(keyCode) 这是 KeyInputManager 的代码
public class KeyInputManager { private static final HashMap<Integer, Boolean> keysDown = new HashMap<Integer, Boolean>(); public static final boolean keyDown(int keyCode) { return keysDown.getOrDefault(keyCode, false); } public static HashMap<Integer, Boolean> getInputManager() { return keysDown; } }
试试这个:
int keyCode = (int) e.getActionCommand().charAt(0);
ActionEvent 的操作命令基本上包含键,并且由于您使用的是单字符键,因此采用第一个字符应该为您提供整数键代码。
以上就是如何从ActionMap中获取key的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号