回答編集履歴
2
追記
test
CHANGED
@@ -57,3 +57,35 @@
|
|
57
57
|
```
|
58
58
|
|
59
59
|
これをsetOnKeyPressed、setOnKeyReleasedそれぞれでKeyManager#handleEventを呼んでやり、Timeline内でisPressed(KeyCode.UP) & isPressed(KeyCode.LEFT)としてやれば「左上」を押していると認識できます。ただし、よくある問題としてフォーカスが外れたときに押しっぱなしになったりするので、そのへんの対処は自分で考えてみてください。
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
```Java
|
64
|
+
|
65
|
+
public static KeyManager keyManager = new KeyManager;
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
// 中略
|
70
|
+
|
71
|
+
root.setOnKeyPressed(event -> keyManager.handleEvent(event));
|
72
|
+
|
73
|
+
root.setOnKeyReleased(event -> keyManager.handleEvent(event));
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
// Timelineの処理の中
|
78
|
+
|
79
|
+
if(keyManager.isPressed(KeyCode.UP)) {
|
80
|
+
|
81
|
+
//上を押されている
|
82
|
+
|
83
|
+
}
|
84
|
+
|
85
|
+
if(keyManager.isPressed(KeyCode.LEFT)) {
|
86
|
+
|
87
|
+
//左を押されている
|
88
|
+
|
89
|
+
}
|
90
|
+
|
91
|
+
```
|
1
追記
test
CHANGED
@@ -13,3 +13,47 @@
|
|
13
13
|
* 別スレッドで移動計算を行います。
|
14
14
|
|
15
15
|
* Platform.runLaterを使い、コントロールの座標を計算結果に合わせて移動させます
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
#キー管理クラスのサンプル
|
20
|
+
|
21
|
+
なにもテストしてませんので動作は保証しかねますが、やろうとすることはこういうことだと思います。
|
22
|
+
|
23
|
+
```Java
|
24
|
+
|
25
|
+
public class KeyManager {
|
26
|
+
|
27
|
+
private Map<KeyCode, boolean> map = new HashMap<>();
|
28
|
+
|
29
|
+
public void handleEvent(KeyEvent e) {
|
30
|
+
|
31
|
+
switch(e.getEventType()) {
|
32
|
+
|
33
|
+
case KeyEvent.KEY_PRESSED:
|
34
|
+
|
35
|
+
map.put(e.getCode(), false);
|
36
|
+
|
37
|
+
break;
|
38
|
+
|
39
|
+
case KeyEvent.KEY_RELEASED:
|
40
|
+
|
41
|
+
map.put(e.getCode(), true);
|
42
|
+
|
43
|
+
break;
|
44
|
+
|
45
|
+
}
|
46
|
+
|
47
|
+
}
|
48
|
+
|
49
|
+
public boolean isPressed(KeyCode code) {
|
50
|
+
|
51
|
+
return map.containsKey(code)?map.get(code):false;
|
52
|
+
|
53
|
+
}
|
54
|
+
|
55
|
+
}
|
56
|
+
|
57
|
+
```
|
58
|
+
|
59
|
+
これをsetOnKeyPressed、setOnKeyReleasedそれぞれでKeyManager#handleEventを呼んでやり、Timeline内でisPressed(KeyCode.UP) & isPressed(KeyCode.LEFT)としてやれば「左上」を押していると認識できます。ただし、よくある問題としてフォーカスが外れたときに押しっぱなしになったりするので、そのへんの対処は自分で考えてみてください。
|