00001 /*************************************************************************** 00002 * Copyright (C) 2007 by * 00003 * Pierre-yves JEZEQUEL, Julien MICHOT, Loic MOISAN, * 00004 * Julien PAPILLON, Sebastien PINEAUD, Barthelemy SERRES * 00005 * * 00006 * https://sourceforge.net/projects/anidam * 00007 * * 00008 * This program is free software; you can redistribute it and/or modify * 00009 * it under the terms of the GNU General Public License as published by * 00010 * the Free Software Foundation; either version 2 of the License, or * 00011 * (at your option) any later version. * 00012 * * 00013 * This program is distributed in the hope that it will be useful, * 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00016 * GNU General Public License for more details. * 00017 * * 00018 * You should have received a copy of the GNU General Public License * 00019 * along with this program; if not, write to the * 00020 * Free Software Foundation, Inc., * 00021 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 00022 ***************************************************************************/ 00023 #ifndef KEYBOARD_HANDLER_H 00024 #define KEYBOARD_HANDLER_H 00025 #include <osgGA/GUIEventHandler> 00026 00027 00028 class keyboardEventHandler : public osgGA::GUIEventHandler 00029 { 00030 public: 00031 00032 typedef void (*functionType) (); 00033 enum keyStatusType 00034 { 00035 KEY_UP, KEY_DOWN 00036 }; 00037 00038 // A struct for storing current status of each key and 00039 // function to execute. Keep track of key's state to avoid 00040 // redundant calls. (If the key is already down, don't call the 00041 // key down method again.) 00042 struct functionStatusType 00043 { 00044 functionStatusType() {keyState = KEY_UP; keyFunction = NULL;} 00045 functionType keyFunction; 00046 keyStatusType keyState; 00047 }; 00048 00049 // Storage for list of registered key, function to execute and 00050 // current state of key. 00051 typedef std::map<int, functionStatusType > keyFunctionMap; 00052 00053 // Function to associate a key with a function. If the key has not 00054 // been previously registered, key and function are added to the 00055 // map of 'key down' events and 'true' is returned. Otherwise, no 00056 // entry made and false is returned. 00057 bool addFunction(int whatKey, functionType newFunction); 00058 00059 // Overloded version allows users to specify if the function should 00060 // be associated with KEY_UP or KEY_DOWN event. 00061 bool addFunction(int whatKey, keyStatusType keyPressStatus, 00062 functionType newFunction); 00063 00064 // The handle method checks the current key down event against 00065 // list of registered key/key status entries. If a match is found 00066 // and it's a new event (key was not already down) corresponding 00067 // function is invoked. 00068 virtual bool handle(const osgGA::GUIEventAdapter& ea, 00069 osgGA::GUIActionAdapter&); 00070 00071 // Overloaded accept method for dealing with event handler visitors 00072 virtual void accept(osgGA::GUIEventHandlerVisitor& v) 00073 { v.visit(*this); }; 00074 00075 protected: 00076 00077 // Storage for registered 'key down' methods and key status 00078 keyFunctionMap keyFuncMap; 00079 00080 // Storage for registered 'key up' methods and key status 00081 keyFunctionMap keyUPFuncMap; 00082 }; 00083 #endif