search
HomeJavaHow to get key from ActionMap

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

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

Solution

Try this:

int keyCode = (int) e.getActionCommand().charAt(0);
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!

Statement
This article is reproduced at:stackoverflow. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft