aboutsummaryrefslogtreecommitdiffstats
path: root/src/ru/olamedia/input/Keyboard.java
blob: 6a9ecbd251cee9d96a05abef8fd2437775a4b631 (plain)
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
62
63
64
package ru.olamedia.input;

import java.awt.event.KeyEvent;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.BidiMap;
import org.apache.commons.collections.bidimap.DualHashBidiMap;

public class Keyboard implements com.jogamp.newt.event.KeyListener {
	public static Keyboard instance = new Keyboard();
	private static boolean[] downState = new boolean[256];
	private static BidiMap names = new DualHashBidiMap();

	public static void setName(String name, int keyCode) {
		names.put(name, keyCode);
	}

	public static boolean isKeyDown(int keyCode) {
		return downState[keyCode];
	}

	public static boolean isKeyDown(String name) {
		if (names.containsKey(name)) {
			return downState[((Integer) names.get(name)).intValue()];
		}
		return false;
	}

	private static List<ru.olamedia.input.KeyListener> listeners = new ArrayList<ru.olamedia.input.KeyListener>();

	public static void attach(ru.olamedia.input.KeyListener l) {
		listeners.add(l);
	}

	@Override
	public void keyPressed(com.jogamp.newt.event.KeyEvent e) {
		downState[e.getKeyCode()] = true;
		if (names.containsValue(e.getKeyCode())) {
			String name = (String) names.getKey(e.getKeyCode());
			for (ru.olamedia.input.KeyListener l : listeners) {
				l.onKeyPressed(name, e);
			}
		}
	}

	@Override
	public void keyReleased(com.jogamp.newt.event.KeyEvent e) {
		downState[e.getKeyCode()] = false;
		if (names.containsValue(e.getKeyCode())) {
			String name = (String) names.getKey(e.getKeyCode());
			for (ru.olamedia.input.KeyListener l : listeners) {
				l.onKeyReleased(name, e);
			}
		}
	}

	@Override
	public void keyTyped(com.jogamp.newt.event.KeyEvent arg0) {
		// TODO Auto-generated method stub

	}
}