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中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

SublimeText3漢化版
中文版,非常好用

SublimeText3 Linux新版
SublimeText3 Linux最新版

禪工作室 13.0.1
強大的PHP整合開發環境