KeyboardEvent.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright (c) 2021-2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/CharacterTypes.h>
  7. #include <LibWeb/Bindings/Intrinsics.h>
  8. #include <LibWeb/UIEvents/KeyboardEvent.h>
  9. namespace Web::UIEvents {
  10. // https://www.w3.org/TR/uievents/#determine-keydown-keyup-keyCode
  11. static unsigned long determine_key_code(KeyCode platform_key, u32 code_point)
  12. {
  13. // If input key when pressed without modifiers would insert a numerical character (0-9), return the ASCII code of that numerical character.
  14. if (is_ascii_digit(code_point))
  15. return code_point;
  16. // If input key when pressed without modifiers would insert a lower case character in the a-z alphabetical range, return the ASCII code of the upper case equivalent.
  17. if (is_ascii_lower_alpha(code_point))
  18. return to_ascii_uppercase(code_point);
  19. // If the key’s function, as determined in an implementation-specific way, corresponds to one of the keys in the §8.3.3 Fixed virtual key codes table, return the corresponding key code.
  20. // https://www.w3.org/TR/uievents/#fixed-virtual-key-codes
  21. switch (platform_key) {
  22. case KeyCode::Key_Backspace:
  23. return 8;
  24. case KeyCode::Key_Tab:
  25. return 9;
  26. case KeyCode::Key_Return:
  27. return 13;
  28. case KeyCode::Key_Shift:
  29. return 16;
  30. case KeyCode::Key_Control:
  31. return 17;
  32. case KeyCode::Key_Alt:
  33. return 18;
  34. case KeyCode::Key_CapsLock:
  35. return 20;
  36. case KeyCode::Key_Escape:
  37. return 27;
  38. case KeyCode::Key_Space:
  39. return 32;
  40. case KeyCode::Key_PageUp:
  41. return 33;
  42. case KeyCode::Key_PageDown:
  43. return 34;
  44. case KeyCode::Key_End:
  45. return 35;
  46. case KeyCode::Key_Home:
  47. return 36;
  48. case KeyCode::Key_Left:
  49. return 37;
  50. case KeyCode::Key_Up:
  51. return 38;
  52. case KeyCode::Key_Right:
  53. return 39;
  54. case KeyCode::Key_Down:
  55. return 40;
  56. default:
  57. break;
  58. }
  59. // Return the virtual key code from the operating system.
  60. return platform_key;
  61. }
  62. WebIDL::ExceptionOr<JS::NonnullGCPtr<KeyboardEvent>> KeyboardEvent::create_from_platform_event(JS::Realm& realm, FlyString const& event_name, KeyCode platform_key, unsigned modifiers, u32 code_point)
  63. {
  64. auto& vm = realm.vm();
  65. // FIXME: Figure out what these should actually contain.
  66. auto event_key = TRY_OR_THROW_OOM(vm, String::from_deprecated_string(key_code_to_string(platform_key)));
  67. auto event_code = TRY_OR_THROW_OOM(vm, "FIXME"_string);
  68. auto key_code = determine_key_code(platform_key, code_point);
  69. KeyboardEventInit event_init {};
  70. event_init.key = move(event_key);
  71. event_init.code = move(event_code);
  72. event_init.location = 0;
  73. event_init.ctrl_key = modifiers & Mod_Ctrl;
  74. event_init.shift_key = modifiers & Mod_Shift;
  75. event_init.alt_key = modifiers & Mod_Alt;
  76. event_init.meta_key = false;
  77. event_init.repeat = false;
  78. event_init.is_composing = false;
  79. event_init.key_code = key_code;
  80. event_init.char_code = code_point;
  81. event_init.bubbles = true;
  82. event_init.cancelable = true;
  83. event_init.composed = true;
  84. return KeyboardEvent::create(realm, event_name, event_init);
  85. }
  86. bool KeyboardEvent::get_modifier_state(String const& key_arg)
  87. {
  88. if (key_arg == "Alt")
  89. return m_alt_key;
  90. if (key_arg == "Control")
  91. return m_ctrl_key;
  92. if (key_arg == "Shift")
  93. return m_shift_key;
  94. if (key_arg == "Meta")
  95. return m_meta_key;
  96. return false;
  97. }
  98. WebIDL::ExceptionOr<JS::NonnullGCPtr<KeyboardEvent>> KeyboardEvent::create(JS::Realm& realm, FlyString const& event_name, KeyboardEventInit const& event_init)
  99. {
  100. return MUST_OR_THROW_OOM(realm.heap().allocate<KeyboardEvent>(realm, realm, event_name, event_init));
  101. }
  102. WebIDL::ExceptionOr<JS::NonnullGCPtr<KeyboardEvent>> KeyboardEvent::construct_impl(JS::Realm& realm, FlyString const& event_name, KeyboardEventInit const& event_init)
  103. {
  104. return create(realm, event_name, event_init);
  105. }
  106. KeyboardEvent::KeyboardEvent(JS::Realm& realm, FlyString const& event_name, KeyboardEventInit const& event_init)
  107. : UIEvent(realm, event_name, event_init)
  108. , m_key(event_init.key)
  109. , m_code(event_init.code)
  110. , m_location(event_init.location)
  111. , m_ctrl_key(event_init.ctrl_key)
  112. , m_shift_key(event_init.shift_key)
  113. , m_alt_key(event_init.alt_key)
  114. , m_meta_key(event_init.meta_key)
  115. , m_repeat(event_init.repeat)
  116. , m_is_composing(event_init.is_composing)
  117. , m_key_code(event_init.key_code)
  118. , m_char_code(event_init.char_code)
  119. {
  120. }
  121. KeyboardEvent::~KeyboardEvent() = default;
  122. JS::ThrowCompletionOr<void> KeyboardEvent::initialize(JS::Realm& realm)
  123. {
  124. MUST_OR_THROW_OOM(Base::initialize(realm));
  125. set_prototype(&Bindings::ensure_web_prototype<Bindings::KeyboardEventPrototype>(realm, "KeyboardEvent"));
  126. return {};
  127. }
  128. }