Shortcut.h 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Traits.h>
  8. #include <Kernel/API/KeyCode.h>
  9. namespace GUI {
  10. class Shortcut {
  11. public:
  12. Shortcut() = default;
  13. Shortcut(u8 modifiers, KeyCode key)
  14. : m_modifiers(modifiers)
  15. , m_key(key)
  16. {
  17. }
  18. Shortcut(KeyCode key)
  19. : m_modifiers(0)
  20. , m_key(key)
  21. {
  22. }
  23. bool is_valid() const { return m_key != KeyCode::Key_Invalid; }
  24. u8 modifiers() const { return m_modifiers; }
  25. KeyCode key() const { return m_key; }
  26. String to_string() const;
  27. bool operator==(Shortcut const& other) const
  28. {
  29. return m_modifiers == other.m_modifiers
  30. && m_key == other.m_key;
  31. }
  32. private:
  33. u8 m_modifiers { 0 };
  34. KeyCode m_key { KeyCode::Key_Invalid };
  35. };
  36. }
  37. namespace AK {
  38. template<>
  39. struct Traits<GUI::Shortcut> : public GenericTraits<GUI::Shortcut> {
  40. static unsigned hash(const GUI::Shortcut& shortcut)
  41. {
  42. return pair_int_hash(shortcut.modifiers(), shortcut.key());
  43. }
  44. };
  45. }