2009-12-07 Rolf Bjarne Kvinge <RKvinge@novell.com>
[moon.git] / src / keyboard.cpp
blob91fe79c7933998c4f36f4d78279f2e01a7bc25ff
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * keyboard.cpp:
5 * Copyright 2008 Novell, Inc. (http://www.novell.com)
7 * See the LICENSE file included with the distribution for details.
9 */
11 #include <config.h>
13 #include <gdk/gdkkeysyms.h>
15 #include "keyboard.h"
17 ModifierKeys Keyboard::modifiers = ModifierKeyNone;
18 GHashTable *Keyboard::pressedKeys = NULL;
21 ModifierKeys
22 Keyboard::GetModifiers ()
24 return modifiers;
27 void
28 Keyboard::SetModifiers (ModifierKeys m)
30 modifiers = m;
33 void
34 Keyboard::OnKeyPress (Key key)
36 if (!pressedKeys)
37 pressedKeys = g_hash_table_new (g_direct_hash, g_direct_equal);
39 g_hash_table_insert (pressedKeys, GINT_TO_POINTER (key), GINT_TO_POINTER (1));
41 switch (key) {
42 case KeyCTRL:
43 modifiers = (ModifierKeys) (modifiers | ModifierKeyControl);
44 break;
45 case KeyALT:
46 modifiers = (ModifierKeys) (modifiers | ModifierKeyAlt);
47 break;
48 case KeySHIFT:
49 modifiers = (ModifierKeys) (modifiers | ModifierKeyShift);
50 break;
51 default:
52 break;
56 void
57 Keyboard::OnKeyRelease (Key key)
59 if (!pressedKeys)
60 return;
62 g_hash_table_remove (pressedKeys, GINT_TO_POINTER (key));
64 switch (key) {
65 case KeyCTRL:
66 modifiers = (ModifierKeys) (modifiers & ~ModifierKeyControl);
67 break;
68 case KeyALT:
69 modifiers = (ModifierKeys) (modifiers & ~ModifierKeyAlt);
70 break;
71 case KeySHIFT:
72 modifiers = (ModifierKeys) (modifiers & ~ModifierKeyShift);
73 break;
74 default:
75 break;
79 bool
80 Keyboard::IsKeyPressed (Key key)
82 return pressedKeys && (g_hash_table_lookup (pressedKeys, GINT_TO_POINTER (key)) != NULL);
85 Key
86 Keyboard::MapKeyValToKey (guint keyval)
88 switch (keyval) {
89 case GDK_BackSpace: return KeyBACKSPACE;
90 case GDK_Tab: case GDK_ISO_Left_Tab: return KeyTAB;
91 case GDK_Return: case GDK_KP_Enter: return KeyENTER;
92 case GDK_Shift_L: case GDK_Shift_R: return KeySHIFT;
93 case GDK_Control_L: case GDK_Control_R: return KeyCTRL;
94 case GDK_Alt_L: case GDK_Alt_R: return KeyALT;
95 case GDK_Caps_Lock: return KeyCAPSLOCK;
96 case GDK_Escape: return KeyESCAPE;
97 case GDK_space: case GDK_KP_Space: return KeySPACE;
98 case GDK_Page_Up: case GDK_KP_Page_Up: return KeyPAGEUP;
99 case GDK_Page_Down: case GDK_KP_Page_Down: return KeyPAGEDOWN;
100 case GDK_End: case GDK_KP_End: return KeyEND;
101 case GDK_Home: case GDK_KP_Home: return KeyHOME;
102 case GDK_Left: case GDK_KP_Left: return KeyLEFT;
103 case GDK_Up: case GDK_KP_Up: return KeyUP;
104 case GDK_Right: case GDK_KP_Right: return KeyRIGHT;
105 case GDK_Down: case GDK_KP_Down: return KeyDOWN;
106 case GDK_Insert: case GDK_KP_Insert: return KeyINSERT;
107 case GDK_Delete: case GDK_KP_Delete: return KeyDELETE;
108 case GDK_0: return KeyDIGIT0;
109 case GDK_1: return KeyDIGIT1;
110 case GDK_2: return KeyDIGIT2;
111 case GDK_3: return KeyDIGIT3;
112 case GDK_4: return KeyDIGIT4;
113 case GDK_5: return KeyDIGIT5;
114 case GDK_6: return KeyDIGIT6;
115 case GDK_7: return KeyDIGIT7;
116 case GDK_8: return KeyDIGIT8;
117 case GDK_9: return KeyDIGIT9;
118 case GDK_a: case GDK_A: return KeyA;
119 case GDK_b: case GDK_B: return KeyB;
120 case GDK_c: case GDK_C: return KeyC;
121 case GDK_d: case GDK_D: return KeyD;
122 case GDK_e: case GDK_E: return KeyE;
123 case GDK_f: case GDK_F: return KeyF;
124 case GDK_g: case GDK_G: return KeyG;
125 case GDK_h: case GDK_H: return KeyH;
126 case GDK_i: case GDK_I: return KeyI;
127 case GDK_j: case GDK_J: return KeyJ;
128 case GDK_k: case GDK_K: return KeyK;
129 case GDK_l: case GDK_L: return KeyL;
130 case GDK_m: case GDK_M: return KeyM;
131 case GDK_n: case GDK_N: return KeyN;
132 case GDK_o: case GDK_O: return KeyO;
133 case GDK_p: case GDK_P: return KeyP;
134 case GDK_q: case GDK_Q: return KeyQ;
135 case GDK_r: case GDK_R: return KeyR;
136 case GDK_s: case GDK_S: return KeyS;
137 case GDK_t: case GDK_T: return KeyT;
138 case GDK_u: case GDK_U: return KeyU;
139 case GDK_v: case GDK_V: return KeyV;
140 case GDK_w: case GDK_W: return KeyW;
141 case GDK_x: case GDK_X: return KeyX;
142 case GDK_y: case GDK_Y: return KeyY;
143 case GDK_z: case GDK_Z: return KeyZ;
145 case GDK_F1: case GDK_KP_F1: return KeyF1;
146 case GDK_F2: case GDK_KP_F2: return KeyF2;
147 case GDK_F3: case GDK_KP_F3: return KeyF3;
148 case GDK_F4: case GDK_KP_F4: return KeyF4;
149 case GDK_F5: return KeyF5;
150 case GDK_F6: return KeyF6;
151 case GDK_F7: return KeyF7;
152 case GDK_F8: return KeyF8;
153 case GDK_F9: return KeyF9;
154 case GDK_F10: return KeyF10;
155 case GDK_F11: return KeyF11;
156 case GDK_F12: return KeyF12;
158 case GDK_KP_0: return KeyNUMPAD0;
159 case GDK_KP_1: return KeyNUMPAD1;
160 case GDK_KP_2: return KeyNUMPAD2;
161 case GDK_KP_3: return KeyNUMPAD3;
162 case GDK_KP_4: return KeyNUMPAD4;
163 case GDK_KP_5: return KeyNUMPAD5;
164 case GDK_KP_6: return KeyNUMPAD6;
165 case GDK_KP_7: return KeyNUMPAD7;
166 case GDK_KP_8: return KeyNUMPAD8;
167 case GDK_KP_9: return KeyNUMPAD9;
169 case GDK_KP_Multiply: case GDK_asterisk: return KeyMULTIPLY;
170 case GDK_KP_Add: case GDK_plus: return KeyADD;
171 case GDK_KP_Subtract: case GDK_minus: return KeySUBTRACT;
172 case GDK_KP_Decimal: case GDK_period: return KeyDECIMAL;
173 case GDK_KP_Divide: case GDK_slash: return KeyDIVIDE;
175 default:
176 return KeyUNKNOWN;