changeset 111:abfe54ebae15

fix key event, patch by yonibear
author spq@33b003aa-7bff-0310-803a-e67f0ece8222
date Sat, 26 Jul 2008 17:35:02 +0000
parents 68534083e1a1
children 98541d3b9220
files engine/core/eventchannel/eventmanager.cpp engine/core/eventchannel/key/ec_key.h engine/core/eventchannel/key/ec_keyevent.h
diffstat 3 files changed, 279 insertions(+), 188 deletions(-) [+]
line wrap: on
line diff
--- a/engine/core/eventchannel/eventmanager.cpp	Sat Jul 26 17:33:44 2008 +0000
+++ b/engine/core/eventchannel/eventmanager.cpp	Sat Jul 26 17:35:02 2008 +0000
@@ -112,99 +112,14 @@
 		} else {
 			throw EventException("Invalid event type in fillKeyEvent");
 		}
-		keyevt.setShiftPressed(sdlevt.key.keysym.mod & KMOD_SHIFT);
-		keyevt.setControlPressed(sdlevt.key.keysym.mod & KMOD_CTRL);
-		keyevt.setAltPressed(sdlevt.key.keysym.mod & KMOD_ALT);
-		keyevt.setMetaPressed(sdlevt.key.keysym.mod & KMOD_META);
-		keyevt.setNumericPad(sdlevt.key.keysym.sym >= SDLK_KP0
-							&& sdlevt.key.keysym.sym <= SDLK_KP_EQUALS);
-
 		SDL_keysym keysym = sdlevt.key.keysym;
-		Key::KeyType value = Key::INVALID_KEY;
-		std::string repr("");
-
-		if (keysym.unicode < 255) {
-			value = static_cast<Key::KeyType>(keysym.unicode);
-			repr.push_back(value);
-		}
 
-		#define MAP_KEY(_orig, _new) case _orig: value = _new; repr = #_new; break;
-		switch (keysym.sym) {
-			MAP_KEY(SDLK_TAB, Key::TAB);
-			MAP_KEY(SDLK_LALT, Key::LEFT_ALT);
-			MAP_KEY(SDLK_RALT, Key::RIGHT_ALT);
-			MAP_KEY(SDLK_LSHIFT, Key::LEFT_SHIFT);
-			MAP_KEY(SDLK_RSHIFT, Key::RIGHT_SHIFT);
-			MAP_KEY(SDLK_LCTRL, Key::LEFT_CONTROL);
-			MAP_KEY(SDLK_RCTRL, Key::RIGHT_CONTROL);
-			MAP_KEY(SDLK_BACKSPACE, Key::BACKSPACE);
-			MAP_KEY(SDLK_PAUSE, Key::PAUSE);
-			MAP_KEY(SDLK_ESCAPE, Key::ESCAPE);
-			MAP_KEY(SDLK_DELETE, Key::DELETE_KEY);
-			MAP_KEY(SDLK_INSERT, Key::INSERT);
-			MAP_KEY(SDLK_HOME, Key::HOME);
-			MAP_KEY(SDLK_END, Key::END);
-			MAP_KEY(SDLK_PAGEUP, Key::PAGE_UP);
-			MAP_KEY(SDLK_PRINT, Key::PRINT_SCREEN);
-			MAP_KEY(SDLK_PAGEDOWN, Key::PAGE_DOWN);
-			MAP_KEY(SDLK_F1, Key::F1);
-			MAP_KEY(SDLK_F2, Key::F2);
-			MAP_KEY(SDLK_F3, Key::F3);
-			MAP_KEY(SDLK_F4, Key::F4);
-			MAP_KEY(SDLK_F5, Key::F5);
-			MAP_KEY(SDLK_F6, Key::F6);
-			MAP_KEY(SDLK_F7, Key::F7);
-			MAP_KEY(SDLK_F8, Key::F8);
-			MAP_KEY(SDLK_F9, Key::F9);
-			MAP_KEY(SDLK_F10, Key::F10);
-			MAP_KEY(SDLK_F11, Key::F11);
-			MAP_KEY(SDLK_F12, Key::F12);
-			MAP_KEY(SDLK_F13, Key::F13);
-			MAP_KEY(SDLK_F14, Key::F14);
-			MAP_KEY(SDLK_F15, Key::F15);
-			MAP_KEY(SDLK_NUMLOCK, Key::NUM_LOCK);
-			MAP_KEY(SDLK_CAPSLOCK, Key::CAPS_LOCK);
-			MAP_KEY(SDLK_SCROLLOCK, Key::SCROLL_LOCK);
-			MAP_KEY(SDLK_RMETA, Key::RIGHT_META);
-			MAP_KEY(SDLK_LMETA, Key::LEFT_META);
-			MAP_KEY(SDLK_LSUPER, Key::LEFT_SUPER);
-			MAP_KEY(SDLK_RSUPER, Key::RIGHT_SUPER);
-			MAP_KEY(SDLK_MODE, Key::ALT_GR);
-			MAP_KEY(SDLK_UP, Key::UP);
-			MAP_KEY(SDLK_DOWN, Key::DOWN);
-			MAP_KEY(SDLK_LEFT, Key::LEFT);
-			MAP_KEY(SDLK_RIGHT, Key::RIGHT);
-			MAP_KEY(SDLK_RETURN, Key::ENTER);
-			MAP_KEY(SDLK_KP_ENTER, Key::ENTER);
-		case SDLK_SPACE:
-			// Special characters like ~ (tilde) ends up with the keysym.sym SDLK_SPACE which
-			// without this check would be lost. The check is only valid on key down events in SDL.
-			if (sdlevt.type == SDL_KEYUP || keysym.unicode == ' ') {
-				value = Key::SPACE;
-			}
-			break;
-		default:
-			break;
-		}
-
-		if (!(keysym.mod & KMOD_NUM)) {
-			switch (keysym.sym) {
-			MAP_KEY(SDLK_KP0, Key::INSERT);
-			MAP_KEY(SDLK_KP1, Key::END);
-			MAP_KEY(SDLK_KP2, Key::DOWN);
-			MAP_KEY(SDLK_KP3, Key::PAGE_DOWN);
-			MAP_KEY(SDLK_KP4, Key::LEFT);
-			MAP_KEY(SDLK_KP5, Key::INVALID_KEY);
-			MAP_KEY(SDLK_KP6, Key::RIGHT);
-			MAP_KEY(SDLK_KP7, Key::HOME);
-			MAP_KEY(SDLK_KP8, Key::UP);
-			MAP_KEY(SDLK_KP9, Key::PAGE_UP);
-			default:
-				break;
-			}
-		}
-
-		keyevt.setKey(Key(value, repr));
+		keyevt.setShiftPressed(keysym.mod & KMOD_SHIFT);
+		keyevt.setControlPressed(keysym.mod & KMOD_CTRL);
+		keyevt.setAltPressed(keysym.mod & KMOD_ALT);
+		keyevt.setMetaPressed(keysym.mod & KMOD_META);
+		keyevt.setNumericPad(keysym.sym >= SDLK_KP0 && keysym.sym <= SDLK_KP_EQUALS);
+		keyevt.setKey(Key(static_cast<Key::KeyType>(keysym.sym), keysym.unicode));
 	}
 
 	template<typename T>
--- a/engine/core/eventchannel/key/ec_key.h	Sat Jul 26 17:33:44 2008 +0000
+++ b/engine/core/eventchannel/key/ec_key.h	Sat Jul 26 17:35:02 2008 +0000
@@ -27,6 +27,7 @@
 
 // 3rd party library includes
 //
+#include <SDL.h>
 
 // FIFE includes
 // These includes are split up in two parts, separated by one empty line
@@ -39,108 +40,283 @@
 	 */
 	class Key {
 	public:
-		/** Constructor
-		 * @param value value of the key
-		 * @param repr string represntation of the key
-		*/
-		Key(int value, const std::string& repr=""): 
-			m_value(value),
-			m_repr(repr) {}
-
-		/** Destructor.
-		 */
-		virtual ~Key() {}
-
-		/** Checks whether a key is a character.
-		 */
-		bool isCharacter() const {
-			return (m_value >= 32 && m_value <= 126)
-				|| (m_value >= 162 && m_value <= 255)
-				|| (m_value == 9);
-		}
-
-		/** Gets the value of the key. 
-		 */
-		bool isNumber() const  {
-			return m_value >= 48 && m_value <= 57;
-		}
-
-		/** Checks whether a key is a letter. 
-		 */
-		bool isLetter() const  {
-			return (((m_value >= 65 && m_value <= 90)
-				|| (m_value >= 97 && m_value <= 122)
-				|| (m_value >= 192 && m_value <= 255))
-				&& (m_value != 215) && (m_value != 247));
-		}
-		
-		/** Gets the value of the key. 
-		 */
-		int getValue() const {
-			return m_value;
-		}
-
-		/** Gets the string representation of the key. 
-		 */
-		const std::string& getAsString() const {
-			return m_repr;
-		}
-
 		/** Codes for different keys
 		 */
 		enum KeyType {
-			INVALID_KEY        = -1,
-			SPACE              = ' ',
-			TAB                = '\t',
-			ENTER              = '\n',
-			LEFT_ALT           = 1000,
-			RIGHT_ALT,
-			LEFT_SHIFT,
-			RIGHT_SHIFT,
-			LEFT_CONTROL,
-			RIGHT_CONTROL,
-			LEFT_META,
-			RIGHT_META,
-			LEFT_SUPER,
-			RIGHT_SUPER,
-			INSERT,
-			HOME,
-			PAGE_UP,
-			DELETE_KEY,
-			END,
-			PAGE_DOWN,
-			ESCAPE,
-			CAPS_LOCK,
-			BACKSPACE,
-			F1,
-			F2,
-			F3,
-			F4,
-			F5,
-			F6,
-			F7,
-			F8,
-			F9,
-			F10,
-			F11,
-			F12,
-			F13,
-			F14,
-			F15,
-			PRINT_SCREEN,
-			SCROLL_LOCK,
-			PAUSE,
-			NUM_LOCK,
-			ALT_GR,
-			LEFT,
-			RIGHT,
-			UP,
-			DOWN
+			INVALID_KEY   = -1,
+			BACKSPACE     = SDLK_BACKSPACE,
+			TAB           = SDLK_TAB,
+			CLEAR         = SDLK_CLEAR,
+			ENTER         = SDLK_RETURN,
+			PAUSE         = SDLK_PAUSE,
+			ESCAPE        = SDLK_ESCAPE,
+			SPACE         = SDLK_SPACE,
+			EXCLAIM       = SDLK_EXCLAIM,
+			QUOTEDBL      = SDLK_QUOTEDBL,
+			HASH          = SDLK_HASH,
+			DOLLAR        = SDLK_DOLLAR,
+			AMPERSAND     = SDLK_AMPERSAND,
+			QUOTE         = SDLK_QUOTE,
+			LEFTPAREN     = SDLK_LEFTPAREN,
+			RIGHTPAREN    = SDLK_RIGHTPAREN,
+			ASTERISK      = SDLK_ASTERISK,
+			PLUS          = SDLK_PLUS,
+			COMMA         = SDLK_COMMA,
+			MINUS         = SDLK_MINUS,
+			PERIOD        = SDLK_PERIOD,
+			SLASH         = SDLK_SLASH,
+			NUM_0         = SDLK_0,
+			NUM_1         = SDLK_1,
+			NUM_2         = SDLK_2,
+			NUM_3         = SDLK_3,
+			NUM_4         = SDLK_4,
+			NUM_5         = SDLK_5,
+			NUM_6         = SDLK_6,
+			NUM_7         = SDLK_7,
+			NUM_8         = SDLK_8,
+			NUM_9         = SDLK_9,
+			COLON         = SDLK_COLON,
+			SEMICOLON     = SDLK_SEMICOLON,
+			LESS          = SDLK_LESS,
+			EQUALS        = SDLK_EQUALS,
+			GREATER       = SDLK_GREATER,
+			QUESTION      = SDLK_QUESTION,
+			AT            = SDLK_AT,
+			LEFTBRACKET   = SDLK_LEFTBRACKET,
+			BACKSLASH     = SDLK_BACKSLASH,
+			RIGHTBRACKET  = SDLK_RIGHTBRACKET,
+			CARET         = SDLK_CARET,
+			UNDERSCORE    = SDLK_UNDERSCORE,
+			BACKQUOTE     = SDLK_BACKQUOTE,
+			A             = SDLK_a,
+			B             = SDLK_b,
+			C             = SDLK_c,
+			D             = SDLK_d,
+			E             = SDLK_e,
+			F             = SDLK_f,
+			G             = SDLK_g,
+			H             = SDLK_h,
+			I             = SDLK_i,
+			J             = SDLK_j,
+			K             = SDLK_k,
+			L             = SDLK_l,
+			M             = SDLK_m,
+			N             = SDLK_n,
+			O             = SDLK_o,
+			P             = SDLK_p,
+			Q             = SDLK_q,
+			R             = SDLK_r,
+			S             = SDLK_s,
+			T             = SDLK_t,
+			U             = SDLK_u,
+			V             = SDLK_v,
+			W             = SDLK_w,
+			X             = SDLK_x,
+			Y             = SDLK_y,
+			Z             = SDLK_z,
+			DELETE        = SDLK_DELETE,
+			WORLD_0       = SDLK_WORLD_0,
+			WORLD_1       = SDLK_WORLD_1,
+			WORLD_2       = SDLK_WORLD_2,
+			WORLD_3       = SDLK_WORLD_3,
+			WORLD_4       = SDLK_WORLD_4,
+			WORLD_5       = SDLK_WORLD_5,
+			WORLD_6       = SDLK_WORLD_6,
+			WORLD_7       = SDLK_WORLD_7,
+			WORLD_8       = SDLK_WORLD_8,
+			WORLD_9       = SDLK_WORLD_9,
+			WORLD_10      = SDLK_WORLD_10,
+			WORLD_11      = SDLK_WORLD_11,
+			WORLD_12      = SDLK_WORLD_12,
+			WORLD_13      = SDLK_WORLD_13,
+			WORLD_14      = SDLK_WORLD_14,
+			WORLD_15      = SDLK_WORLD_15,
+			WORLD_16      = SDLK_WORLD_16,
+			WORLD_17      = SDLK_WORLD_17,
+			WORLD_18      = SDLK_WORLD_18,
+			WORLD_19      = SDLK_WORLD_19,
+			WORLD_20      = SDLK_WORLD_20,
+			WORLD_21      = SDLK_WORLD_21,
+			WORLD_22      = SDLK_WORLD_22,
+			WORLD_23      = SDLK_WORLD_23,
+			WORLD_24      = SDLK_WORLD_24,
+			WORLD_25      = SDLK_WORLD_25,
+			WORLD_26      = SDLK_WORLD_26,
+			WORLD_27      = SDLK_WORLD_27,
+			WORLD_28      = SDLK_WORLD_28,
+			WORLD_29      = SDLK_WORLD_29,
+			WORLD_30      = SDLK_WORLD_30,
+			WORLD_31      = SDLK_WORLD_31,
+			WORLD_32      = SDLK_WORLD_32,
+			WORLD_33      = SDLK_WORLD_33,
+			WORLD_34      = SDLK_WORLD_34,
+			WORLD_35      = SDLK_WORLD_35,
+			WORLD_36      = SDLK_WORLD_36,
+			WORLD_37      = SDLK_WORLD_37,
+			WORLD_38      = SDLK_WORLD_38,
+			WORLD_39      = SDLK_WORLD_39,
+			WORLD_40      = SDLK_WORLD_40,
+			WORLD_41      = SDLK_WORLD_41,
+			WORLD_42      = SDLK_WORLD_42,
+			WORLD_43      = SDLK_WORLD_43,
+			WORLD_44      = SDLK_WORLD_44,
+			WORLD_45      = SDLK_WORLD_45,
+			WORLD_46      = SDLK_WORLD_46,
+			WORLD_47      = SDLK_WORLD_47,
+			WORLD_48      = SDLK_WORLD_48,
+			WORLD_49      = SDLK_WORLD_49,
+			WORLD_50      = SDLK_WORLD_50,
+			WORLD_51      = SDLK_WORLD_51,
+			WORLD_52      = SDLK_WORLD_52,
+			WORLD_53      = SDLK_WORLD_53,
+			WORLD_54      = SDLK_WORLD_54,
+			WORLD_55      = SDLK_WORLD_55,
+			WORLD_56      = SDLK_WORLD_56,
+			WORLD_57      = SDLK_WORLD_57,
+			WORLD_58      = SDLK_WORLD_58,
+			WORLD_59      = SDLK_WORLD_59,
+			WORLD_60      = SDLK_WORLD_60,
+			WORLD_61      = SDLK_WORLD_61,
+			WORLD_62      = SDLK_WORLD_62,
+			WORLD_63      = SDLK_WORLD_63,
+			WORLD_64      = SDLK_WORLD_64,
+			WORLD_65      = SDLK_WORLD_65,
+			WORLD_66      = SDLK_WORLD_66,
+			WORLD_67      = SDLK_WORLD_67,
+			WORLD_68      = SDLK_WORLD_68,
+			WORLD_69      = SDLK_WORLD_69,
+			WORLD_70      = SDLK_WORLD_70,
+			WORLD_71      = SDLK_WORLD_71,
+			WORLD_72      = SDLK_WORLD_72,
+			WORLD_73      = SDLK_WORLD_73,
+			WORLD_74      = SDLK_WORLD_74,
+			WORLD_75      = SDLK_WORLD_75,
+			WORLD_76      = SDLK_WORLD_76,
+			WORLD_77      = SDLK_WORLD_77,
+			WORLD_78      = SDLK_WORLD_78,
+			WORLD_79      = SDLK_WORLD_79,
+			WORLD_80      = SDLK_WORLD_80,
+			WORLD_81      = SDLK_WORLD_81,
+			WORLD_82      = SDLK_WORLD_82,
+			WORLD_83      = SDLK_WORLD_83,
+			WORLD_84      = SDLK_WORLD_84,
+			WORLD_85      = SDLK_WORLD_85,
+			WORLD_86      = SDLK_WORLD_86,
+			WORLD_87      = SDLK_WORLD_87,
+			WORLD_88      = SDLK_WORLD_88,
+			WORLD_89      = SDLK_WORLD_89,
+			WORLD_90      = SDLK_WORLD_90,
+			WORLD_91      = SDLK_WORLD_91,
+			WORLD_92      = SDLK_WORLD_92,
+			WORLD_93      = SDLK_WORLD_93,
+			WORLD_94      = SDLK_WORLD_94,
+			WORLD_95      = SDLK_WORLD_95,
+			KP0           = SDLK_KP0,
+			KP1           = SDLK_KP1,
+			KP2           = SDLK_KP2,
+			KP3           = SDLK_KP3,
+			KP4           = SDLK_KP4,
+			KP5           = SDLK_KP5,
+			KP6           = SDLK_KP6,
+			KP7           = SDLK_KP7,
+			KP8           = SDLK_KP8,
+			KP9           = SDLK_KP9,
+			KP_PERIOD     = SDLK_KP_PERIOD,
+			KP_DIVIDE     = SDLK_KP_DIVIDE,
+			KP_MULTIPLY   = SDLK_KP_MULTIPLY,
+			KP_MINUS      = SDLK_KP_MINUS,
+			KP_PLUS       = SDLK_KP_PLUS,
+			KP_ENTER      = SDLK_KP_ENTER,
+			KP_EQUALS     = SDLK_KP_EQUALS,
+			UP            = SDLK_UP,
+			DOWN          = SDLK_DOWN,
+			RIGHT         = SDLK_RIGHT,
+			LEFT          = SDLK_LEFT,
+			INSERT        = SDLK_INSERT,
+			HOME          = SDLK_HOME,
+			END           = SDLK_END,
+			PAGE_UP       = SDLK_PAGEUP,
+			PAGE_DOWN     = SDLK_PAGEDOWN,
+			F1            = SDLK_F1,
+			F2            = SDLK_F2,
+			F3            = SDLK_F3,
+			F4            = SDLK_F4,
+			F5            = SDLK_F5,
+			F6            = SDLK_F6,
+			F7            = SDLK_F7,
+			F8            = SDLK_F8,
+			F9            = SDLK_F9,
+			F10           = SDLK_F10,
+			F11           = SDLK_F11,
+			F12           = SDLK_F12,
+			F13           = SDLK_F13,
+			F14           = SDLK_F14,
+			F15           = SDLK_F15,
+			NUM_LOCK      = SDLK_NUMLOCK,
+			CAPS_LOCK     = SDLK_CAPSLOCK,
+			SCROLL_LOCK   = SDLK_SCROLLOCK,
+			RIGHT_SHIFT   = SDLK_RSHIFT,
+			LEFT_SHIFT    = SDLK_LSHIFT,
+			RIGHT_CONTROL = SDLK_RCTRL,
+			LEFT_CONTROL  = SDLK_LCTRL,
+			RIGHT_ALT     = SDLK_RALT,
+			LEFT_ALT      = SDLK_LALT,
+			RIGHT_META    = SDLK_RMETA,
+			LEFT_META     = SDLK_LMETA,
+			RIGHT_SUPER   = SDLK_RSUPER,
+			LEFT_SUPER    = SDLK_LSUPER,
+			ALT_GR        = SDLK_MODE,
+			COMPOSE       = SDLK_COMPOSE,
+			HELP          = SDLK_HELP,
+			PRINT_SCREEN  = SDLK_PRINT,
+			SYSREQ        = SDLK_SYSREQ,
+			BREAK         = SDLK_BREAK,
+			MENU          = SDLK_MENU,
+			POWER         = SDLK_POWER,
+			EURO          = SDLK_EURO,
+			UNDO          = SDLK_UNDO
 		};
 
+		/** Constructor
+		 * @param value value of the key
+		 * @param codepoint Unicode representation of the key
+		*/
+		Key(KeyType key = INVALID_KEY, uint32_t codepoint = 0):
+			m_key(key),
+			m_unicode(codepoint) {}
+			
+		/** Destructor.
+		 */
+		virtual ~Key() {}
+		
+		/** Tells whether the Key can be represented as a string.
+		 */
+		bool isPrintable() const {
+			// Unicode does not have control characters, so this should be sufficient
+			return m_unicode != 0;
+		}
+		
+		
+		/** Gets the value of the key. 
+		 */
+		KeyType getValue() const {
+			return m_key;
+		}
+
+		/** Gets the string representation of the key, encoded as UTF-8. 
+		 */
+		std::string getAsString() const {
+			// older sdl versions have the 3rd argument non const, but it is in fact const...
+			char* converted = SDL_iconv_string("UTF-8", "UTF-32",
+				const_cast<char*>(reinterpret_cast<const char*>(&m_unicode)), 4);
+			std::string retval(converted);
+			SDL_free(converted);
+			return retval;
+		}
+
 	private:
-		int m_value;
-		std::string m_repr;
+		KeyType m_key;
+		uint32_t m_unicode;
 	};
 
 } //FIFE
--- a/engine/core/eventchannel/key/ec_keyevent.h	Sat Jul 26 17:33:44 2008 +0000
+++ b/engine/core/eventchannel/key/ec_keyevent.h	Sat Jul 26 17:35:02 2008 +0000
@@ -56,7 +56,7 @@
 			InputEvent(), 
 			m_eventtype(UNKNOWN), 
 			m_isnumericpad(false),
-			m_key(Key(0)) {}
+			m_key(Key()) {}
 
 		/** Destructor.
 		 */