BarrierInputKeyboard.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #pragma once
  9. #include <BarrierInput/RawInputNotificationBus_Barrier.h>
  10. #include <AzFramework/Input/Devices/Keyboard/InputDeviceKeyboard.h>
  11. #include <AzCore/std/parallel/mutex.h>
  12. ////////////////////////////////////////////////////////////////////////////////////////////////////
  13. namespace BarrierInput
  14. {
  15. ////////////////////////////////////////////////////////////////////////////////////////////////
  16. //! Barrier specific implementation for keyboard input devices.
  17. class InputDeviceKeyboardBarrier : public AzFramework::InputDeviceKeyboard::Implementation
  18. , public RawInputNotificationBusBarrier::Handler
  19. {
  20. public:
  21. ////////////////////////////////////////////////////////////////////////////////////////////
  22. // Allocator
  23. AZ_CLASS_ALLOCATOR(InputDeviceKeyboardBarrier, AZ::SystemAllocator);
  24. ////////////////////////////////////////////////////////////////////////////////////////////
  25. //! Custom factory create function
  26. //! \param[in] inputDevice Reference to the input device being implemented
  27. static Implementation* Create(AzFramework::InputDeviceKeyboard& inputDevice);
  28. ////////////////////////////////////////////////////////////////////////////////////////////
  29. //! Constructor
  30. //! \param[in] inputDevice Reference to the input device being implemented
  31. InputDeviceKeyboardBarrier(AzFramework::InputDeviceKeyboard& inputDevice);
  32. ////////////////////////////////////////////////////////////////////////////////////////////
  33. //! Destructor
  34. ~InputDeviceKeyboardBarrier() override;
  35. private:
  36. ////////////////////////////////////////////////////////////////////////////////////////////
  37. //! \ref AzFramework::InputDeviceKeyboard::Implementation::IsConnected
  38. bool IsConnected() const override;
  39. ////////////////////////////////////////////////////////////////////////////////////////////
  40. //! \ref AzFramework::InputDeviceKeyboard::Implementation::HasTextEntryStarted
  41. bool HasTextEntryStarted() const override;
  42. ////////////////////////////////////////////////////////////////////////////////////////////
  43. //! \ref AzFramework::InputDeviceKeyboard::Implementation::TextEntryStart
  44. void TextEntryStart(const AzFramework::InputTextEntryRequests::VirtualKeyboardOptions& options) override;
  45. ////////////////////////////////////////////////////////////////////////////////////////////
  46. //! \ref AzFramework::InputDeviceKeyboard::Implementation::TextEntryStop
  47. void TextEntryStop() override;
  48. ////////////////////////////////////////////////////////////////////////////////////////////
  49. //! \ref AzFramework::InputDeviceKeyboard::Implementation::TickInputDevice
  50. void TickInputDevice() override;
  51. ////////////////////////////////////////////////////////////////////////////////////////////
  52. //! \ref RawInputNotificationsBarrier::OnRawKeyboardKeyDownEvent
  53. void OnRawKeyboardKeyDownEvent(uint32_t scanCode, ModifierMask activeModifiers) override;
  54. ////////////////////////////////////////////////////////////////////////////////////////////
  55. //! \ref RawInputNotificationsBarrier::OnRawKeyboardKeyUpEvent
  56. void OnRawKeyboardKeyUpEvent(uint32_t scanCode, ModifierMask activeModifiers) override;
  57. ////////////////////////////////////////////////////////////////////////////////////////////
  58. //! \ref RawInputNotificationsBarrier::OnRawKeyboardKeyRepeatEvent
  59. void OnRawKeyboardKeyRepeatEvent(uint32_t scanCode, ModifierMask activeModifiers) override;
  60. ////////////////////////////////////////////////////////////////////////////////////////////
  61. //! Thread safe method to queue raw key events to be processed in the main thread update
  62. //! \param[in] scanCode The scan code of the key
  63. //! \param[in] rawKeyState The raw key state
  64. void ThreadSafeQueueRawKeyEvent(uint32_t scanCode, bool rawKeyState);
  65. ////////////////////////////////////////////////////////////////////////////////////////////
  66. //! Thread safe method to queue raw text events to be processed in the main thread update
  67. //! \param[in] textUTF8 The text to queue (encoded using UTF-8)
  68. void ThreadSafeQueueRawTextEvent(const AZStd::string& textUTF8);
  69. ////////////////////////////////////////////////////////////////////////////////////////////
  70. //! Translate a key event to an ASCII character. This is required because Barrier only sends
  71. //! raw key events, not translated text input. While we would ideally support the full range
  72. //! of UTF-8 text input, that is beyond the scope of this debug/development only class. Note
  73. //! that this function assumes an ANSI mechanical keyboard layout with a standard QWERTY key
  74. //! mapping, and will not produce correct results if used with other key layouts or mappings.
  75. //! \param[in] scanCode The scan code of the key
  76. //! \param[in] activeModifiers The bit mask of currently active modifier keys
  77. //! \return If the scan code and active modifiers produce a valid ASCII character
  78. char TranslateRawKeyEventToASCIIChar(uint32_t scanCode, ModifierMask activeModifiers);
  79. ////////////////////////////////////////////////////////////////////////////////////////////
  80. // Variables
  81. RawKeyEventQueueByIdMap m_threadAwareRawKeyEventQueuesById;
  82. AZStd::mutex m_threadAwareRawKeyEventQueuesByIdMutex;
  83. AZStd::vector<AZStd::string> m_threadAwareRawTextEventQueue;
  84. AZStd::mutex m_threadAwareRawTextEventQueueMutex;
  85. bool m_hasTextEntryStarted;
  86. };
  87. } // namespace BarrierInput