KeyboardEvent.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/CharacterTypes.h>
  7. #include <LibWeb/UIEvents/KeyboardEvent.h>
  8. namespace Web::UIEvents {
  9. // https://www.w3.org/TR/uievents/#determine-keydown-keyup-keyCode
  10. static unsigned long determine_key_code(KeyCode platform_key, u32 code_point)
  11. {
  12. // If input key when pressed without modifiers would insert a numerical character (0-9), return the ASCII code of that numerical character.
  13. if (is_ascii_digit(code_point))
  14. return code_point;
  15. // 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.
  16. if (is_ascii_lower_alpha(code_point))
  17. return to_ascii_uppercase(code_point);
  18. // 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.
  19. // https://www.w3.org/TR/uievents/#fixed-virtual-key-codes
  20. switch (platform_key) {
  21. case KeyCode::Key_Backspace:
  22. return 8;
  23. case KeyCode::Key_Tab:
  24. return 9;
  25. case KeyCode::Key_Return:
  26. return 13;
  27. case KeyCode::Key_Shift:
  28. return 16;
  29. case KeyCode::Key_Control:
  30. return 17;
  31. case KeyCode::Key_Alt:
  32. return 18;
  33. case KeyCode::Key_CapsLock:
  34. return 20;
  35. case KeyCode::Key_Escape:
  36. return 27;
  37. case KeyCode::Key_Space:
  38. return 32;
  39. case KeyCode::Key_PageUp:
  40. return 33;
  41. case KeyCode::Key_PageDown:
  42. return 34;
  43. case KeyCode::Key_End:
  44. return 35;
  45. case KeyCode::Key_Home:
  46. return 36;
  47. case KeyCode::Key_Left:
  48. return 37;
  49. case KeyCode::Key_Up:
  50. return 38;
  51. case KeyCode::Key_Right:
  52. return 39;
  53. case KeyCode::Key_Down:
  54. return 40;
  55. default:
  56. break;
  57. }
  58. // Return the virtual key code from the operating system.
  59. return platform_key;
  60. }
  61. NonnullRefPtr<KeyboardEvent> KeyboardEvent::create_from_platform_event(FlyString const& event_name, KeyCode platform_key, unsigned modifiers, u32 code_point)
  62. {
  63. // FIXME: Figure out what these should actually contain.
  64. String event_key = key_code_to_string(platform_key);
  65. String event_code = "FIXME";
  66. auto key_code = determine_key_code(platform_key, code_point);
  67. KeyboardEventInit event_init {};
  68. event_init.key = move(event_key);
  69. event_init.code = move(event_code);
  70. event_init.location = 0;
  71. event_init.ctrl_key = modifiers & Mod_Ctrl;
  72. event_init.shift_key = modifiers & Mod_Shift;
  73. event_init.alt_key = modifiers & Mod_Alt;
  74. event_init.meta_key = false;
  75. event_init.repeat = false;
  76. event_init.is_composing = false;
  77. event_init.key_code = key_code;
  78. event_init.char_code = code_point;
  79. event_init.bubbles = true;
  80. event_init.cancelable = true;
  81. event_init.composed = true;
  82. return KeyboardEvent::create(event_name, event_init);
  83. }
  84. bool KeyboardEvent::get_modifier_state(String const& key_arg)
  85. {
  86. if (key_arg == "Alt")
  87. return m_alt_key;
  88. if (key_arg == "Control")
  89. return m_ctrl_key;
  90. if (key_arg == "Shift")
  91. return m_shift_key;
  92. if (key_arg == "Meta")
  93. return m_meta_key;
  94. return false;
  95. }
  96. }