-
Notifications
You must be signed in to change notification settings - Fork 0
/
KeyListener.java
32 lines (29 loc) · 925 Bytes
/
KeyListener.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
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.HashMap;
/**
* The KeyListener checks whether a key is pressed currently
*
* @author Manuel Kienlein
*/
public class KeyListener
{
private static HashMap<String, Long> register = new HashMap<String, Long>();
public static final int DELAY = 500;
public static boolean isKeyTyped(String keyCode){
if(Greenfoot.isKeyDown(keyCode)){
if(register.containsKey(keyCode)){
if(register.get(keyCode) + DELAY < System.currentTimeMillis()){
logTyped(keyCode);
return true;
}
}else{
logTyped(keyCode);
return true;
}
}
return false;
}
private static void logTyped(String keyCode){
register.put(keyCode, System.currentTimeMillis());
}
}