Leviathan  0.8.0.0
Leviathan game engine
Leviathan::KeyMapping Namespace Reference

Functions

DLLEXPORT int GetCEFButtonFromSdlMouseButton (uint32_t whichbutton)
 
DLLEXPORT int32_t ConvertStringToKeyCode (const std::string &str)
 
DLLEXPORT std::string ConvertKeyCodeToString (const int32_t &code)
 
DLLEXPORT int SDLKeyToX11Key (const SDL_Keysym &key)
 
DLLEXPORT KeyboardCode GdkEventToWindowsKeyCode (int key)
 
DLLEXPORT KeyboardCode GetWindowsKeyCodeWithoutLocation (KeyboardCode key_code)
 
DLLEXPORT int GetControlCharacter (KeyboardCode windows_key_code, bool shift)
 

Function Documentation

◆ ConvertKeyCodeToString()

DLLEXPORT std::string Leviathan::KeyMapping::ConvertKeyCodeToString ( const int32_t &  code)

Definition at line 52 of file KeyMapping.cpp.

53 {
54 
55  return std::string(SDL_GetKeyName(code));
56 }

◆ ConvertStringToKeyCode()

DLLEXPORT int32_t Leviathan::KeyMapping::ConvertStringToKeyCode ( const std::string &  str)

Definition at line 38 of file KeyMapping.cpp.

39 {
40 
41  auto key = SDL_GetKeyFromName(str.c_str());
42 
43  if(key == SDLK_UNKNOWN) {
44 
45  LOG_ERROR("Invalid SDL key name('" + str + "'): " + std::string(SDL_GetError()));
46  return key;
47  }
48 
49  return key;
50 }
#define LOG_ERROR(x)
Definition: Define.h:90

◆ GdkEventToWindowsKeyCode()

DLLEXPORT KeyboardCode Leviathan::KeyMapping::GdkEventToWindowsKeyCode ( int  key)

Definition at line 764 of file KeyMapping.cpp.

765 {
766  static const unsigned int kHardwareCodeToXKKeyval[] = {
767  0, // 0x00:
768  0, // 0x01:
769  0, // 0x02:
770  0, // 0x03:
771  0, // 0x04:
772  0, // 0x05:
773  0, // 0x06:
774  0, // 0x07:
775  0, // 0x08:
776  0, // 0x09: GDK_Escape
777  XK_1, // 0x0A: XK_1
778  XK_2, // 0x0B: XK_2
779  XK_3, // 0x0C: XK_3
780  XK_4, // 0x0D: XK_4
781  XK_5, // 0x0E: XK_5
782  XK_6, // 0x0F: XK_6
783  XK_7, // 0x10: XK_7
784  XK_8, // 0x11: XK_8
785  XK_9, // 0x12: XK_9
786  XK_0, // 0x13: XK_0
787  XK_minus, // 0x14: XK_minus
788  XK_equal, // 0x15: XK_equal
789  0, // 0x16: XK_BackSpace
790  0, // 0x17: XK_Tab
791  XK_q, // 0x18: XK_q
792  XK_w, // 0x19: XK_w
793  XK_e, // 0x1A: XK_e
794  XK_r, // 0x1B: XK_r
795  XK_t, // 0x1C: XK_t
796  XK_y, // 0x1D: XK_y
797  XK_u, // 0x1E: XK_u
798  XK_i, // 0x1F: XK_i
799  XK_o, // 0x20: XK_o
800  XK_p, // 0x21: XK_p
801  XK_bracketleft, // 0x22: XK_bracketleft
802  XK_bracketright, // 0x23: XK_bracketright
803  0, // 0x24: XK_Return
804  0, // 0x25: XK_Control_L
805  XK_a, // 0x26: XK_a
806  XK_s, // 0x27: XK_s
807  XK_d, // 0x28: XK_d
808  XK_f, // 0x29: XK_f
809  XK_g, // 0x2A: XK_g
810  XK_h, // 0x2B: XK_h
811  XK_j, // 0x2C: XK_j
812  XK_k, // 0x2D: XK_k
813  XK_l, // 0x2E: XK_l
814  XK_semicolon, // 0x2F: XK_semicolon
815  XK_apostrophe, // 0x30: XK_apostrophe
816  XK_grave, // 0x31: XK_grave
817  0, // 0x32: XK_Shift_L
818  XK_backslash, // 0x33: XK_backslash
819  XK_z, // 0x34: XK_z
820  XK_x, // 0x35: XK_x
821  XK_c, // 0x36: XK_c
822  XK_v, // 0x37: XK_v
823  XK_b, // 0x38: XK_b
824  XK_n, // 0x39: XK_n
825  XK_m, // 0x3A: XK_m
826  XK_comma, // 0x3B: XK_comma
827  XK_period, // 0x3C: XK_period
828  XK_slash, // 0x3D: XK_slash
829  0, // 0x3E: XK_Shift_R
830  0, // 0x3F:
831  0, // 0x40:
832  0, // 0x41:
833  0, // 0x42:
834  0, // 0x43:
835  0, // 0x44:
836  0, // 0x45:
837  0, // 0x46:
838  0, // 0x47:
839  0, // 0x48:
840  0, // 0x49:
841  0, // 0x4A:
842  0, // 0x4B:
843  0, // 0x4C:
844  0, // 0x4D:
845  0, // 0x4E:
846  0, // 0x4F:
847  0, // 0x50:
848  0, // 0x51:
849  0, // 0x52:
850  0, // 0x53:
851  0, // 0x54:
852  0, // 0x55:
853  0, // 0x56:
854  0, // 0x57:
855  0, // 0x58:
856  0, // 0x59:
857  0, // 0x5A:
858  0, // 0x5B:
859  0, // 0x5C:
860  0, // 0x5D:
861  0, // 0x5E:
862  0, // 0x5F:
863  0, // 0x60:
864  0, // 0x61:
865  0, // 0x62:
866  0, // 0x63:
867  0, // 0x64:
868  0, // 0x65:
869  0, // 0x66:
870  0, // 0x67:
871  0, // 0x68:
872  0, // 0x69:
873  0, // 0x6A:
874  0, // 0x6B:
875  0, // 0x6C:
876  0, // 0x6D:
877  0, // 0x6E:
878  0, // 0x6F:
879  0, // 0x70:
880  0, // 0x71:
881  0, // 0x72:
882  XK_Super_L, // 0x73: XK_Super_L
883  XK_Super_R, // 0x74: XK_Super_R
884  };
885 
886  // |windows_key_code| has to include a valid virtual-key code even when we
887  // use non-US layouts, e.g. even when we type an 'A' key of a US keyboard
888  // on the Hebrew layout, |windows_key_code| should be VK_A.
889  // On the other hand, |event->keyval| value depends on the current
890  // GdkKeymap object, i.e. when we type an 'A' key of a US keyboard on
891  // the Hebrew layout, |event->keyval| becomes XK_hebrew_shin and this
892  // KeyboardCodeFromXKeysym() call returns 0.
893  // To improve compatibilty with Windows, we use |event->hardware_keycode|
894  // for retrieving its Windows key-code for the keys when the
895  // WebCore::windows_key_codeForEvent() call returns 0.
896  // We shouldn't use |event->hardware_keycode| for keys that GdkKeymap
897  // objects cannot change because |event->hardware_keycode| doesn't change
898  // even when we change the layout options, e.g. when we swap a control
899  // key and a caps-lock key, GTK doesn't swap their
900  // |event->hardware_keycode| values but swap their |event->keyval| values.
901  KeyboardCode windows_key_code = KeyboardCodeFromXKeysym(key);
902  if(windows_key_code)
903  return windows_key_code;
904 
905  if(static_cast<size_t>(key) < std::size(kHardwareCodeToXKKeyval)) {
906  int keyval = kHardwareCodeToXKKeyval[key];
907  if(keyval)
908  return KeyboardCodeFromXKeysym(keyval);
909  }
910 
911  // This key is one that keyboard-layout drivers cannot change.
912  // Use |event->keyval| to retrieve its |windows_key_code| value.
913  return KeyboardCodeFromXKeysym(key);
914 }
KeyboardCode
Definition: KeyMapping.cpp:280
KeyboardCode KeyboardCodeFromXKeysym(unsigned int keysym)
Definition: KeyMapping.cpp:476

◆ GetCEFButtonFromSdlMouseButton()

DLLEXPORT int Leviathan::KeyMapping::GetCEFButtonFromSdlMouseButton ( uint32_t  whichbutton)

Definition at line 22 of file KeyMapping.cpp.

23 {
24  if(whichbutton == SDL_BUTTON_LEFT) {
25  return MBT_LEFT;
26 
27  } else if(whichbutton == SDL_BUTTON_RIGHT) {
28  return MBT_RIGHT;
29 
30  } else if(whichbutton == SDL_BUTTON_MIDDLE) {
31  return MBT_MIDDLE;
32 
33  } else {
34  return -1;
35  }
36 }

◆ GetControlCharacter()

DLLEXPORT int Leviathan::KeyMapping::GetControlCharacter ( KeyboardCode  windows_key_code,
bool  shift 
)

Definition at line 934 of file KeyMapping.cpp.

935 {
936  if(windows_key_code >= VKEY_A && windows_key_code <= VKEY_Z) {
937  // ctrl-A ~ ctrl-Z map to \x01 ~ \x1A
938  return windows_key_code - VKEY_A + 1;
939  }
940  if(shift) {
941  // following graphics chars require shift key to input.
942  switch(windows_key_code) {
943  // ctrl-@ maps to \x00 (Null byte)
944  case VKEY_2:
945  return 0;
946  // ctrl-^ maps to \x1E (Record separator, Information separator two)
947  case VKEY_6:
948  return 0x1E;
949  // ctrl-_ maps to \x1F (Unit separator, Information separator one)
950  case VKEY_OEM_MINUS:
951  return 0x1F;
952  // Returns 0 for all other keys to avoid inputting unexpected chars.
953  default: return 0;
954  }
955  } else {
956  switch(windows_key_code) {
957  // ctrl-[ maps to \x1B (Escape)
958  case VKEY_OEM_4:
959  return 0x1B;
960  // ctrl-\ maps to \x1C (File separator, Information separator four)
961  case VKEY_OEM_5:
962  return 0x1C;
963  // ctrl-] maps to \x1D (Group separator, Information separator three)
964  case VKEY_OEM_6:
965  return 0x1D;
966  // ctrl-Enter maps to \x0A (Line feed)
967  case VKEY_RETURN:
968  return 0x0A;
969  // Returns 0 for all other keys to avoid inputting unexpected chars.
970  default: return 0;
971  }
972  }
973 }

◆ GetWindowsKeyCodeWithoutLocation()

DLLEXPORT KeyboardCode Leviathan::KeyMapping::GetWindowsKeyCodeWithoutLocation ( KeyboardCode  key_code)

Definition at line 917 of file KeyMapping.cpp.

918 {
919  switch(key_code) {
920  case VKEY_LCONTROL:
921  case VKEY_RCONTROL: return VKEY_CONTROL;
922  case VKEY_LSHIFT:
923  case VKEY_RSHIFT: return VKEY_SHIFT;
924  case VKEY_LMENU:
925  case VKEY_RMENU: return VKEY_MENU;
926  default: return key_code;
927  }
928 }

◆ SDLKeyToX11Key()

DLLEXPORT int Leviathan::KeyMapping::SDLKeyToX11Key ( const SDL_Keysym &  key)

Definition at line 63 of file KeyMapping.cpp.

64 {
65  switch(key.sym) {
66  // case SDLK_a: return XK_A;
67  // case SDLK_b: return XK_B;
68  // case SDLK_c: return XK_C;
69  // case SDLK_d: return XK_D;
70  // case SDLK_e: return XK_E;
71  // case SDLK_f: return XK_F;
72  // case SDLK_g: return XK_G;
73  // case SDLK_h: return XK_H;
74  // case SDLK_i: return XK_I;
75  // case SDLK_j: return XK_J;
76  // case SDLK_k: return XK_K;
77  // case SDLK_l: return XK_L;
78  // case SDLK_m: return XK_M;
79  // case SDLK_n: return XK_N;
80  // case SDLK_o: return XK_O;
81  // case SDLK_p: return XK_P;
82  // case SDLK_q: return XK_Q;
83  // case SDLK_r: return XK_R;
84  // case SDLK_s: return XK_S;
85  // case SDLK_t: return XK_T;
86  // case SDLK_u: return XK_U;
87  // case SDLK_v: return XK_V;
88  // case SDLK_w: return XK_W;
89  // case SDLK_x: return XK_X;
90  // case SDLK_y: return XK_Y;
91  // case SDLK_z: return XK_Z;
92 
93  case SDLK_a: return XK_a;
94  case SDLK_b: return XK_b;
95  case SDLK_c: return XK_c;
96  case SDLK_d: return XK_d;
97  case SDLK_e: return XK_e;
98  case SDLK_f: return XK_f;
99  case SDLK_g: return XK_g;
100  case SDLK_h: return XK_h;
101  case SDLK_i: return XK_i;
102  case SDLK_j: return XK_j;
103  case SDLK_k: return XK_k;
104  case SDLK_l: return XK_l;
105  case SDLK_m: return XK_m;
106  case SDLK_n: return XK_n;
107  case SDLK_o: return XK_o;
108  case SDLK_p: return XK_p;
109  case SDLK_q: return XK_q;
110  case SDLK_r: return XK_r;
111  case SDLK_s: return XK_s;
112  case SDLK_t: return XK_t;
113  case SDLK_u: return XK_u;
114  case SDLK_v: return XK_v;
115  case SDLK_w: return XK_w;
116  case SDLK_x: return XK_x;
117  case SDLK_y: return XK_y;
118  case SDLK_z: return XK_z;
119 
120  case SDLK_1: return XK_1;
121  case SDLK_2: return XK_2;
122  case SDLK_3: return XK_3;
123  case SDLK_4: return XK_4;
124  case SDLK_5: return XK_5;
125  case SDLK_6: return XK_6;
126  case SDLK_7: return XK_7;
127  case SDLK_8: return XK_8;
128  case SDLK_9: return XK_9;
129  case SDLK_0: return XK_0;
130 
131  case SDLK_RETURN: return XK_Return;
132  case SDLK_ESCAPE: return XK_Escape;
133  case SDLK_BACKSPACE: return XK_BackSpace;
134  case SDLK_TAB: return XK_Tab;
135  case SDLK_SPACE: return XK_space;
136 
137  case SDLK_PLUS: return XK_plus;
138  case SDLK_MINUS: return XK_minus;
139  case SDLK_EQUALS: return XK_equal;
140  case SDLK_LEFTBRACKET: return XK_bracketleft;
141  case SDLK_RIGHTBRACKET: return XK_bracketright;
142  case SDLK_BACKSLASH: return XK_backslash;
143  case SDLK_COLON: return XK_colon;
144  case SDLK_SEMICOLON: return XK_semicolon;
145  case SDLK_QUOTE: return XK_apostrophe;
146  case SDLK_BACKQUOTE: return XK_grave;
147  case SDLK_COMMA: return XK_comma;
148  case SDLK_PERIOD: return XK_period;
149  case SDLK_SLASH: return XK_slash;
150  case SDLK_CAPSLOCK: return XK_Caps_Lock;
151 
152  case SDLK_F1: return XK_F1;
153  case SDLK_F2: return XK_F2;
154  case SDLK_F3: return XK_F3;
155  case SDLK_F4: return XK_F4;
156  case SDLK_F5: return XK_F5;
157  case SDLK_F6: return XK_F6;
158  case SDLK_F7: return XK_F7;
159  case SDLK_F8: return XK_F8;
160  case SDLK_F9: return XK_F9;
161  case SDLK_F10: return XK_F10;
162  case SDLK_F11: return XK_F11;
163  case SDLK_F12: return XK_F12;
164 
165  case SDLK_PRINTSCREEN: return XK_Print;
166  case SDLK_SCROLLLOCK: return XK_Scroll_Lock;
167  case SDLK_PAUSE: return XK_Pause;
168  case SDLK_INSERT: return XK_Insert;
169  case SDLK_HOME: return XK_Home;
170  case SDLK_PAGEUP: return XK_Page_Up;
171  case SDLK_DELETE: return XK_Delete;
172  case SDLK_END: return XK_End;
173  case SDLK_PAGEDOWN: return XK_Page_Down;
174  case SDLK_RIGHT: return XK_Right;
175  case SDLK_LEFT: return XK_Left;
176  case SDLK_DOWN: return XK_Down;
177  case SDLK_UP: return XK_Up;
178 
179  case SDLK_NUMLOCKCLEAR: return XK_Num_Lock;
180  case SDLK_CLEAR: return XK_Clear;
181 
182  case SDLK_KP_DIVIDE: return XK_KP_Divide;
183  case SDLK_KP_MULTIPLY: return XK_KP_Multiply;
184  case SDLK_KP_MINUS: return XK_KP_Subtract;
185  case SDLK_KP_PLUS: return XK_KP_Add;
186  case SDLK_KP_ENTER: return XK_KP_Enter;
187  case SDLK_KP_PERIOD: return XK_KP_Decimal;
188 
189  case SDLK_KP_1: return XK_KP_1;
190  case SDLK_KP_2: return XK_KP_2;
191  case SDLK_KP_3: return XK_KP_3;
192  case SDLK_KP_4: return XK_KP_4;
193  case SDLK_KP_5: return XK_KP_5;
194  case SDLK_KP_6: return XK_KP_6;
195  case SDLK_KP_7: return XK_KP_7;
196  case SDLK_KP_8: return XK_KP_8;
197  case SDLK_KP_9: return XK_KP_9;
198  case SDLK_KP_0: return XK_KP_0;
199 
200  case SDLK_LCTRL: return XK_Control_L;
201  case SDLK_LSHIFT: return XK_Shift_L;
202  case SDLK_LALT: return XK_Alt_L;
203  case SDLK_RCTRL: return XK_Control_R;
204  case SDLK_RSHIFT: return XK_Shift_R;
205  case SDLK_RALT:
206  return XK_Alt_R;
207 
208  // case SDLK_LGUI: return XK_Meta_L;
209  case SDLK_LGUI:
210  return XK_Super_L;
211  // case SDLK_RGUI: return XK_Meta_R;
212  case SDLK_RGUI: return XK_Super_R;
213  case SDLK_MENU: return XK_Menu;
214 
215  case SDLK_LEFTPAREN: return XK_parenleft;
216  case SDLK_RIGHTPAREN: return XK_parenright;
217  case SDLK_KP_LEFTBRACE: return XK_braceleft;
218  case SDLK_KP_RIGHTBRACE: return XK_braceright;
219 
220  case SDLK_LESS: return XK_less;
221  case SDLK_GREATER: return XK_greater;
222  case SDLK_KP_VERTICALBAR: return XK_bar;
223  case SDLK_EXCLAIM: return XK_exclam;
224  case SDLK_QUOTEDBL: return XK_quotedbl;
225  case SDLK_HASH: return XK_numbersign;
226  case SDLK_PERCENT: return XK_percent;
227  case SDLK_DOLLAR: return XK_dollar;
228  case SDLK_AMPERSAND: return XK_ampersand;
229  case SDLK_ASTERISK: return XK_asterisk;
230  case SDLK_QUESTION: return XK_question;
231  case SDLK_AT: return XK_at;
232  case SDLK_UNKNOWN: return XK_periodcentered;
233  case SDLK_CARET: return XK_caret;
234  case SDLK_UNDERSCORE: return XK_underscore;
235 
236  default:
237 #if LEVIATHAN_PRINT_ERRORS_ABOUT_UNKOWN_KEYS == 1
238  LOG_ERROR("Unkown SDL key: " + ConvertKeyCodeToString(key.sym) + +"(" +
239  std::to_string(key.sym) + ")");
240 #endif
241  return XK_VoidSymbol;
242  }
243 }
#define LOG_ERROR(x)
Definition: Define.h:90
DLLEXPORT std::string ConvertKeyCodeToString(const int32_t &code)
Definition: KeyMapping.cpp:52
#define XK_VoidSymbol
Definition: keysymdef.h:101