은 사용자 유형 N+J
경우에, 나는 NJ
를 인쇄 할.
N
및 J
가 동시에 다른 이후에 도착 한 KeyEvent
가 발생합니다 눌러 시도. 한 가지 방법은 one과 비슷한 enum Key
을 만드는 것입니다. EnumSet
을 사용하여 Set current
을 만듭니다. KEY_PRESSED
이벤트가 도착하면 current
을 업데이트하여 현재 누른 키와 새 키를 포함하십시오. KEY_RELEASED
이벤트가 도착하면 current
을 업데이트하여 새 이벤트를 제외하십시오. current.equals()
메서드를 사용하면 게임에서 사용되는 미리 정의 된 키 상태와 비교할 수 있습니다. EnumSet
인스턴스는 불변이지만 합리적인 수의 키에 대해서는 효율적으로 작습니다.
class MyDispatcher implements KeyEventDispatcher {
ArrayList<String>typedKeys = new ArrayList<String>();
public boolean dispatchKeyEvent(KeyEvent e) {
if (e.getID() == KeyEvent.KEY_PRESSED)
typedKeys.add(""+e.getKeyChar());
if (e.getID() == KeyEvent.KEY_RELEASED) {
String str = typedKeys+"";
System.out.println(str.substring(1,str.length()-1).replaceAll(", ",""));
typedKeys.clear();
}
return false;
}
}
이 무엇을 달성하고자 않습니다 – kleopatra
사용자 유형 N + J 인쇄를 원할 경우 NJ – user1610362