package org.expeditee.gio.input; import org.expeditee.core.Line; public class KBMInputEvent extends InputEvent { public enum EventType { MOUSE_BUTTON_DOWN, MOUSE_BUTTON_UP, MOUSE_MOVE, MOUSE_WHEEL_SCROLL, KEY_DOWN, KEY_UP, CHAR_TYPED } /** * Enumerates the mouse buttons of interest to Expeditee. */ public enum MouseButton { LEFT, MIDDLE, RIGHT } /** * Enumerates the keyboard keys of interest to Expeditee. */ public enum Key { SHIFT,CTRL,ALT, ESC, F1,F2,F3,F4,F5,F6,F7,F8,F9,F10,F11,F12, UP_ARROW,DOWN_ARROW,LEFT_ARROW,RIGHT_ARROW, HOME,END, DELETE, PAGE_UP,PAGE_DOWN, BACKSPACE, TAB, ENTER, A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z } private EventType _eventType; private Object _eventData; public KBMInputEvent(EventType type, Object data) throws Exception { super(InputEvent.InputType.KBM); _eventType = type; _eventData = data; checkData(); } public EventType getEventType() { return _eventType; } public Object getData() { return _eventData; } /** Gets the key that went down if this is a key-down event. */ public Key getKeyDown() { if (_eventType == EventType.KEY_DOWN) return (Key) _eventData; return null; } /** Gets the key that went up if this is a key-up event. */ public Key getKeyUp() { if (_eventType == EventType.KEY_UP) return (Key) _eventData; return null; } /** * Gets the name of the key that was pressed/released. * Returns the empty string if this is not a key event. */ public String getKeyText() { Key key = getKeyDown(); if (key == null) key = getKeyUp(); if (key == null) return ""; return key.name(); } /** Gets the character that was typed if this is a CHAR_TYPED event. */ public Character getCharTyped() { if (_eventType == EventType.CHAR_TYPED) return (Character) _eventData; return null; } /** Gets the mouse button that went down if this is a mouse-button-down event. */ public MouseButton getMouseButtonDown() { if (_eventType == EventType.MOUSE_BUTTON_DOWN) return (MouseButton) _eventData; return null; } /** Gets the mouse button that went up if this is a mouse-button-up event. */ public MouseButton getMouseButtonUp() { if (_eventType == EventType.MOUSE_BUTTON_UP) return (MouseButton) _eventData; return null; } /** Gets the position of the mouse if this is a mouse-move event. */ public Line getMouseMove() { if (_eventType == EventType.MOUSE_MOVE) return (Line) _eventData; return null; } private void checkData() throws Exception { if (_eventType == null) throw new Exception("No event type specified"); if (_eventData == null) throw new Exception("No event data specified"); switch (_eventType) { case MOUSE_BUTTON_DOWN: case MOUSE_BUTTON_UP: if (!(_eventData instanceof MouseButton)) throw new Exception("MouseButton data expected"); break; case KEY_DOWN: case KEY_UP: if (!(_eventData instanceof Key)) throw new Exception("Key data expected"); break; case CHAR_TYPED: if (!(_eventData instanceof Character)) throw new Exception("Character data expected"); break; case MOUSE_MOVE: if (!(_eventData instanceof Line)) throw new Exception("Line data expected"); break; case MOUSE_WHEEL_SCROLL: if (!(_eventData instanceof Integer)) throw new Exception("Integer data expected"); break; } } @Override public String toString() { return _eventType.toString() + " event : Data = " + _eventData.toString(); } }