📄 keyeventstate.java
字号:
/* Copyright (C) 2001, 2008 United States Government as represented bythe Administrator of the National Aeronautics and Space Administration.All Rights Reserved.*/package gov.nasa.worldwind.awt;import java.awt.event.*;import java.util.*;/** * @author dcollins * @version $Id: KeyEventState.java 8595 2009-01-26 20:37:08Z dcollins $ */public class KeyEventState implements KeyListener{ protected static class InputState { private int eventType; private int keyOrButtonCode; private long timestamp; public InputState(int eventType, int keyOrButtonCode, long timestamp) { this.eventType = eventType; this.keyOrButtonCode = keyOrButtonCode; this.timestamp = timestamp; } public int getEventType() { return this.eventType; } public int getKeyOrButtonCode() { return this.keyOrButtonCode; } public long getTimestamp() { return this.timestamp; } } private Map<Object, InputState> keyStateMap = new HashMap<Object, InputState>(); private int modifiers; private int modifiersEx; public KeyEventState() { } public boolean isKeyDown(int keyCode) { InputState state = this.getKeyState(keyCode); return state != null && state.getEventType() == KeyEvent.KEY_PRESSED; } public int getModifiers() { return this.modifiers; } public int getModifiersEx() { return this.modifiersEx; } public void clearKeyState() { this.keyStateMap.clear(); this.modifiers = 0; this.modifiersEx = 0; } public void keyTyped(KeyEvent e) { } public void keyPressed(KeyEvent e) { this.onKeyEvent(e, KeyEvent.KEY_PRESSED); } public void keyReleased(KeyEvent e) { this.onKeyEvent(e, KeyEvent.KEY_RELEASED); } protected void onKeyEvent(KeyEvent e, int eventType) { if (e == null) return; long timestamp = this.getTimeStamp(e, eventType, this.keyStateMap.get(e.getKeyCode())); this.setKeyState(e.getKeyCode(), new InputState(eventType, e.getKeyCode(), timestamp)); this.setModifiers(e.getModifiers()); this.setModifiersEx(e.getModifiersEx()); } protected InputState getKeyState(int keyCode) { return this.keyStateMap.get(keyCode); } protected void setKeyState(int keyCode, InputState state) { this.keyStateMap.put(keyCode, state); } protected void setModifiers(int modifiers) { this.modifiers = modifiers; } protected void setModifiersEx(int modifiersEx) { this.modifiersEx = modifiersEx; } protected long getTimeStamp(InputEvent e, int eventType, InputState currentState) { // If the current state for this input event type exists and is not null, then keep the current timestamp. if (currentState != null && currentState.getEventType() == eventType) return currentState.getTimestamp(); // Otherwise return the InputEvent's timestamp. return e.getWhen(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -