PageRenderTime 148ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/llwindow/llkeyboard.h

https://bitbucket.org/lindenlab/viewer-beta/
C Header | 148 lines | 88 code | 30 blank | 30 comment | 0 complexity | ac9fdae7fcbd3cf54c2ecbc209d81e20 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file llkeyboard.h
  3. * @brief Handler for assignable key bindings
  4. *
  5. * $LicenseInfo:firstyear=2001&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2010, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. #ifndef LL_LLKEYBOARD_H
  27. #define LL_LLKEYBOARD_H
  28. #include <map>
  29. #include "string_table.h"
  30. #include "lltimer.h"
  31. #include "indra_constants.h"
  32. enum EKeystate
  33. {
  34. KEYSTATE_DOWN,
  35. KEYSTATE_LEVEL,
  36. KEYSTATE_UP
  37. };
  38. typedef boost::function<void(EKeystate keystate)> LLKeyFunc;
  39. typedef std::string (LLKeyStringTranslatorFunc)(const char *label);
  40. enum EKeyboardInsertMode
  41. {
  42. LL_KIM_INSERT,
  43. LL_KIM_OVERWRITE
  44. };
  45. class LLKeyBinding
  46. {
  47. public:
  48. KEY mKey;
  49. MASK mMask;
  50. // const char *mName; // unused
  51. LLKeyFunc mFunction;
  52. };
  53. class LLWindowCallbacks;
  54. class LLKeyboard
  55. {
  56. public:
  57. typedef enum e_numpad_distinct
  58. {
  59. ND_NEVER,
  60. ND_NUMLOCK_OFF,
  61. ND_NUMLOCK_ON
  62. } ENumpadDistinct;
  63. public:
  64. LLKeyboard();
  65. virtual ~LLKeyboard();
  66. void resetKeys();
  67. F32 getCurKeyElapsedTime() { return getKeyDown(mCurScanKey) ? getKeyElapsedTime( mCurScanKey ) : 0.f; }
  68. F32 getCurKeyElapsedFrameCount() { return getKeyDown(mCurScanKey) ? (F32)getKeyElapsedFrameCount( mCurScanKey ) : 0.f; }
  69. BOOL getKeyDown(const KEY key) { return mKeyLevel[key]; }
  70. BOOL getKeyRepeated(const KEY key) { return mKeyRepeated[key]; }
  71. BOOL translateKey(const U16 os_key, KEY *translated_key);
  72. U16 inverseTranslateKey(const KEY translated_key);
  73. BOOL handleTranslatedKeyUp(KEY translated_key, U32 translated_mask); // Translated into "Linden" keycodes
  74. BOOL handleTranslatedKeyDown(KEY translated_key, U32 translated_mask); // Translated into "Linden" keycodes
  75. virtual BOOL handleKeyUp(const U16 key, MASK mask) = 0;
  76. virtual BOOL handleKeyDown(const U16 key, MASK mask) = 0;
  77. // Asynchronously poll the control, alt, and shift keys and set the
  78. // appropriate internal key masks.
  79. virtual void resetMaskKeys() = 0;
  80. virtual void scanKeyboard() = 0; // scans keyboard, calls functions as necessary
  81. // Mac must differentiate between Command = Control for keyboard events
  82. // and Command != Control for mouse events.
  83. virtual MASK currentMask(BOOL for_mouse_event) = 0;
  84. virtual KEY currentKey() { return mCurTranslatedKey; }
  85. EKeyboardInsertMode getInsertMode() { return mInsertMode; }
  86. void toggleInsertMode();
  87. static BOOL maskFromString(const std::string& str, MASK *mask); // False on failure
  88. static BOOL keyFromString(const std::string& str, KEY *key); // False on failure
  89. static std::string stringFromKey(KEY key);
  90. static std::string stringFromAccelerator( MASK accel_mask, KEY key );
  91. e_numpad_distinct getNumpadDistinct() { return mNumpadDistinct; }
  92. void setNumpadDistinct(e_numpad_distinct val) { mNumpadDistinct = val; }
  93. void setCallbacks(LLWindowCallbacks *cbs) { mCallbacks = cbs; }
  94. F32 getKeyElapsedTime( KEY key ); // Returns time in seconds since key was pressed.
  95. S32 getKeyElapsedFrameCount( KEY key ); // Returns time in frames since key was pressed.
  96. static void setStringTranslatorFunc( LLKeyStringTranslatorFunc *trans_func );
  97. protected:
  98. void addKeyName(KEY key, const std::string& name);
  99. protected:
  100. std::map<U16, KEY> mTranslateKeyMap; // Map of translations from OS keys to Linden KEYs
  101. std::map<KEY, U16> mInvTranslateKeyMap; // Map of translations from Linden KEYs to OS keys
  102. LLWindowCallbacks *mCallbacks;
  103. LLTimer mKeyLevelTimer[KEY_COUNT]; // Time since level was set
  104. S32 mKeyLevelFrameCount[KEY_COUNT]; // Frames since level was set
  105. BOOL mKeyLevel[KEY_COUNT]; // Levels
  106. BOOL mKeyRepeated[KEY_COUNT]; // Key was repeated
  107. BOOL mKeyUp[KEY_COUNT]; // Up edge
  108. BOOL mKeyDown[KEY_COUNT]; // Down edge
  109. KEY mCurTranslatedKey;
  110. KEY mCurScanKey; // Used during the scanKeyboard()
  111. static LLKeyStringTranslatorFunc* mStringTranslator; // Used for l10n + PC/Mac/Linux accelerator labeling
  112. e_numpad_distinct mNumpadDistinct;
  113. EKeyboardInsertMode mInsertMode;
  114. static std::map<KEY,std::string> sKeysToNames;
  115. static std::map<std::string,KEY> sNamesToKeys;
  116. };
  117. extern LLKeyboard *gKeyboard;
  118. #endif