-
Notifications
You must be signed in to change notification settings - Fork 0
/
MoveListener.java
61 lines (42 loc) · 1.42 KB
/
MoveListener.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// custom keyboard listener to watch for arrow keys and then dispath actions
// to server
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MoveListener implements KeyListener {
private boolean movingUp;
private boolean movingDown;
private boolean movingRight;
private boolean movingLeft;
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_UP)
movingUp=true;
if(e.getKeyCode()==KeyEvent.VK_DOWN)
movingDown=true;
if(e.getKeyCode()==KeyEvent.VK_RIGHT)
movingRight=true;
if(e.getKeyCode()==KeyEvent.VK_LEFT)
movingLeft=true;
} //keyPressed
public void keyReleased(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_UP)
movingUp=false;
if(e.getKeyCode()==KeyEvent.VK_DOWN)
movingDown=false;
if(e.getKeyCode()==KeyEvent.VK_RIGHT)
movingRight=false;
if(e.getKeyCode()==KeyEvent.VK_LEFT)
movingLeft=false;
} //keyReleased
public void submit() {
//sends some move action off to server
// *will not work well if two keys are pressed at once
String message=null;
if(movingUp) message=GameActionType.MOVE_UP.toString();
if(movingDown) message=GameActionType.MOVE_DOWN.toString();
if(movingRight) message=GameActionType.MOVE_RIGHT.toString();
if(movingLeft) message=GameActionType.MOVE_LEFT.toString();
if(message!=null) Client.sendMessage(message);
} //submit
public void keyTyped(KeyEvent e) { }
} //class