2 fegdk: FE Game Development Kit
3 Copyright (C) 2001-2008 Alexey "waker" Yakovenko
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with this library; if not, write to the Free
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 waker@users.sourceforge.net
24 #include "f_keybinder.h"
25 #include "f_keycodes.h"
27 #include "f_console.h"
33 keyBinder::keyBinder (void)
37 keyBinder::~keyBinder (void)
41 void keyBinder::bind (int keycombo
, const char *cmd
)
43 mHandlers
[keycombo
] = cmd
;
46 void keyBinder::unbind (int keycombo
)
48 handlersMap::iterator it
= mHandlers
.find (keycombo
);
49 if (it
!= mHandlers
.end ())
53 void keyBinder::unbindAll (void)
58 int keyBinder::keycodeForString (const char *str
) const
60 const char *ptr
= str
;
63 struct { const char* name
; int value
; } modifiers
[] = {
75 char symbols
[] = "[]\\-=`;'/,.";
76 // tab capslock backspace esc ins del home end pgup pgdown
79 // function keys (f1-f15)
82 struct { const char* name
; int value
; } syskeys
[] = {
84 {"capslock", Key_CapsLock
},
85 {"backspace", Key_BackSpace
},
87 {"escape", Key_Escape
},
89 {"return", Key_Enter
},
96 {"left", Key_LeftArrow
},
97 {"right", Key_RightArrow
},
99 {"down", Key_DownArrow
},
100 {"kphome", Key_Kp_Home
},
101 {"kpup", Key_Kp_UpArrow
},
102 {"kppgup", Key_Kp_PgUp
},
103 {"kpleft", Key_Kp_LeftArrow
},
105 {"kpright", Key_Kp_RightArrow
},
106 {"kpend", Key_Kp_End
},
107 {"kpdown", Key_Kp_DownArrow
},
108 {"kpenter", Key_Kp_Enter
},
109 {"kpins", Key_Kp_Ins
},
110 {"kpdel", Key_Kp_Del
},
111 {"kpslash", Key_Kp_Slash
},
112 {"kpminus", Key_Kp_Minus
},
113 {"kpplus", Key_Kp_Plus
},
114 {"kpnumlock", Key_Kp_NumLock
},
115 {"kpstar", Key_Kp_Star
},
116 {"kpequals", Key_Kp_Equals
},
132 {"mouse1", Key_Mouse1
},
133 {"mouse2", Key_Mouse2
},
134 {"mouse3", Key_Mouse3
},
135 {"mouse4", Key_Mouse4
},
136 {"mouse5", Key_Mouse5
},
137 {"wheelup", Key_MWheelUp
},
138 {"wheeldown", Key_MWheelDown
},
146 else if (*ptr
== '+' || *ptr
== ' ')
152 // check if we have a modifier key
153 for (i
= 0; modifiers
[i
].name
; i
++)
155 size_t l
= strlen (modifiers
[i
].name
);
156 if (!strncmp (modifiers
[i
].name
, ptr
, l
)
157 && (*(ptr
+l
) == 0 || *(ptr
+l
) == '+' || *(ptr
+l
) == ' '))
159 keycombo
|= modifiers
[i
].value
;
164 if (modifiers
[i
].name
)
168 if ((*ptr
>= 0x61 && *ptr
<= 0x7a)
169 && (*(ptr
+1)=='+' || *(ptr
+1)==' ' || *(ptr
+1)==0))
177 if ((*ptr
>= 0x31 && *ptr
<= 0x38)
178 && (*(ptr
+1)=='+' || *(ptr
+1)==' ' || *(ptr
+1)==0))
185 // []\-=`;'/,. symbols
186 if (strchr (symbols
, *ptr
))
193 // tab capslock backspace esc ins del home end pgup pgdown
196 // function keys (f1-f15)
198 // wheelup, wheeldown
200 for (i
= 0; syskeys
[i
].name
; i
++)
202 size_t l
= strlen (syskeys
[i
].name
);
203 if (!strncmp (syskeys
[i
].name
, ptr
, l
)
204 && (*(ptr
+l
) == 0 || *(ptr
+l
) == '+' || *(ptr
+l
) == ' '))
206 keycombo
|= syskeys
[i
].value
;
214 // if we got there -- error
220 bool keyBinder::keyDown (int keycombo
)
222 handlersMap::iterator it
;
223 inputState
*st
= g_engine
->getInputDevice ()->getState ();
225 // we gonna check all possible mod-combos
226 // we allow only one modifier + key at a time
227 /* if (st->isPressed (Key_Shift)
228 || st->isPressed (Key_LShift)
229 || st->isPressed (Key_RShift))
231 it = mHandlers.find (keycombo | shift);
233 else*/ if (st
->isPressed (Key_LShift
))
235 it
= mHandlers
.find (keycombo
| lshift
);
237 else if (st
->isPressed (Key_RShift
))
239 it
= mHandlers
.find (keycombo
| rshift
);
241 /*else if (st->isPressed (Key_Alt)
242 || st->isPressed (Key_LAlt)
243 || st->isPressed (Key_RAlt))
245 it = mHandlers.find (keycombo | alt);
247 else if (st
->isPressed (Key_LAlt
))
249 it
= mHandlers
.find (keycombo
| lalt
);
251 else if (st
->isPressed (Key_RAlt
))
253 it
= mHandlers
.find (keycombo
| ralt
);
255 /*else if (st->isPressed (Key_Ctrl)
256 || st->isPressed (Key_LCtrl)
257 || st->isPressed (Key_RCtrl))
259 it = mHandlers.find (keycombo | ctrl);
261 else if (st
->isPressed (Key_LCtrl
))
263 it
= mHandlers
.find (keycombo
| lctrl
);
265 else if (st
->isPressed (Key_RCtrl
))
267 it
= mHandlers
.find (keycombo
| rctrl
);
271 it
= mHandlers
.find (keycombo
);
274 if (it
!= mHandlers
.end ())
276 g_engine
->getConsole ()->command ((*it
).second
.c_str ());
282 bool keyBinder::keyUp (int keycombo
)
284 handlersMap::iterator it
;
285 it
= mHandlers
.find (keycombo
);
286 if (it
!= mHandlers
.end ())
288 if ((*it
).second
[0] == '+')
290 const char *p
= (*it
).second
.c_str ();
291 g_engine
->getConsole ()->command (cStr("-") + p
+ 1);