llkeyboard.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /**
  2. * @file llkeyboard.h
  3. * @brief Handler for assignable key bindings
  4. *
  5. * $LicenseInfo:firstyear=2001&license=viewergpl$
  6. *
  7. * Copyright (c) 2001-2009, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #ifndef LL_LLKEYBOARD_H
  33. #define LL_LLKEYBOARD_H
  34. #include <map>
  35. #include "indra_constants.h"
  36. #include "llpreprocessor.h"
  37. #include "llstringtable.h"
  38. #include "lltimer.h"
  39. enum EKeystate
  40. {
  41. KEYSTATE_DOWN,
  42. KEYSTATE_LEVEL,
  43. KEYSTATE_UP
  44. };
  45. typedef void (*LLKeyFunc)(EKeystate keystate);
  46. enum EKeyboardInsertMode
  47. {
  48. LL_KIM_INSERT,
  49. LL_KIM_OVERWRITE
  50. };
  51. class LLKeyBinding
  52. {
  53. public:
  54. KEY mKey;
  55. MASK mMask;
  56. #if 0 // unused
  57. const char* mName;
  58. #endif
  59. LLKeyFunc mFunction;
  60. };
  61. class LLWindowCallbacks;
  62. class LLKeyboard
  63. {
  64. protected:
  65. LOG_CLASS(LLKeyboard);
  66. public:
  67. typedef enum e_numpad_distinct
  68. {
  69. ND_NEVER,
  70. ND_NUMLOCK_OFF,
  71. ND_NUMLOCK_ON
  72. } ENumpadDistinct;
  73. public:
  74. LLKeyboard();
  75. virtual ~LLKeyboard() {}
  76. void resetKeys();
  77. LL_INLINE F32 getCurKeyElapsedTime() { return getKeyDown(mCurScanKey) ? getKeyElapsedTime(mCurScanKey) : 0.f; }
  78. LL_INLINE F32 getCurKeyElapsedFrameCount() { return getKeyDown(mCurScanKey) ? (F32)getKeyElapsedFrameCount(mCurScanKey) : 0.f; }
  79. LL_INLINE bool getKeyDown(KEY key) { return mKeyLevel[key]; }
  80. LL_INLINE bool getKeyRepeated(KEY key) { return mKeyRepeated[key]; }
  81. bool translateKey(U32 os_key, KEY* translated_key, MASK mask);
  82. U32 inverseTranslateKey(KEY translated_key);
  83. // Translated into "Linden" keycodes
  84. bool handleTranslatedKeyUp(KEY translated_key, U32 translated_mask);
  85. // Translated into "Linden" keycodes
  86. bool handleTranslatedKeyDown(KEY translated_key, U32 translated_mask);
  87. virtual bool handleKeyUp(U32 key, MASK mask) = 0;
  88. virtual bool handleKeyDown(U32 key, MASK mask) = 0;
  89. #if LL_DARWIN
  90. // We only actually use this for OS X.
  91. virtual void handleModifier(MASK mask) = 0;
  92. #endif
  93. // Asynchronously poll the control, alt, and shift keys and set the
  94. // appropriate internal key masks.
  95. virtual void resetMaskKeys() = 0;
  96. // scans keyboard, calls functions as necessary:
  97. virtual void scanKeyboard() = 0;
  98. // Mac must differentiate between Command = Control for keyboard events
  99. // and Command != Control for mouse events.
  100. virtual MASK currentMask(bool for_mouse_event) = 0;
  101. LL_INLINE virtual KEY currentKey() { return mCurTranslatedKey; }
  102. LL_INLINE EKeyboardInsertMode getInsertMode() { return mInsertMode; }
  103. void toggleInsertMode();
  104. // false on failure
  105. static bool maskFromString(const char* str, MASK* mask);
  106. static bool keyFromString(const char* str, KEY* key);
  107. static std::string stringFromKey(KEY key);
  108. e_numpad_distinct getNumpadDistinct() { return mNumpadDistinct; }
  109. void setNumpadDistinct(e_numpad_distinct val) { mNumpadDistinct = val; }
  110. void setCallbacks(LLWindowCallbacks* cbs) { mCallbacks = cbs; }
  111. // Returns time in seconds since key was pressed:
  112. F32 getKeyElapsedTime(KEY key);
  113. // Returns time in frames since key was pressed:
  114. S32 getKeyElapsedFrameCount(KEY key);
  115. protected:
  116. void addKeyName(KEY key, const std::string& name);
  117. protected:
  118. // Map of translations from OS keys to Linden KEYs
  119. std::map<U32, KEY> mTranslateKeyMap;
  120. // Map of translations from Linden KEYs to OS keys
  121. std::map<KEY, U32> mInvTranslateKeyMap;
  122. LLWindowCallbacks* mCallbacks;
  123. LLTimer mKeyLevelTimer[KEY_COUNT]; // Time since level was set
  124. S32 mKeyLevelFrameCount[KEY_COUNT]; // Frames since level was set
  125. KEY mCurTranslatedKey;
  126. KEY mCurScanKey; // Used during the scanKeyboard()
  127. e_numpad_distinct mNumpadDistinct;
  128. EKeyboardInsertMode mInsertMode;
  129. bool mKeyLevel[KEY_COUNT]; // Levels
  130. bool mKeyRepeated[KEY_COUNT]; // Key was repeated
  131. bool mKeyUp[KEY_COUNT]; // Up edge
  132. bool mKeyDown[KEY_COUNT]; // Down edge
  133. static std::map<KEY, std::string> sKeysToNames;
  134. static std::map<std::string, KEY> sNamesToKeys;
  135. };
  136. extern LLKeyboard* gKeyboardp;
  137. #endif