BarrierInputKeyboard.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. //! Constructor
  26. //! \param[in] inputDevice Reference to the input device being implemented
  27. InputDeviceKeyboardBarrier(AzFramework::InputDeviceKeyboard& inputDevice);
  28. ////////////////////////////////////////////////////////////////////////////////////////////
  29. //! Destructor
  30. ~InputDeviceKeyboardBarrier() override;
  31. private:
  32. ////////////////////////////////////////////////////////////////////////////////////////////
  33. //! \ref AzFramework::InputDeviceKeyboard::Implementation::IsConnected
  34. bool IsConnected() const override;
  35. ////////////////////////////////////////////////////////////////////////////////////////////
  36. //! \ref AzFramework::InputDeviceKeyboard::Implementation::HasTextEntryStarted
  37. bool HasTextEntryStarted() const override;
  38. ////////////////////////////////////////////////////////////////////////////////////////////
  39. //! \ref AzFramework::InputDeviceKeyboard::Implementation::TextEntryStart
  40. void TextEntryStart(const AzFramework::InputTextEntryRequests::VirtualKeyboardOptions& options) override;
  41. ////////////////////////////////////////////////////////////////////////////////////////////
  42. //! \ref AzFramework::InputDeviceKeyboard::Implementation::TextEntryStop
  43. void TextEntryStop() override;
  44. ////////////////////////////////////////////////////////////////////////////////////////////
  45. //! \ref AzFramework::InputDeviceKeyboard::Implementation::TickInputDevice
  46. void TickInputDevice() override;
  47. ////////////////////////////////////////////////////////////////////////////////////////////
  48. //! \ref RawInputNotificationsBarrier::OnRawKeyboardKeyDownEvent
  49. void OnRawKeyboardKeyDownEvent(uint32_t scanCode, ModifierMask activeModifiers) override;
  50. ////////////////////////////////////////////////////////////////////////////////////////////
  51. //! \ref RawInputNotificationsBarrier::OnRawKeyboardKeyUpEvent
  52. void OnRawKeyboardKeyUpEvent(uint32_t scanCode, ModifierMask activeModifiers) override;
  53. ////////////////////////////////////////////////////////////////////////////////////////////
  54. //! \ref RawInputNotificationsBarrier::OnRawKeyboardKeyRepeatEvent
  55. void OnRawKeyboardKeyRepeatEvent(uint32_t scanCode, ModifierMask activeModifiers) override;
  56. ////////////////////////////////////////////////////////////////////////////////////////////
  57. //! Thread safe method to queue raw key events to be processed in the main thread update
  58. //! \param[in] scanCode The scan code of the key
  59. //! \param[in] rawKeyState The raw key state
  60. void ThreadSafeQueueRawKeyEvent(uint32_t scanCode, bool rawKeyState);
  61. ////////////////////////////////////////////////////////////////////////////////////////////
  62. //! Thread safe method to queue raw text events to be processed in the main thread update
  63. //! \param[in] textUTF8 The text to queue (encoded using UTF-8)
  64. void ThreadSafeQueueRawTextEvent(const AZStd::string& textUTF8);
  65. ////////////////////////////////////////////////////////////////////////////////////////////
  66. //! Translate a key event to an ASCII character. This is required because Barrier only sends
  67. //! raw key events, not translated text input. While we would ideally support the full range
  68. //! of UTF-8 text input, that is beyond the scope of this debug/development only class. Note
  69. //! that this function assumes an ANSI mechanical keyboard layout with a standard QWERTY key
  70. //! mapping, and will not produce correct results if used with other key layouts or mappings.
  71. //! \param[in] scanCode The scan code of the key
  72. //! \param[in] activeModifiers The bit mask of currently active modifier keys
  73. //! \return If the scan code and active modifiers produce a valid ASCII character
  74. char TranslateRawKeyEventToASCIIChar(uint32_t scanCode, ModifierMask activeModifiers);
  75. ////////////////////////////////////////////////////////////////////////////////////////////
  76. // Variables
  77. RawKeyEventQueueByIdMap m_threadAwareRawKeyEventQueuesById;
  78. AZStd::mutex m_threadAwareRawKeyEventQueuesByIdMutex;
  79. AZStd::vector<AZStd::string> m_threadAwareRawTextEventQueue;
  80. AZStd::mutex m_threadAwareRawTextEventQueueMutex;
  81. bool m_hasTextEntryStarted;
  82. };
  83. struct InputDeviceKeyboardBarrierImplFactory
  84. : public AzFramework::InputDeviceKeyboard::ImplementationFactory
  85. {
  86. AZStd::unique_ptr<AzFramework::InputDeviceKeyboard::Implementation> Create(AzFramework::InputDeviceKeyboard& inputDevice) override;
  87. };
  88. } // namespace BarrierInput