OSDLKeyboardHandler.h

Go to the documentation of this file.
00001 #ifndef OSDL_KEYBOARD_HANDLER_H_
00002 #define OSDL_KEYBOARD_HANDLER_H_
00003 
00004 
00005 #include "OSDLInputDeviceHandler.h"   // for inheritance
00006 #include "OSDLEventsCommon.h"         // for KeyModifier
00007 #include "OSDLEvents.h"               // for EventsModule friendship
00008 
00009 #include "Ceylan.h"                   // for Unicode
00010 
00011 #include <string>
00012 #include <list>
00013 #include <map>
00014 
00015 
00016 
00017 namespace OSDL
00018 {
00019 
00020 
00021     namespace MVC
00022     {
00023     
00024         /*
00025          * The keyboard handler can propagate keyboard events to the 
00026          * relevant controllers.
00027          *
00028          */
00029         class Controller ;
00030         
00031     }
00032     
00033         
00034     namespace Events
00035     {
00036             
00037         
00051         typedef void (* KeyboardEventHandler)( 
00052             const KeyboardEvent & keyboardEvent ) /* throw() */ ;
00053         
00054         
00055         
00057         class OSDL_DLL KeyboardException: public EventsException 
00058         {
00059             public: 
00060                 explicit KeyboardException( const std::string & reason ) 
00061                     throw() ; 
00062                 virtual ~KeyboardException() throw() ; 
00063                 
00064         } ;
00065 
00066 
00067                 
00088         enum KeyboardMode { rawInput, textInput } ;
00089 
00090         
00098         class OSDL_DLL KeyboardHandler : public InputDeviceHandler
00099         {
00100         
00101         
00102             /*
00103              * The events module has to trigger the keyboard event callbacks
00104              * of this keyboard handler.
00105              *
00106              */
00107             friend class OSDL::Events::EventsModule ;
00108 
00109             
00110             public:
00111 
00112 
00113 
00127                 enum KeyIdentifier
00128                 {
00129                 
00130                 
00131                     /*
00132                      * The keyboard identifiers have been cleverly chosen to 
00133                      * map to ASCII :
00134                      *
00135                      */
00136                 
00137                     // For unknown keys :   
00138                     //UnknownKey        = SDLK_UNKNOWN      ,  
00139                     
00140                     //FirstKey          = SDLK_FIRST        , // 
00141                     
00142                     BackspaceKey        = SDLK_BACKSPACE    , // 
00143                     TabKey              = SDLK_TAB          , // 
00144                     ClearKey            = SDLK_CLEAR        , // 
00145                     
00146                     // See also : EnterKeypadKey
00147                     EnterKey            = SDLK_RETURN       , 
00148                     
00149                      // Synonym of EnterKey :
00150                     ReturnKey           = SDLK_RETURN       ,
00151                     
00152                     PauseKey            = SDLK_PAUSE        , // 
00153                     EscapeKey           = SDLK_ESCAPE       , // 
00154                     SpaceKey            = SDLK_SPACE        , // 
00155                     ExclaimKey          = SDLK_EXCLAIM      , // 
00156                     QuotedBLKey         = SDLK_QUOTEDBL     , // 
00157                     HashKey             = SDLK_HASH         , // 
00158                     DollarKey           = SDLK_DOLLAR       , // 
00159                     AmpersandKey        = SDLK_AMPERSAND    , // 
00160                     QuoteKey            = SDLK_QUOTE        , // 
00161                     LeftParenthesisKey  = SDLK_LEFTPAREN    , // 
00162                     RightParenthesisKey = SDLK_RIGHTPAREN   , // 
00163                     AsteriskKey         = SDLK_ASTERISK     , // 
00164                     PlusKey             = SDLK_PLUS         , // 
00165                     CommaKey            = SDLK_COMMA        , // 
00166                     MinusKey            = SDLK_MINUS        , // 
00167                     PeriodKey           = SDLK_PERIOD       , // 
00168                     SlashKey            = SDLK_SLASH        , // 
00169                     ZeroKey             = SDLK_0            , // 
00170                     OneKey              = SDLK_1            , // 
00171                     TwoKey              = SDLK_2            , // 
00172                     ThreeKey            = SDLK_3            , // 
00173                     FourKey             = SDLK_4            , // 
00174                     FiveKey             = SDLK_5            , // 
00175                     SixKey              = SDLK_6            , // 
00176                     SevenKey            = SDLK_7            , // 
00177                     HeightKey           = SDLK_8            , // 
00178                     NineKey             = SDLK_9            , // 
00179                     ColonKey            = SDLK_COLON        , // 
00180                     SemiColonKey        = SDLK_SEMICOLON    , // 
00181                     LessKey             = SDLK_LESS         , // 
00182                     EqualsKey           = SDLK_EQUALS       , // 
00183                     GreaterKey          = SDLK_GREATER      , // 
00184                     QuestionKey         = SDLK_QUESTION     , // 
00185                     AtKey               = SDLK_AT           , // 
00186 
00187                     
00188                     // Skip uppercase letters.
00189                     
00190                     LeftBracketKey      = SDLK_LEFTBRACKET  , // 
00191                     RightBracket        = SDLK_RIGHTBRACKET , // 
00192                     BackslashKey        = SDLK_BACKSLASH    , // 
00193                     Caret               = SDLK_CARET        , // 
00194                     Underscore          = SDLK_UNDERSCORE   , // 
00195                     BackQuote           = SDLK_BACKQUOTE    , // 
00196                     aKey                = SDLK_a            , // 
00197                     bKey                = SDLK_b            , // 
00198                     cKey                = SDLK_c            , // 
00199                     dKey                = SDLK_d            , // 
00200                     eKey                = SDLK_e            , // 
00201                     fKey                = SDLK_f            , // 
00202                     gKey                = SDLK_g            , // 
00203                     hKey                = SDLK_h            , // 
00204                     iKey                = SDLK_i            , // 
00205                     jKey                = SDLK_j            , // 
00206                     kKey                = SDLK_k            , // 
00207                     lKey                = SDLK_l            , // 
00208                     mKey                = SDLK_m            , // 
00209                     nKey                = SDLK_n            , // 
00210                     oKey                = SDLK_o            , // 
00211                     pKey                = SDLK_p            , // 
00212                     qKey                = SDLK_q            , // 
00213                     rKey                = SDLK_r            , // 
00214                     sKey                = SDLK_s            , // 
00215                     tKey                = SDLK_t            , // 
00216                     uKey                = SDLK_u            , // 
00217                     vKey                = SDLK_v            , // 
00218                     wKey                = SDLK_w            , // 
00219                     xKey                = SDLK_x            , // 
00220                     yKey                = SDLK_y            , // 
00221                     zKey                = SDLK_z            , // 
00222                     DeleteKey           = SDLK_DELETE       , // 
00223                     
00224                     // End of ASCII mapped key identifiers.
00225                     
00226                     
00227                     // International keyboard identifiers :
00228 
00229                     International0Key = SDLK_WORLD_0        , // 0xA0.
00230                     International1Key = SDLK_WORLD_1        ,
00231                     International2Key = SDLK_WORLD_2        ,
00232                     International3Key = SDLK_WORLD_3        ,
00233                     International4Key = SDLK_WORLD_4        ,
00234                     International5Key = SDLK_WORLD_5        ,
00235                     International6Key = SDLK_WORLD_6        ,
00236                     International7Key = SDLK_WORLD_7        ,
00237                     International8Key = SDLK_WORLD_8        ,
00238                     International9Key = SDLK_WORLD_9        ,
00239                     
00240                     International10Key = SDLK_WORLD_10      ,
00241                     International11Key = SDLK_WORLD_11      ,
00242                     International12Key = SDLK_WORLD_12      ,
00243                     International13Key = SDLK_WORLD_13      ,
00244                     International14Key = SDLK_WORLD_14      ,
00245                     International15Key = SDLK_WORLD_15      ,
00246                     International16Key = SDLK_WORLD_16      ,
00247                     International17Key = SDLK_WORLD_17      ,
00248                     International18Key = SDLK_WORLD_18      ,
00249                     International19Key = SDLK_WORLD_19      ,
00250                     
00251                     International20Key = SDLK_WORLD_20      ,
00252                     International21Key = SDLK_WORLD_21      ,
00253                     International22Key = SDLK_WORLD_22      ,
00254                     International23Key = SDLK_WORLD_23      ,
00255                     International24Key = SDLK_WORLD_24      ,
00256                     International25Key = SDLK_WORLD_25      ,
00257                     International26Key = SDLK_WORLD_26      ,
00258                     International27Key = SDLK_WORLD_27      ,
00259                     International28Key = SDLK_WORLD_28      ,
00260                     International29Key = SDLK_WORLD_29      ,
00261                     
00262                     International30Key = SDLK_WORLD_30      ,
00263                     International31Key = SDLK_WORLD_31      ,
00264                     International32Key = SDLK_WORLD_32      ,
00265                     International33Key = SDLK_WORLD_33      ,
00266                     International34Key = SDLK_WORLD_34      ,
00267                     International35Key = SDLK_WORLD_35      ,
00268                     International36Key = SDLK_WORLD_36      ,
00269                     International37Key = SDLK_WORLD_37      ,
00270                     International38Key = SDLK_WORLD_38      ,
00271                     International39Key = SDLK_WORLD_39      ,
00272                                                
00273                     International40Key = SDLK_WORLD_40      ,
00274                     International41Key = SDLK_WORLD_41      ,
00275                     International42Key = SDLK_WORLD_42      ,
00276                     International43Key = SDLK_WORLD_43      ,
00277                     International44Key = SDLK_WORLD_44      ,
00278                     International45Key = SDLK_WORLD_45      ,
00279                     International46Key = SDLK_WORLD_46      ,
00280                     International47Key = SDLK_WORLD_47      ,
00281                     International48Key = SDLK_WORLD_48      ,
00282                     International49Key = SDLK_WORLD_49      ,
00283     
00284                     International50Key = SDLK_WORLD_50      ,
00285                     International51Key = SDLK_WORLD_51      ,
00286                     International52Key = SDLK_WORLD_52      ,
00287                     International53Key = SDLK_WORLD_53      ,
00288                     International54Key = SDLK_WORLD_54      ,
00289                     International55Key = SDLK_WORLD_55      ,
00290                     International56Key = SDLK_WORLD_56      ,
00291                     International57Key = SDLK_WORLD_57      ,
00292                     International58Key = SDLK_WORLD_58      ,
00293                     International59Key = SDLK_WORLD_59      ,
00294     
00295                     International60Key = SDLK_WORLD_60      ,
00296                     International61Key = SDLK_WORLD_61      ,
00297                     International62Key = SDLK_WORLD_62      ,
00298                     International63Key = SDLK_WORLD_63      ,
00299                     International64Key = SDLK_WORLD_64      ,
00300                     International65Key = SDLK_WORLD_65      ,
00301                     International66Key = SDLK_WORLD_66      ,
00302                     International67Key = SDLK_WORLD_67      ,
00303                     International68Key = SDLK_WORLD_68      ,
00304                     International69Key = SDLK_WORLD_69      ,
00305     
00306                     International70Key = SDLK_WORLD_70      ,
00307                     International71Key = SDLK_WORLD_71      ,
00308                     International72Key = SDLK_WORLD_72      ,
00309                     International73Key = SDLK_WORLD_73      ,
00310                     International74Key = SDLK_WORLD_74      ,
00311                     International75Key = SDLK_WORLD_75      ,
00312                     International76Key = SDLK_WORLD_76      ,
00313                     International77Key = SDLK_WORLD_77      ,
00314                     International78Key = SDLK_WORLD_78      ,
00315                     International79Key = SDLK_WORLD_79      ,
00316     
00317                     International80Key = SDLK_WORLD_80      ,
00318                     International81Key = SDLK_WORLD_81      ,
00319                     International82Key = SDLK_WORLD_82      ,
00320                     International83Key = SDLK_WORLD_83      ,
00321                     International84Key = SDLK_WORLD_84      ,
00322                     International85Key = SDLK_WORLD_85      ,
00323                     International86Key = SDLK_WORLD_86      ,
00324                     International87Key = SDLK_WORLD_87      ,
00325                     International88Key = SDLK_WORLD_88      ,
00326                     International89Key = SDLK_WORLD_89      ,
00327     
00328                     International90Key = SDLK_WORLD_90      ,
00329                     International91Key = SDLK_WORLD_91      ,
00330                     International92Key = SDLK_WORLD_92      ,
00331                     International93Key = SDLK_WORLD_93      ,
00332                     International94Key = SDLK_WORLD_94      ,
00333                     International95Key = SDLK_WORLD_95      , // 0xFF.
00334 
00335                     // End of international keyboard identifiers.
00336                     
00337                     
00338                     // Numeric keypad :
00339                     ZeroKeypadKey       = SDLK_KP0          , // 
00340                     OneKeypadKey        = SDLK_KP1          , // 
00341                     TwoKeypadKey        = SDLK_KP2          , // 
00342                     ThreeKeypadKey      = SDLK_KP3          , // 
00343                     FourKeypadKey       = SDLK_KP4          , // 
00344                     FiveKeypadKey       = SDLK_KP5          , // 
00345                     SixKeypadKey        = SDLK_KP6          , // 
00346                     SevenKeypadKey      = SDLK_KP7          , // 
00347                     HeightKeypadKey     = SDLK_KP8          , // 
00348                     NineKeypadKey       = SDLK_KP9          , // 
00349 
00350 
00351                     // Rest of keypad :
00352                     PeriodKeypadKey     = SDLK_KP_PERIOD    , // 
00353                     DivideKeypadKey     = SDLK_KP_DIVIDE    , // 
00354                     MultiplyKeypadKey   = SDLK_KP_MULTIPLY  , // 
00355                     MinusKeypadKey      = SDLK_KP_MINUS     , // 
00356                     PlusKeypadKey       = SDLK_KP_PLUS      , //
00357                     
00358                     // See also : EnterKey. 
00359                     EnterKeypadKey      = SDLK_KP_ENTER     , 
00360                     EqualsKeypadKey     = SDLK_KP_EQUALS    , // 
00361 
00362 
00363                     // Arrows and Home/End pad :
00364                     UpArrowKey          = SDLK_UP           , // 
00365                     DownArrowKey        = SDLK_DOWN         , // 
00366                     LeftArrowKey        = SDLK_LEFT         , // 
00367                     RightArrowKey       = SDLK_RIGHT        , // 
00368                     InsertKey           = SDLK_INSERT       , // 
00369                     HomeKey             = SDLK_HOME         , // 
00370                     EndKey              = SDLK_END          , // 
00371                     PageUpKey           = SDLK_PAGEUP       , // 
00372                     PageDownKey         = SDLK_PAGEDOWN     , //  
00373 
00374 
00375                     // Function keys :
00376                     F1Key               = SDLK_F1           , // 
00377                     F2Key               = SDLK_F2           , // 
00378                     F3Key               = SDLK_F3           , // 
00379                     F4Key               = SDLK_F4           , // 
00380                     F5Key               = SDLK_F5           , // 
00381                     F6Key               = SDLK_F6           , // 
00382                     F7Key               = SDLK_F7           , // 
00383                     F8Key               = SDLK_F8           , // 
00384                     F9Key               = SDLK_F9           , // 
00385                     F10Key              = SDLK_F10          , // 
00386                     F11Key              = SDLK_F11          , // 
00387                     F12Key              = SDLK_F12          , // 
00388                     F13Key              = SDLK_F13          , // 
00389                     F14Key              = SDLK_F14          , // 
00390                     F15Key              = SDLK_F15          , // 
00391                     
00392                     
00393                     // Key state modifier keys :
00394                     NumLockKey          = SDLK_NUMLOCK      , // 
00395                     CapsLockKey         = SDLK_CAPSLOCK     , // 
00396                     ScrolLockKey        = SDLK_SCROLLOCK    , // 
00397                     LeftShiftKey        = SDLK_LSHIFT       , // 
00398                     RightShiftKey       = SDLK_RSHIFT       , // 
00399                     LeftControlKey      = SDLK_LCTRL        , // 
00400                     RightControlKey     = SDLK_RCTRL        , // 
00401                     LeftAltKey          = SDLK_LALT         , // 
00402                     RightAltKey         = SDLK_RALT         , // 
00403                     LeftMetaKey         = SDLK_LMETA        , // 
00404                     RightMetaKey        = SDLK_RMETA        , // 
00405                     
00406                     // Left "Windows" key :
00407                     LeftSuperKey        = SDLK_LSUPER       ,
00408                     
00409                     // Right "Windows" key : 
00410                     RightSuperKey       = SDLK_RSUPER       , 
00411                     ModeKey             = SDLK_MODE         , // "Alt Gr" key.
00412                     
00413                     // Multi-key compose key :
00414                     ComposeKey          = SDLK_COMPOSE      ,  
00415                     
00416                     
00417                     // Miscellaneous function keys :
00418                     HelpKey             = SDLK_HELP         , // 
00419                     PrintKey            = SDLK_PRINT        , // 
00420                     SysReqKey           = SDLK_SYSREQ       , // 
00421                     BreakKey            = SDLK_BREAK        , // 
00422                     MenuKey             = SDLK_MENU         , // 
00423                     
00424                     // Power Macintosh power key :
00425                     PowerKey            = SDLK_POWER        ,
00426                     
00427                     // Some european keyboards : 
00428                     EuroKey             = SDLK_EURO         ,
00429                     
00430                     // Atari keyboard has Undo : 
00431                     UndoKey             = SDLK_UNDO
00432 
00433     
00434                     // Add any other keys here.
00435 
00436                 } ;
00437 
00438 
00439 
00451                 enum KeyModifier
00452                 {
00453                     
00454                     
00455                     NoneModifier         = KMOD_NONE        ,
00456                     
00457                     LeftShiftModifier    = KMOD_LSHIFT      ,
00458                     RightShiftModifier   = KMOD_RSHIFT      ,
00459                     
00460                     LeftControlModifier  = KMOD_LCTRL       ,
00461                     RightControlModifier = KMOD_RCTRL       ,
00462                     
00463                     LeftAltModifier      = KMOD_LALT        ,
00464                     RightAltModifier     = KMOD_RALT        ,
00465                     
00466                     LeftMetaModifier     = KMOD_LMETA       ,
00467                     RightMetaModifier    = KMOD_RMETA       ,
00468                     
00469                     NumModifier          = KMOD_NUM         ,
00470                     CapsModifier         = KMOD_CAPS        ,
00471                     ModeModifier         = KMOD_MODE        ,
00472                     ReservedModifier     = KMOD_RESERVED    ,
00473                     
00474                     
00475                     /*
00476                      * Virtual modifiers, when left and right versions of 
00477                      * the modifier mean the same :
00478                      *
00479                      */
00480                     ShiftModifier        = LeftShiftModifier   
00481                         | RightShiftModifier,
00482                         
00483                     ControlModifier      = LeftControlModifier 
00484                         | RightControlModifier,
00485                         
00486                     AltModifier          = LeftAltModifier     
00487                         | RightAltModifier, 
00488                         
00489                     MetaModifier         = LeftMetaModifier    
00490                         | RightMetaModifier
00491                 
00492                 
00493                 } ;
00494                 
00495 
00496 
00515                 explicit KeyboardHandler( KeyboardMode initialMode = rawInput, 
00516                         bool useSmarterDefaultKeyHandler = false ) 
00517                     throw( InputDeviceHandlerException ) ;
00518                 
00519                 
00524                 virtual ~KeyboardHandler() throw() ;
00525                 
00526                                 
00543                 virtual void linkToController( KeyIdentifier rawKey, 
00544                     OSDL::MVC::Controller & controller ) throw() ;
00545                 
00546                 
00563                 virtual void linkToController( Ceylan::Unicode unicode, 
00564                     OSDL::MVC::Controller & controller ) throw() ;
00565                 
00566                 
00579                 virtual void linkToFocusController(
00580                     OSDL::MVC::Controller & controller ) throw() ;
00581                 
00582                 
00595                 virtual void linkToHandler( KeyIdentifier rawKey,
00596                     KeyboardEventHandler handler ) throw() ;
00597             
00598                 
00611                 virtual void linkToHandler( Ceylan::Unicode unicode,
00612                     KeyboardEventHandler handler ) throw() ;
00613                 
00614                 
00615                 
00627                 virtual void setSmarterDefaultKeyHandlers() throw() ;
00628                 
00629 
00636                 virtual void setDefaultRawKeyHandler( 
00637                     KeyboardEventHandler newHandler ) throw() ;
00638                 
00639                 
00646                 virtual void setDefaultUnicodeHandler( 
00647                     KeyboardEventHandler newHandler ) throw() ;
00648     
00649                                 
00661                 virtual const std::string toString( 
00662                         Ceylan::VerbosityLevels level = Ceylan::high ) 
00663                     const throw() ;
00664 
00665 
00666 
00667                 // Static section :
00668 
00669 
00681                 static KeyboardMode GetMode() throw() ;
00682                 
00683 
00693                 static void SetMode( KeyboardMode newMode ) throw() ;
00694 
00695 
00702                 static std::string DescribeKey( KeyIdentifier key ) throw() ;
00703                 
00704                 
00712                 static std::string DescribeModifier( KeyModifier modifier )
00713                     throw() ;
00714                 
00715                 
00722                 static std::string DescribeUnicode( Ceylan::Unicode value )
00723                     throw() ;
00724                 
00725                 
00731                 static const Ceylan::System::Millisecond
00732                     DefaultDelayBeforeKeyRepeat ;
00733     
00734     
00742                 static const Ceylan::System::Millisecond 
00743                     DefaulKeyRepeatInterval ;
00744     
00745                         
00746 
00747                             
00748         protected :
00749         
00750                 
00751                 
00759                 virtual void focusGained(
00760                     const FocusEvent & keyboardFocusEvent ) throw() ;
00761                 
00762                 
00770                 virtual void focusLost( 
00771                     const FocusEvent & keyboardFocusEvent ) throw() ;
00772                 
00773                 
00774                 
00790                 virtual void keyPressed( const KeyboardEvent & keyboardEvent )
00791                     throw() ;
00792                 
00793                 
00809                 virtual void keyReleased( const KeyboardEvent & keyboardEvent )
00810                     throw() ;
00811 
00812 
00813                 
00815                 static KeyboardMode _CurrentMode ;
00816         
00817         
00818 /* 
00819  * Takes care of the awful issue of Windows DLL with templates.
00820  *
00821  * @see Ceylan's developer guide and README-build-for-windows.txt 
00822  * to understand it, and to be aware of the associated risks. 
00823  * 
00824  */
00825 #pragma warning( push )
00826 #pragma warning( disable : 4251 )
00827         
00828         
00839                 std::map<KeyIdentifier, OSDL::MVC::Controller *>
00840                     _rawKeyControllerMap ;
00841                 
00842                 
00861                 std::map<KeyIdentifier, KeyboardEventHandler> 
00862                     _rawKeyHandlerMap ;
00863                 
00864                 
00878                 std::map<Ceylan::Unicode, OSDL::MVC::Controller *>
00879                     _unicodeControllerMap ;
00880                 
00881                 
00903                 std::map<Ceylan::Unicode, KeyboardEventHandler>
00904                     _unicodeHandlerMap ;
00905                 
00906 
00907 #pragma warning( pop ) 
00908 
00909 
00910 
00918                 KeyboardEventHandler _defaultRawKeyHandler ;
00919                 
00920                 
00928                 KeyboardEventHandler _defaultUnicodeHandler ;
00929                 
00930                 
00937                 OSDL::MVC::Controller * _focusController ;
00938                 
00939                 
00945                 bool _unicodeInputWasActivated ;
00946 
00947 
00948 
00949         private:
00950         
00951         
00960                 explicit KeyboardHandler( const KeyboardHandler & source )
00961                     throw() ;
00962             
00963             
00972                 KeyboardHandler & operator = ( const KeyboardHandler & source )
00973                     throw() ;
00974                                         
00975                 
00976         } ;
00977     
00978     }   
00979     
00980 }
00981 
00982 
00983 
00984 #endif // OSDL_KEYBOARD_HANDLER_H_

Generated on Fri Mar 30 14:46:59 2007 for OSDL by  doxygen 1.5.1