ifdef's for future py3 support, after this adding py3 can mostly be done with defines...
[blender.git] / source / gameengine / GameLogic / SCA_KeyboardSensor.cpp
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  * Sensor for keyboard input
29  */
30 #include "SCA_KeyboardSensor.h"
31 #include "SCA_KeyboardManager.h"
32 #include "SCA_LogicManager.h"
33 #include "StringValue.h"
34 #include "SCA_IInputDevice.h"
35
36 #ifdef HAVE_CONFIG_H
37 #include <config.h>
38 #endif
39
40 /* ------------------------------------------------------------------------- */
41 /* Native functions                                                          */
42 /* ------------------------------------------------------------------------- */
43
44 SCA_KeyboardSensor::SCA_KeyboardSensor(SCA_KeyboardManager* keybdmgr,
45                                                                            short int hotkey,
46                                                                            short int qual,
47                                                                            short int qual2,
48                                                                            bool bAllKeys,
49                                                                            const STR_String& targetProp,
50                                                                            const STR_String& toggleProp,
51                                                                            SCA_IObject* gameobj,
52                                                                            PyTypeObject* T )
53         :SCA_ISensor(gameobj,keybdmgr,T),
54          m_pKeyboardMgr(keybdmgr),
55          m_hotkey(hotkey),
56          m_qual(qual),
57          m_qual2(qual2),
58          m_bAllKeys(bAllKeys),
59          m_targetprop(targetProp),
60          m_toggleprop(toggleProp)
61 {
62         if (hotkey == SCA_IInputDevice::KX_ESCKEY)
63                 keybdmgr->GetInputDevice()->HookEscape();
64 //      SetDrawColor(0xff0000ff);
65         Init();
66 }
67
68
69
70 SCA_KeyboardSensor::~SCA_KeyboardSensor()
71 {
72 }
73
74 void SCA_KeyboardSensor::Init()
75 {
76         // this function is used when the sensor is disconnected from all controllers
77         // by the state engine. It reinitializes the sensor as if it was just created.
78         // However, if the target key is pressed when the sensor is reactivated, it
79         // will not generated an event (see remark in Evaluate()).
80         m_val = (m_invert)?1:0;
81         m_reset = true;
82 }
83
84 CValue* SCA_KeyboardSensor::GetReplica()
85 {
86         SCA_KeyboardSensor* replica = new SCA_KeyboardSensor(*this);
87         // this will copy properties and so on...
88         replica->ProcessReplica();
89         replica->Init();
90         return replica;
91 }
92
93
94
95 short int SCA_KeyboardSensor::GetHotkey()
96 {
97         return m_hotkey;
98 }
99
100
101
102 bool SCA_KeyboardSensor::IsPositiveTrigger()
103
104         bool result = (m_val != 0);
105
106         if (m_invert)
107                 result = !result;
108                 
109         return result;
110 }
111
112
113
114 bool SCA_KeyboardSensor::TriggerOnAllKeys()
115
116         return m_bAllKeys;
117 }
118
119
120
121 bool SCA_KeyboardSensor::Evaluate(CValue* eventval)
122 {
123         bool result    = false;
124         bool reset     = m_reset && m_level;
125         bool qual          = true;
126         bool qual_change = false;
127         short int m_val_orig = m_val;
128         
129         SCA_IInputDevice* inputdev = m_pKeyboardMgr->GetInputDevice();
130         //      cerr << "SCA_KeyboardSensor::Eval event, sensing for "<< m_hotkey << " at device " << inputdev << "\n";
131
132         /* See if we need to do logging: togPropState exists and is
133      * different from 0 */
134         CValue* myparent = GetParent();
135         CValue* togPropState = myparent->GetProperty(m_toggleprop);
136         if (togPropState &&
137                 (((int)togPropState->GetNumber()) != 0) )
138         {
139                 LogKeystrokes();
140         }
141
142         m_reset = false;
143
144         /* Now see whether events must be bounced. */
145         if (m_bAllKeys)
146         {
147                 bool justactivated = false;
148                 bool justreleased = false;
149                 bool active = false;
150
151                 for (int i=SCA_IInputDevice::KX_BEGINKEY ; i<= SCA_IInputDevice::KX_ENDKEY;i++)
152                 {
153                         const SCA_InputEvent & inevent = inputdev->GetEventValue((SCA_IInputDevice::KX_EnumInputs) i);
154                         switch (inevent.m_status) 
155                         { 
156                         case SCA_InputEvent::KX_JUSTACTIVATED:
157                                 justactivated = true;
158                                 break;
159                         case SCA_InputEvent::KX_JUSTRELEASED:
160                                 justreleased = true;
161                                 break;
162                         case SCA_InputEvent::KX_ACTIVE:
163                                 active = true;
164                                 break;
165                         }
166                 }
167
168                 if (justactivated)
169                 {
170                         m_val=1;
171                         result = true;
172                 } else
173                 {
174                         if (justreleased)
175                         {
176                                 m_val=(active)?1:0;
177                                 result = true;
178                         } else
179                         {
180                                 if (active)
181                                 {
182                                         if (m_val == 0)
183                                         {
184                                                 m_val = 1;
185                                                 if (m_level) {
186                                                         result = true;
187                                                 }
188                                         }
189                                 } else
190                                 {
191                                         if (m_val == 1)
192                                         {
193                                                 m_val = 0;
194                                                 result = true;
195                                         }
196                                 }
197                         }
198                 }
199
200
201         } else
202         {
203
204         //              cerr << "======= SCA_KeyboardSensor::Evaluate:: peeking at key status" << endl;
205                 const SCA_InputEvent & inevent = inputdev->GetEventValue(
206                         (SCA_IInputDevice::KX_EnumInputs) m_hotkey);
207         
208         //              cerr << "======= SCA_KeyboardSensor::Evaluate:: status: " << inevent.m_status << endl;
209                 
210                 
211                 /* Check qualifier keys
212                  * - see if the qualifiers we request are pressed - 'qual' true/false
213                  * - see if the qualifiers we request changed their state - 'qual_change' true/false
214                  */
215                 if (m_qual > 0) {
216                         const SCA_InputEvent & qualevent = inputdev->GetEventValue((SCA_IInputDevice::KX_EnumInputs) m_qual);
217                         switch(qualevent.m_status) {
218                         case SCA_InputEvent::KX_NO_INPUTSTATUS:
219                                 qual = false;
220                                 break;
221                         case SCA_InputEvent::KX_JUSTRELEASED:
222                                 qual_change = true;
223                                 qual = false;
224                                 break;
225                         case SCA_InputEvent::KX_JUSTACTIVATED:
226                                 qual_change = true;
227                         }
228                 }
229                 if (m_qual2 > 0 && qual==true) {
230                         const SCA_InputEvent & qualevent = inputdev->GetEventValue((SCA_IInputDevice::KX_EnumInputs) m_qual2);
231                         /* copy of above */
232                         switch(qualevent.m_status) {
233                         case SCA_InputEvent::KX_NO_INPUTSTATUS:
234                                 qual = false;
235                                 break;
236                         case SCA_InputEvent::KX_JUSTRELEASED:
237                                 qual_change = true;
238                                 qual = false;
239                                 break;
240                         case SCA_InputEvent::KX_JUSTACTIVATED:
241                                 qual_change = true;
242                         }
243                 }
244                 /* done reading qualifiers */
245                 
246                 if (inevent.m_status == SCA_InputEvent::KX_NO_INPUTSTATUS)
247                 {
248                         if (m_val == 1)
249                         {
250                                 // this situation may occur after a scene suspend: the keyboard release 
251                                 // event was not captured, produce now the event off
252                                 m_val = 0;
253                                 result = true;
254                         }
255                 } else
256                 {
257                         if (inevent.m_status == SCA_InputEvent::KX_JUSTACTIVATED)
258                         {
259                                 m_val=1;
260                                 result = true;
261                         } else
262                         {
263                                 if (inevent.m_status == SCA_InputEvent::KX_JUSTRELEASED)
264                                 {
265                                         m_val = 0;
266                                         result = true;
267                                 } else 
268                                 {
269                                         if (inevent.m_status == SCA_InputEvent::KX_ACTIVE)
270                                         {
271                                                 if (m_val == 0)
272                                                 {
273                                                         m_val = 1;
274                                                         if (m_level) 
275                                                         {
276                                                                 result = true;
277                                                         }
278                                                 }
279                                         }
280                                 }
281                         }
282                 }
283                 
284                 /* Modify the key state based on qual(s)
285                  * Tested carefuly. dont touch unless your really sure.
286                  * note, this will only change the results if key modifiers are set.
287                  *
288                  * When all modifiers and keys are positive
289                  *  - pulse true
290                  * 
291                  * When ANY of the modifiers or main key become inactive,
292                  *  - pulse false
293                  */
294                 if (qual==false) { /* one of the qualifiers are not pressed */
295                         if (m_val_orig && qual_change) { /* we were originally enabled, but a qualifier changed */
296                                 result = true;
297                         } else {
298                                 result = false;
299                         }
300                         m_val = 0; /* since one of the qualifiers is not on, set the state to false */
301                 } else {                                                /* we done have any qualifiers or they are all pressed */
302                         if (m_val && qual_change) {     /* the main key state is true and our qualifier just changed */
303                                 result = true;
304                         }
305                 }
306                 /* done with key quals */
307                 
308         }
309         
310         if (reset)
311                 // force an event
312                 result = true;
313         return result;
314
315 }
316
317 void SCA_KeyboardSensor::AddToTargetProp(int keyIndex)
318 {
319         if (IsPrintable(keyIndex)) {
320                 CValue* tprop = GetParent()->GetProperty(m_targetprop);
321                 
322                 if (tprop) {
323                         /* overwrite the old property */
324                         if (IsDelete(keyIndex)) {
325                                 /* strip one char, if possible */
326                                 STR_String newprop = tprop->GetText();
327                                 int oldlength = newprop.Length();
328                                 if (oldlength >= 1 ) {
329                                         newprop.SetLength(oldlength - 1);
330                                         CStringValue * newstringprop = new CStringValue(newprop, m_targetprop);
331                                         GetParent()->SetProperty(m_targetprop, newstringprop);
332                                         newstringprop->Release();
333                                 }                               
334                         } else {
335                                 /* append */
336                                 char pchar = ToCharacter(keyIndex, IsShifted());
337                                 STR_String newprop = tprop->GetText() + pchar;
338                                 CStringValue * newstringprop = new CStringValue(newprop, m_targetprop);                 
339                                 GetParent()->SetProperty(m_targetprop, newstringprop);
340                                 newstringprop->Release();
341                         }
342                 } else {
343                         if (!IsDelete(keyIndex)) {
344                                 /* Make a new property. Deletes can be ignored. */
345                                 char pchar = ToCharacter(keyIndex, IsShifted());
346                                 STR_String newprop = pchar;
347                                 CStringValue * newstringprop = new CStringValue(newprop, m_targetprop);                 
348                                 GetParent()->SetProperty(m_targetprop, newstringprop);
349                                 newstringprop->Release();
350                         }
351                 }
352         }
353         
354 }
355         
356 /**
357  * Tests whether shift is pressed
358  */     
359 bool SCA_KeyboardSensor::IsShifted(void)
360 {
361         SCA_IInputDevice* inputdev = m_pKeyboardMgr->GetInputDevice();
362         
363         if ( (inputdev->GetEventValue(SCA_IInputDevice::KX_RIGHTSHIFTKEY).m_status 
364                   == SCA_InputEvent::KX_ACTIVE)
365                  || (inputdev->GetEventValue(SCA_IInputDevice::KX_RIGHTSHIFTKEY).m_status 
366                          == SCA_InputEvent::KX_JUSTACTIVATED)
367                  || (inputdev->GetEventValue(SCA_IInputDevice::KX_LEFTSHIFTKEY).m_status 
368                          == SCA_InputEvent::KX_ACTIVE)
369                  || (inputdev->GetEventValue(SCA_IInputDevice::KX_LEFTSHIFTKEY).m_status 
370                          == SCA_InputEvent::KX_JUSTACTIVATED)
371                 ) {
372                 return true;
373         } else {
374                 return false;
375         }       
376 }
377
378 void SCA_KeyboardSensor::LogKeystrokes(void) 
379 {
380         SCA_IInputDevice* inputdev = m_pKeyboardMgr->GetInputDevice();
381         int num = inputdev->GetNumActiveEvents();
382
383         /* weird loop, this one... */
384         if (num > 0)
385         {
386                 
387                 int index = 0;
388                 /* Check on all keys whether they were pushed. This does not
389          * untangle the ordering, so don't type too fast :) */
390                 for (int i=SCA_IInputDevice::KX_BEGINKEY ; i<= SCA_IInputDevice::KX_ENDKEY;i++)
391                 {
392                         const SCA_InputEvent & inevent = inputdev->GetEventValue((SCA_IInputDevice::KX_EnumInputs) i);
393                         if (inevent.m_status == SCA_InputEvent::KX_JUSTACTIVATED) //NO_INPUTSTATUS)
394                         {
395                                 if (index < num)
396                                 {
397                                         AddToTargetProp(i);
398                                         index++;
399                                 }
400                         }
401                 }
402         }
403 }
404
405
406 /* ------------------------------------------------------------------------- */
407 /* Python Functions                                                    */
408 /* ------------------------------------------------------------------------- */
409
410 //Deprecated ----->
411 /** 1. GetKey : check which key this sensor looks at */
412 const char SCA_KeyboardSensor::GetKey_doc[] = 
413 "getKey()\n"
414 "\tReturn the code of the key this sensor is listening to.\n" ;
415 PyObject* SCA_KeyboardSensor::PyGetKey()
416 {
417         ShowDeprecationWarning("getKey()", "the key property");
418         return PyInt_FromLong(m_hotkey);
419 }
420
421 /** 2. SetKey: change the key to look at */
422 const char SCA_KeyboardSensor::SetKey_doc[] = 
423 "setKey(keycode)\n"
424 "\t- keycode: any code from GameKeys\n"
425 "\tSet the key this sensor should listen to.\n" ;
426 PyObject* SCA_KeyboardSensor::PySetKey(PyObject* args)
427 {
428         ShowDeprecationWarning("setKey()", "the key property");
429         int keyCode;
430         
431         if(!PyArg_ParseTuple(args, "i:setKey", &keyCode)) {
432                 return NULL;
433         }
434
435         /* Since we have symbolic constants for this in Python, we don't guard   */
436         /* anything. It's up to the user to provide a sensible number.           */
437         m_hotkey = keyCode;
438
439         Py_RETURN_NONE;
440 }
441
442 /** 3. GetHold1 : set the first bucky bit */
443 const char SCA_KeyboardSensor::GetHold1_doc[] = 
444 "getHold1()\n"
445 "\tReturn the code of the first key modifier to the key this \n"
446 "\tsensor is listening to.\n" ;
447 PyObject* SCA_KeyboardSensor::PyGetHold1()
448 {
449         ShowDeprecationWarning("getHold1()", "the hold1 property");
450         return PyInt_FromLong(m_qual);
451 }
452
453 /** 4. SetHold1: change the first bucky bit */
454 const char SCA_KeyboardSensor::SetHold1_doc[] = 
455 "setHold1(keycode)\n"
456 "\t- keycode: any code from GameKeys\n"
457 "\tSet the first modifier to the key this sensor should listen to.\n" ;
458 PyObject* SCA_KeyboardSensor::PySetHold1(PyObject* args)
459 {
460         ShowDeprecationWarning("setHold1()", "the hold1 property");
461         int keyCode;
462
463         if(!PyArg_ParseTuple(args, "i:setHold1", &keyCode)) {
464                 return NULL;
465         }
466         
467         /* Since we have symbolic constants for this in Python, we don't guard   */
468         /* anything. It's up to the user to provide a sensible number.           */
469         m_qual = keyCode;
470
471         Py_RETURN_NONE;
472 }
473         
474 /** 5. GetHold2 : get the second bucky bit */
475 const char SCA_KeyboardSensor::GetHold2_doc[] = 
476 "getHold2()\n"
477 "\tReturn the code of the second key modifier to the key this \n"
478 "\tsensor is listening to.\n" ;
479 PyObject* SCA_KeyboardSensor::PyGetHold2()
480 {
481         ShowDeprecationWarning("getHold2()", "the hold2 property");
482         return PyInt_FromLong(m_qual2);
483 }
484
485 /** 6. SetHold2: change the second bucky bit */
486 const char SCA_KeyboardSensor::SetHold2_doc[] = 
487 "setHold2(keycode)\n"
488 "\t- keycode: any code from GameKeys\n"
489 "\tSet the first modifier to the key this sensor should listen to.\n" ;
490 PyObject* SCA_KeyboardSensor::PySetHold2(PyObject* args)
491 {
492         ShowDeprecationWarning("setHold2()", "the hold2 property");
493         int keyCode;
494
495         if(!PyArg_ParseTuple(args, "i:setHold2", &keyCode)) {
496                 return NULL;
497         }
498         
499         /* Since we have symbolic constants for this in Python, we don't guard   */
500         /* anything. It's up to the user to provide a sensible number.           */
501         m_qual2 = keyCode;
502
503         Py_RETURN_NONE;
504 }
505
506         
507 const char SCA_KeyboardSensor::GetPressedKeys_doc[] = 
508 "getPressedKeys()\n"
509 "\tGet a list of pressed keys that have either been pressed, or just released this frame.\n" ;
510
511 PyObject* SCA_KeyboardSensor::PyGetPressedKeys()
512 {
513         ShowDeprecationWarning("getPressedKeys()", "events");
514
515         SCA_IInputDevice* inputdev = m_pKeyboardMgr->GetInputDevice();
516
517         int num = inputdev->GetNumJustEvents();
518         PyObject* resultlist = PyList_New(num);
519
520         if (num > 0)
521         {
522                 
523                 int index = 0;
524                 
525                 for (int i=SCA_IInputDevice::KX_BEGINKEY ; i<= SCA_IInputDevice::KX_ENDKEY;i++)
526                 {
527                         const SCA_InputEvent & inevent = inputdev->GetEventValue((SCA_IInputDevice::KX_EnumInputs) i);
528                         if ((inevent.m_status == SCA_InputEvent::KX_JUSTACTIVATED)
529                                 || (inevent.m_status == SCA_InputEvent::KX_JUSTRELEASED))
530                         {
531                                 PyObject* keypair = PyList_New(2);
532                                 PyList_SET_ITEM(keypair,0,PyInt_FromLong(i));
533                                 PyList_SET_ITEM(keypair,1,PyInt_FromLong(inevent.m_status));
534                                 PyList_SET_ITEM(resultlist,index,keypair);
535                                 index++;
536                                 
537                                 if (index >= num) /* should not happen */
538                                         break; 
539                         }
540                 }
541         }
542         
543         return resultlist;
544 }
545
546
547
548 const char SCA_KeyboardSensor::GetCurrentlyPressedKeys_doc[] = 
549 "getCurrentlyPressedKeys()\n"
550 "\tGet a list of keys that are currently pressed.\n" ;
551
552 PyObject* SCA_KeyboardSensor::PyGetCurrentlyPressedKeys()
553 {
554         ShowDeprecationWarning("getCurrentlyPressedKeys()", "events");
555
556         SCA_IInputDevice* inputdev = m_pKeyboardMgr->GetInputDevice();
557
558         int num = inputdev->GetNumActiveEvents();
559         PyObject* resultlist = PyList_New(num);
560
561         if (num > 0)
562         {
563                 int index = 0;
564                 
565                 for (int i=SCA_IInputDevice::KX_BEGINKEY ; i<= SCA_IInputDevice::KX_ENDKEY;i++)
566                 {
567                         const SCA_InputEvent & inevent = inputdev->GetEventValue((SCA_IInputDevice::KX_EnumInputs) i);
568                         if ( (inevent.m_status == SCA_InputEvent::KX_ACTIVE)
569                                  || (inevent.m_status == SCA_InputEvent::KX_JUSTACTIVATED))
570                         {
571                                 PyObject* keypair = PyList_New(2);
572                                 PyList_SET_ITEM(keypair,0,PyInt_FromLong(i));
573                                 PyList_SET_ITEM(keypair,1,PyInt_FromLong(inevent.m_status));
574                                 PyList_SET_ITEM(resultlist,index,keypair);
575                                 index++;
576                                 
577                                 if (index >= num) /* should never happen */
578                                         break;
579                         }
580                 }
581         }
582
583         return resultlist;
584 }
585
586 //<---- Deprecated
587
588 KX_PYMETHODDEF_DOC_O(SCA_KeyboardSensor, getKeyStatus,
589 "getKeyStatus(keycode)\n"
590 "\tGet the given key's status (KX_NO_INPUTSTATUS, KX_JUSTACTIVATED, KX_ACTIVE or KX_JUSTRELEASED).\n")
591 {
592         if (!PyInt_Check(value)) {
593                 PyErr_SetString(PyExc_ValueError, "sensor.getKeyStatus(int): Keyboard Sensor, expected an int");
594                 return NULL;
595         }
596         
597         int keycode = PyInt_AsLong(value);
598         
599         if ((keycode < SCA_IInputDevice::KX_BEGINKEY)
600                 || (keycode > SCA_IInputDevice::KX_ENDKEY)){
601                 PyErr_SetString(PyExc_AttributeError, "sensor.getKeyStatus(int): Keyboard Sensor, invalid keycode specified!");
602                 return NULL;
603         }
604         
605         SCA_IInputDevice* inputdev = m_pKeyboardMgr->GetInputDevice();
606         const SCA_InputEvent & inevent = inputdev->GetEventValue((SCA_IInputDevice::KX_EnumInputs) keycode);
607         return PyInt_FromLong(inevent.m_status);
608 }
609
610 /* ------------------------------------------------------------------------- */
611 /* Python Integration Hooks                                            */
612 /* ------------------------------------------------------------------------- */
613
614 PyTypeObject SCA_KeyboardSensor::Type = {
615 #if (PY_VERSION_HEX >= 0x02060000)
616         PyVarObject_HEAD_INIT(NULL, 0)
617 #else
618         /* python 2.5 and below */
619         PyObject_HEAD_INIT( NULL )  /* required py macro */
620         0,                          /* ob_size */
621 #endif
622         "SCA_KeyboardSensor",
623         sizeof(PyObjectPlus_Proxy),
624         0,
625         py_base_dealloc,
626         0,
627         0,
628         0,
629         0,
630         py_base_repr,
631         0,0,0,0,0,0,
632         py_base_getattro,
633         py_base_setattro,
634         0,0,0,0,0,0,0,0,0,
635         Methods
636 };
637
638 PyParentObject SCA_KeyboardSensor::Parents[] = {
639         &SCA_KeyboardSensor::Type,
640         &SCA_ISensor::Type,
641         &SCA_ILogicBrick::Type,
642         &CValue::Type,
643         NULL
644 };
645
646 PyMethodDef SCA_KeyboardSensor::Methods[] = {
647         //Deprecated functions ------>
648         {"getKey", (PyCFunction) SCA_KeyboardSensor::sPyGetKey, METH_NOARGS, (PY_METHODCHAR)GetKey_doc},
649         {"setKey", (PyCFunction) SCA_KeyboardSensor::sPySetKey, METH_VARARGS, (PY_METHODCHAR)SetKey_doc},
650         {"getHold1", (PyCFunction) SCA_KeyboardSensor::sPyGetHold1, METH_NOARGS, (PY_METHODCHAR)GetHold1_doc},
651         {"setHold1", (PyCFunction) SCA_KeyboardSensor::sPySetHold1, METH_VARARGS, (PY_METHODCHAR)SetHold1_doc},
652         {"getHold2", (PyCFunction) SCA_KeyboardSensor::sPyGetHold2, METH_NOARGS, (PY_METHODCHAR)GetHold2_doc},
653         {"setHold2", (PyCFunction) SCA_KeyboardSensor::sPySetHold2, METH_VARARGS, (PY_METHODCHAR)SetHold2_doc},
654         {"getPressedKeys", (PyCFunction) SCA_KeyboardSensor::sPyGetPressedKeys, METH_NOARGS, (PY_METHODCHAR)GetPressedKeys_doc},
655         {"getCurrentlyPressedKeys", (PyCFunction) SCA_KeyboardSensor::sPyGetCurrentlyPressedKeys, METH_NOARGS, (PY_METHODCHAR)GetCurrentlyPressedKeys_doc},
656         //<----- Deprecated
657         KX_PYMETHODTABLE_O(SCA_KeyboardSensor, getKeyStatus),
658         {NULL,NULL} //Sentinel
659 };
660
661 PyAttributeDef SCA_KeyboardSensor::Attributes[] = {
662         KX_PYATTRIBUTE_RO_FUNCTION("events", SCA_KeyboardSensor, pyattr_get_events),
663         KX_PYATTRIBUTE_BOOL_RW("useAllKeys",SCA_KeyboardSensor,m_bAllKeys),
664         KX_PYATTRIBUTE_INT_RW("key",0,SCA_IInputDevice::KX_ENDKEY,true,SCA_KeyboardSensor,m_hotkey),
665         KX_PYATTRIBUTE_SHORT_RW("hold1",0,SCA_IInputDevice::KX_ENDKEY,true,SCA_KeyboardSensor,m_qual),
666         KX_PYATTRIBUTE_SHORT_RW("hold2",0,SCA_IInputDevice::KX_ENDKEY,true,SCA_KeyboardSensor,m_qual2),
667         KX_PYATTRIBUTE_STRING_RW("toggleProperty",0,100,false,SCA_KeyboardSensor,m_toggleprop),
668         KX_PYATTRIBUTE_STRING_RW("targetProperty",0,100,false,SCA_KeyboardSensor,m_targetprop),
669         { NULL }        //Sentinel
670 };
671
672 PyObject* SCA_KeyboardSensor::py_getattro(PyObject *attr)
673 {
674   py_getattro_up(SCA_ISensor);
675 }
676
677 PyObject* SCA_KeyboardSensor::py_getattro_dict() {
678         py_getattro_dict_up(SCA_ISensor);
679 }
680
681 int SCA_KeyboardSensor::py_setattro(PyObject *attr, PyObject *value)
682 {
683         py_setattro_up(SCA_ISensor);
684 }
685
686
687 PyObject* SCA_KeyboardSensor::pyattr_get_events(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
688 {
689         SCA_KeyboardSensor* self= static_cast<SCA_KeyboardSensor*>(self_v);
690         
691         SCA_IInputDevice* inputdev = self->m_pKeyboardMgr->GetInputDevice();
692
693         PyObject* resultlist = PyList_New(0);
694         
695         for (int i=SCA_IInputDevice::KX_BEGINKEY ; i<= SCA_IInputDevice::KX_ENDKEY;i++)
696         {
697                 const SCA_InputEvent & inevent = inputdev->GetEventValue((SCA_IInputDevice::KX_EnumInputs) i);
698                 if (inevent.m_status != SCA_InputEvent::KX_NO_INPUTSTATUS)
699                 {
700                         PyObject* keypair = PyList_New(2);
701                         PyList_SET_ITEM(keypair,0,PyInt_FromLong(i));
702                         PyList_SET_ITEM(keypair,1,PyInt_FromLong(inevent.m_status));
703                         PyList_Append(resultlist,keypair);
704                 }
705         }       
706         return resultlist;
707 }
708
709
710 /* Accessed from python */
711
712 // this code looks ugly, please use an ordinary hashtable
713
714 char ToCharacter(int keyIndex, bool shifted)
715 {
716         /* numerals */
717         if ( (keyIndex >= SCA_IInputDevice::KX_ZEROKEY) 
718                  && (keyIndex <= SCA_IInputDevice::KX_NINEKEY) ) {
719                 if (shifted) {
720                         char numshift[] = ")!@#$%^&*(";
721                         return numshift[keyIndex - '0']; 
722                 } else {
723                         return keyIndex - SCA_IInputDevice::KX_ZEROKEY + '0'; 
724                 }
725         }
726         
727         /* letters... always lowercase... is that desirable? */
728         if ( (keyIndex >= SCA_IInputDevice::KX_AKEY) 
729                  && (keyIndex <= SCA_IInputDevice::KX_ZKEY) ) {
730                 if (shifted) {
731                         return keyIndex - SCA_IInputDevice::KX_AKEY + 'A'; 
732                 } else {
733                         return keyIndex - SCA_IInputDevice::KX_AKEY + 'a'; 
734                 }
735         }
736         
737         if (keyIndex == SCA_IInputDevice::KX_SPACEKEY) {
738                 return ' ';
739         }
740         if (keyIndex == SCA_IInputDevice::KX_RETKEY || keyIndex == SCA_IInputDevice::KX_PADENTER) {
741                 return '\n';
742         }
743         
744         
745         if (keyIndex == SCA_IInputDevice::KX_PADASTERKEY) {
746                 return '*';
747         }
748         
749         if (keyIndex == SCA_IInputDevice::KX_TABKEY) {
750                 return '\t';
751         }
752         
753         /* comma to period */
754         char commatoperiod[] = ",-.";
755         char commatoperiodshifted[] = "<_>";
756         if (keyIndex == SCA_IInputDevice::KX_COMMAKEY) {
757                 if (shifted) {
758                         return commatoperiodshifted[0];
759                 } else {
760                         return commatoperiod[0];
761                 }
762         }
763         if (keyIndex == SCA_IInputDevice::KX_MINUSKEY) {
764                 if (shifted) {
765                         return commatoperiodshifted[1];
766                 } else {
767                         return commatoperiod[1];
768                 }
769         }
770         if (keyIndex == SCA_IInputDevice::KX_PERIODKEY) {
771                 if (shifted) {
772                         return commatoperiodshifted[2];
773                 } else {
774                         return commatoperiod[2];
775                 }
776         }
777         
778         /* semicolon to rightbracket */
779         char semicolontorightbracket[] = ";\'`/\\=[]";
780         char semicolontorightbracketshifted[] = ":\"~\?|+{}";
781         if ((keyIndex >= SCA_IInputDevice::KX_SEMICOLONKEY) 
782                 && (keyIndex <= SCA_IInputDevice::KX_RIGHTBRACKETKEY)) {
783                 if (shifted) {
784                         return semicolontorightbracketshifted[keyIndex - SCA_IInputDevice::KX_SEMICOLONKEY];
785                 } else {
786                         return semicolontorightbracket[keyIndex - SCA_IInputDevice::KX_SEMICOLONKEY];
787                 }
788         }
789         
790         /* keypad2 to padplus */
791         char pad2topadplus[] = "246813579. 0- +";
792         if ((keyIndex >= SCA_IInputDevice::KX_PAD2) 
793                 && (keyIndex <= SCA_IInputDevice::KX_PADPLUSKEY)) { 
794                 return pad2topadplus[keyIndex - SCA_IInputDevice::KX_PAD2];
795         }
796
797         return '!';
798 }
799
800
801
802 /**
803  * Determine whether this character can be printed. We cannot use
804  * the library functions here, because we need to test our own
805  * keycodes. */
806 bool IsPrintable(int keyIndex)
807 {
808         /* only print 
809          * - numerals: KX_ZEROKEY to KX_NINEKEY
810          * - alphas:   KX_AKEY to KX_ZKEY. 
811          * - specials: KX_RETKEY, KX_PADASTERKEY, KX_PADCOMMAKEY to KX_PERIODKEY,
812          *             KX_TABKEY , KX_SEMICOLONKEY to KX_RIGHTBRACKETKEY, 
813          *             KX_PAD2 to KX_PADPLUSKEY
814          * - delete and backspace: also printable in the sense that they modify 
815          *                         the string
816          * - retkey: should this be printable?
817          * - virgule: prints a space... don't know which key that's supposed
818          *   to be...
819          */
820         if ( ((keyIndex >= SCA_IInputDevice::KX_ZEROKEY) 
821                   && (keyIndex <= SCA_IInputDevice::KX_NINEKEY))
822                  || ((keyIndex >= SCA_IInputDevice::KX_AKEY) 
823                          && (keyIndex <= SCA_IInputDevice::KX_ZKEY)) 
824                  || (keyIndex == SCA_IInputDevice::KX_SPACEKEY) 
825                  || (keyIndex == SCA_IInputDevice::KX_RETKEY)
826                  || (keyIndex == SCA_IInputDevice::KX_PADENTER)
827                  || (keyIndex == SCA_IInputDevice::KX_PADASTERKEY) 
828                  || (keyIndex == SCA_IInputDevice::KX_TABKEY) 
829                  || ((keyIndex >= SCA_IInputDevice::KX_COMMAKEY) 
830                          && (keyIndex <= SCA_IInputDevice::KX_PERIODKEY)) 
831                  || ((keyIndex >= SCA_IInputDevice::KX_SEMICOLONKEY) 
832                          && (keyIndex <= SCA_IInputDevice::KX_RIGHTBRACKETKEY)) 
833                  || ((keyIndex >= SCA_IInputDevice::KX_PAD2) 
834                          && (keyIndex <= SCA_IInputDevice::KX_PADPLUSKEY)) 
835                  || (keyIndex == SCA_IInputDevice::KX_DELKEY)
836                  || (keyIndex == SCA_IInputDevice::KX_BACKSPACEKEY)
837                 )
838         {
839                 return true;
840         } else {
841                 return false;
842         }
843 }
844
845 /**
846  * Tests whether this is a delete key.
847  */     
848 bool IsDelete(int keyIndex)
849 {
850         if ( (keyIndex == SCA_IInputDevice::KX_DELKEY)
851                  || (keyIndex == SCA_IInputDevice::KX_BACKSPACEKEY) ) {
852                 return true;
853         } else {
854                 return false;
855         }
856 }