/*
 */

import java.awt.*;
import java.awt.event.*;

public class KeyHandler
	implements KeyListener
{
	private void sendMakeCode(int makeCode)
	{
		System.out.println("Keyboard: MakeCode(" + (byte)makeCode
			+ ")");
	}

	private void sendBreakCode(int makeCode)
	{
		System.out.println("Keyboard: BreakCode("
			+ (byte)(makeCode + (byte)128) + ")");
	}

	private int getMakeCode(int keyCode)
	{
		switch (keyCode) {
		case KeyEvent.VK_ALT:
			return (56);

		case KeyEvent.VK_ALT_GRAPH:
			return (56);

		case KeyEvent.VK_CONTROL:
			return (29);

		case KeyEvent.VK_DELETE:
			return (83);

		case KeyEvent.VK_DOWN:
			return (80);

		case KeyEvent.VK_END:
			return (79);

		case KeyEvent.VK_ENTER:
			return (28);

		case KeyEvent.VK_ESCAPE:
			return (1);

		case KeyEvent.VK_F1:
			return (59);

		case KeyEvent.VK_F2:
			return (60);

		case KeyEvent.VK_F3:
			return (61);

		case KeyEvent.VK_F4:
			return (62);

		case KeyEvent.VK_F5:
			return (63);

		case KeyEvent.VK_F6:
			return (64);

		case KeyEvent.VK_F7:
			return (65);

		case KeyEvent.VK_F8:
			return (66);

		case KeyEvent.VK_F9:
			return (67);

		case KeyEvent.VK_F10:
			return (68);

		case KeyEvent.VK_F11:
			return (87);

		case KeyEvent.VK_F12:
			return (88);

		case KeyEvent.VK_LEFT:
			return (75);

		case KeyEvent.VK_HOME:
			return (71);

		case KeyEvent.VK_INSERT:
			return (82);

		case KeyEvent.VK_PAGE_DOWN:
			return (81);

		case KeyEvent.VK_PAGE_UP:
			return (73);

		case KeyEvent.VK_PRINTSCREEN:
			return (55);

		case KeyEvent.VK_RIGHT:
			return (77);

		case KeyEvent.VK_SHIFT:
			return (42);

		case KeyEvent.VK_SPACE:
			return (57);

		case KeyEvent.VK_TAB:
			return (15);

		case KeyEvent.VK_UP:
			return (72);

		}
		return (0);
	}

	private int getMakeCode(char keyChar)
	{
		switch (keyChar) {
		case 'A': case 'a':
			return (30);

		case 'B': case 'b':
			return (48);

		case 'C': case 'c':
			return (46);

		case 'D': case 'd':
			return (32);

		case 'E': case 'e':
			return (18);

		case 'F': case 'f':
			return (33);

		case 'G': case 'g':
			return (34);

		case 'H': case 'h':
			return (35);

		case 'I': case 'i':
			return (23);

		case 'J': case 'j':
			return (36);

		case 'K': case 'k':
			return (37);

		case 'L': case 'l':
			return (38);

		case 'M': case 'm':
			return (50);

		case 'N': case 'n':
			return (49);

		case 'O': case 'o':
			return (24);

		case 'P': case 'p':
			return (25);

		case 'Q': case 'q': case '@':
			return (16);

		case 'R': case 'r':
			return (19);

		case 'S': case 's':
			return (31);

		case 'T': case 't':
			return (20);

		case 'U': case 'u':
			return (22);

		case 'V': case 'v':
			return (47);

		case 'W': case 'w':
			return (17);

		case 'X': case 'x':
			return (45);

		case 'Y': case 'y':
			return (44);

		case 'Z': case 'z':
			return (21);

		case '1': case '!':
			return (2);

		case '2': case '\"':
			return (3);

		case '3': case '§':
			return (4);

		case '4': case '$':
			return (5);

		case '5': case '%':
			return (6);

		case '6': case '&':
			return (7);

		case '7': case '/': case '{':
			return (8);

		case '8': case '(': case '[':
			return (9);

		case '9': case ')': case ']':
			return (10);

		case '0': case '=': case '}':
			return (11);

		case '?': case '\\': // XXX
			return (12);

		case '`': case '´': // XXX
			return (13);

		}

		return (0);
	}

	public void keyPressed(KeyEvent event)
	{
		int makeCode = getMakeCode(event.getKeyCode());

		if (makeCode != 0)
			sendMakeCode(makeCode);
	}

	public void keyReleased(KeyEvent event)
	{
		int makeCode = getMakeCode(event.getKeyCode());

		if (makeCode != 0)
			sendBreakCode(makeCode);
	}

	public void keyTyped(KeyEvent event)
	{
		int makeCode = getMakeCode(event.getKeyChar());

		if (makeCode != 0) {
			sendMakeCode(makeCode);
			sendBreakCode(makeCode);
		}
	}
}

