Home > Java > body text

How to get key from ActionMap

WBOY
Release: 2024-02-09 13:15:18
forward
843 people have browsed it

php editor Xiaoxin will introduce to you today how to get the key from ActionMap. When writing PHP programs, we often use associative arrays (also called dictionaries), and ActionMap is a common associative array. By using key to obtain the corresponding value, we can achieve more flexible data processing and logic control. Below we will introduce in detail the method of obtaining keys from ActionMap to help everyone better understand and apply this technique.

Question content

I want to know how to get the key code from AbstractAction in the action map.

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));
    }
}
Copy after login

I am trying to store the state of all keys (pressed or not) in a HashMap that is accessed via getInputManager. To check if a key is pressed we can use KeyInputManager.getKeyDown(keyCode) This is the code for KeyInputManager <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">public class KeyInputManager { private static final HashMap&lt;Integer, Boolean&gt; keysDown = new HashMap&lt;Integer, Boolean&gt;(); public static final boolean keyDown(int keyCode) { return keysDown.getOrDefault(keyCode, false); } public static HashMap&lt;Integer, Boolean&gt; getInputManager() { return keysDown; } }</pre><div class="contentsignin">Copy after login</div></div>

Solution

Try this:

int keyCode = (int) e.getActionCommand().charAt(0);
Copy after login
The action command for

ActionEvent basically contains the key, and since you are using single character keys, taking the first character should give you the integer key code.

The above is the detailed content of How to get key from ActionMap. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template