LWJGL Forum

Programming => Lightweight Java Gaming Library => Topic started by: darkmoon on October 09, 2006, 16:25:34

Title: Mouse and Keyboard in applet?
Post by: darkmoon on October 09, 2006, 16:25:34
I'm trying to make my lwjgl game work in an applet, with AWTGLCanvas. But it seems I can't use the Keyboard and Mouse classes. I can't create them because no Display is created.
Am I supposed to use the AWT keyboard and mouse listeners instead?
Title: Mouse and Keyboard in applet?
Post by: darkmoon on October 17, 2006, 14:02:37
Does nobody know anything about this? Or was it a stupid question? :(
I guess I'll have to write wrappers then...
Title: Mouse and Keyboard in applet?
Post by: Evil-Devil on October 17, 2006, 14:31:23
Have you tried the AWT Mouse/Keyboard Listeners? They should work...though...
Title: Mouse and Keyboard in applet?
Post by: Spezi on October 17, 2006, 15:52:52
have a look at this: http://lwjgl.org/forum/viewtopic.php?t=1768
Title: Mouse and Keyboard in applet?
Post by: darkmoon on October 17, 2006, 16:40:36
Thanks. It's a pity there's no way to handle input that works in both an application and an applet. The key codes from Keyboard are different from those in the KeyListener, it's not fun to do the translation...
Title: Mouse and Keyboard in applet?
Post by: darkmoon on October 21, 2006, 10:18:21
Sorry to bump this thread, but I've made wrappers around the lwjgl Mouse and Keyboard classes now, so my code works independent of being run as an application or applet.

In case anyone is interested, here's the key code translation from AWT to Keyboard:


private int translateFromAWT( int aCode ) {
   switch ( aCode ) {
     case KeyEvent.VK_ESCAPE: return Keyboard.KEY_ESCAPE;
     case KeyEvent.VK_1: return Keyboard.KEY_1;
     case KeyEvent.VK_2: return Keyboard.KEY_2;
     case KeyEvent.VK_3: return Keyboard.KEY_3;
     case KeyEvent.VK_4: return Keyboard.KEY_4;
     case KeyEvent.VK_5: return Keyboard.KEY_5;
     case KeyEvent.VK_6: return Keyboard.KEY_6;
     case KeyEvent.VK_7: return Keyboard.KEY_7;
     case KeyEvent.VK_8: return Keyboard.KEY_8;
     case KeyEvent.VK_9: return Keyboard.KEY_9;
     case KeyEvent.VK_0: return Keyboard.KEY_0;
     case KeyEvent.VK_MINUS: return Keyboard.KEY_MINUS;
     case KeyEvent.VK_EQUALS: return Keyboard.KEY_EQUALS;
     case KeyEvent.VK_BACK_SPACE: return Keyboard.KEY_BACK;
     case KeyEvent.VK_TAB: return Keyboard.KEY_TAB;
     case KeyEvent.VK_Q: return Keyboard.KEY_Q;
     case KeyEvent.VK_W: return Keyboard.KEY_W;
     case KeyEvent.VK_E: return Keyboard.KEY_E;
     case KeyEvent.VK_R: return Keyboard.KEY_R;
     case KeyEvent.VK_T: return Keyboard.KEY_T;
     case KeyEvent.VK_Y: return Keyboard.KEY_Y;
     case KeyEvent.VK_U: return Keyboard.KEY_U;
     case KeyEvent.VK_I: return Keyboard.KEY_I;
     case KeyEvent.VK_O: return Keyboard.KEY_O;
     case KeyEvent.VK_P: return Keyboard.KEY_P;
     case KeyEvent.VK_OPEN_BRACKET: return Keyboard.KEY_LBRACKET;
     case KeyEvent.VK_CLOSE_BRACKET: return Keyboard.KEY_RBRACKET;
     case KeyEvent.VK_ENTER: return Keyboard.KEY_RETURN;
     case KeyEvent.VK_CONTROL: return Keyboard.KEY_LCONTROL;
     case KeyEvent.VK_A: return Keyboard.KEY_A;
     case KeyEvent.VK_S: return Keyboard.KEY_S;
     case KeyEvent.VK_D: return Keyboard.KEY_D;
     case KeyEvent.VK_F: return Keyboard.KEY_F;
     case KeyEvent.VK_G: return Keyboard.KEY_G;
     case KeyEvent.VK_H: return Keyboard.KEY_H;
     case KeyEvent.VK_J: return Keyboard.KEY_J;
     case KeyEvent.VK_K: return Keyboard.KEY_K;
     case KeyEvent.VK_L: return Keyboard.KEY_L;
     case KeyEvent.VK_SEMICOLON: return Keyboard.KEY_SEMICOLON;
     case KeyEvent.VK_QUOTE: return Keyboard.KEY_APOSTROPHE;
     case KeyEvent.VK_DEAD_GRAVE: return Keyboard.KEY_GRAVE;
     case KeyEvent.VK_SHIFT: return Keyboard.KEY_LSHIFT;
     case KeyEvent.VK_BACK_SLASH: return Keyboard.KEY_BACKSLASH;
     case KeyEvent.VK_Z: return Keyboard.KEY_Z;
     case KeyEvent.VK_X: return Keyboard.KEY_X;
     case KeyEvent.VK_C: return Keyboard.KEY_C;
     case KeyEvent.VK_V: return Keyboard.KEY_V;
     case KeyEvent.VK_B: return Keyboard.KEY_B;
     case KeyEvent.VK_N: return Keyboard.KEY_N;
     case KeyEvent.VK_M: return Keyboard.KEY_M;
     case KeyEvent.VK_COMMA: return Keyboard.KEY_COMMA;
     case KeyEvent.VK_PERIOD: return Keyboard.KEY_PERIOD;
     case KeyEvent.VK_SLASH: return Keyboard.KEY_SLASH;
     case KeyEvent.VK_MULTIPLY: return Keyboard.KEY_MULTIPLY;
     case KeyEvent.VK_ALT: return Keyboard.KEY_LMENU;
     case KeyEvent.VK_SPACE: return Keyboard.KEY_SPACE;
     case KeyEvent.VK_CAPS_LOCK: return Keyboard.KEY_CAPITAL;
     case KeyEvent.VK_F1: return Keyboard.KEY_F1;
     case KeyEvent.VK_F2: return Keyboard.KEY_F2;
     case KeyEvent.VK_F3: return Keyboard.KEY_F3;
     case KeyEvent.VK_F4: return Keyboard.KEY_F4;
     case KeyEvent.VK_F5: return Keyboard.KEY_F5;
     case KeyEvent.VK_F6: return Keyboard.KEY_F6;
     case KeyEvent.VK_F7: return Keyboard.KEY_F7;
     case KeyEvent.VK_F8: return Keyboard.KEY_F8;
     case KeyEvent.VK_F9: return Keyboard.KEY_F9;
     case KeyEvent.VK_F10: return Keyboard.KEY_F10;
     case KeyEvent.VK_NUM_LOCK: return Keyboard.KEY_NUMLOCK;
     case KeyEvent.VK_SCROLL_LOCK: return Keyboard.KEY_SCROLL;
     case KeyEvent.VK_NUMPAD7: return Keyboard.KEY_NUMPAD7;
     case KeyEvent.VK_NUMPAD8: return Keyboard.KEY_NUMPAD8;
     case KeyEvent.VK_NUMPAD9: return Keyboard.KEY_NUMPAD9;
     case KeyEvent.VK_SUBTRACT: return Keyboard.KEY_SUBTRACT;
     case KeyEvent.VK_NUMPAD4: return Keyboard.KEY_NUMPAD4;
     case KeyEvent.VK_NUMPAD5: return Keyboard.KEY_NUMPAD5;
     case KeyEvent.VK_NUMPAD6: return Keyboard.KEY_NUMPAD6;
     case KeyEvent.VK_ADD: return Keyboard.KEY_ADD;
     case KeyEvent.VK_NUMPAD1: return Keyboard.KEY_NUMPAD1;
     case KeyEvent.VK_NUMPAD2: return Keyboard.KEY_NUMPAD2;
     case KeyEvent.VK_NUMPAD3: return Keyboard.KEY_NUMPAD3;
     case KeyEvent.VK_NUMPAD0: return Keyboard.KEY_NUMPAD0;
     case KeyEvent.VK_DECIMAL: return Keyboard.KEY_DECIMAL;
     case KeyEvent.VK_F11: return Keyboard.KEY_F11;
     case KeyEvent.VK_F12: return Keyboard.KEY_F12;
     case KeyEvent.VK_F13: return Keyboard.KEY_F13;
     case KeyEvent.VK_F14: return Keyboard.KEY_F14;
     case KeyEvent.VK_F15: return Keyboard.KEY_F15;
     case KeyEvent.VK_KANA: return Keyboard.KEY_KANA;
     case KeyEvent.VK_CONVERT: return Keyboard.KEY_CONVERT;
     case KeyEvent.VK_NONCONVERT: return Keyboard.KEY_NOCONVERT;
     case KeyEvent.VK_CIRCUMFLEX: return Keyboard.KEY_CIRCUMFLEX;
     case KeyEvent.VK_AT: return Keyboard.KEY_AT;
     case KeyEvent.VK_COLON: return Keyboard.KEY_COLON;
     case KeyEvent.VK_UNDERSCORE: return Keyboard.KEY_UNDERLINE;
     case KeyEvent.VK_KANJI: return Keyboard.KEY_KANJI;
     case KeyEvent.VK_STOP: return Keyboard.KEY_STOP;
     case KeyEvent.VK_DIVIDE: return Keyboard.KEY_DIVIDE;
     case KeyEvent.VK_PAUSE: return Keyboard.KEY_PAUSE;
     case KeyEvent.VK_HOME: return Keyboard.KEY_HOME;
     case KeyEvent.VK_UP: return Keyboard.KEY_UP;
     case KeyEvent.VK_PAGE_UP: return Keyboard.KEY_PRIOR;
     case KeyEvent.VK_LEFT: return Keyboard.KEY_LEFT;
     case KeyEvent.VK_RIGHT: return Keyboard.KEY_RIGHT;
     case KeyEvent.VK_END: return Keyboard.KEY_END;
     case KeyEvent.VK_DOWN: return Keyboard.KEY_DOWN;
     case KeyEvent.VK_PAGE_DOWN: return Keyboard.KEY_NEXT;
     case KeyEvent.VK_INSERT: return Keyboard.KEY_INSERT;
     case KeyEvent.VK_DELETE: return Keyboard.KEY_DELETE;
     case KeyEvent.VK_META: return Keyboard.KEY_LWIN;
   }
   return Keyboard.KEY_NONE;
 }


Please note that this is not 100% tested, and that some keys are missing (e.g. AWT does not distinguish between left and right shift, ctrl etc kets).
Title: Mouse and Keyboard in applet?
Post by: darkprophet on October 21, 2006, 23:17:46
wouldn't it be faster to have an array lookup instead of a switch statement?

Heh, there goes my over new year vow of not going over the top with optimisations.... :lol:
Title: Mouse and Keyboard in applet?
Post by: Matzon on October 22, 2006, 00:34:56
switches are always faster since its a table lookup
Title: Mouse and Keyboard in applet?
Post by: darkprophet on October 22, 2006, 14:15:27
ah right...didn't know that. Thanks
Title: Mouse and Keyboard in applet?
Post by: Spezi on October 22, 2006, 15:14:29
So using pages of switch statements has the same speed as using hash maps?
Title: Mouse and Keyboard in applet?
Post by: Matzon on October 22, 2006, 17:25:51
no - switch is faster.
A switch is made using a table lookup by the compiler - a hashtable is done using a runtime generated hash for the key entry.