1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_dom_KeyboardEvent_h_
8 #define mozilla_dom_KeyboardEvent_h_
10 #include "mozilla/dom/UIEvent.h"
11 #include "mozilla/dom/KeyboardEventBinding.h"
12 #include "mozilla/EventForwards.h"
14 namespace mozilla::dom
{
16 class KeyboardEvent
: public UIEvent
{
18 KeyboardEvent(EventTarget
* aOwner
, nsPresContext
* aPresContext
,
19 WidgetKeyboardEvent
* aEvent
);
21 NS_INLINE_DECL_REFCOUNTING_INHERITED(KeyboardEvent
, UIEvent
)
23 virtual KeyboardEvent
* AsKeyboardEvent() override
{ return this; }
25 static already_AddRefed
<KeyboardEvent
> ConstructorJS(
26 const GlobalObject
& aGlobal
, const nsAString
& aType
,
27 const KeyboardEventInit
& aParam
);
29 virtual JSObject
* WrapObjectInternal(
30 JSContext
* aCx
, JS::Handle
<JSObject
*> aGivenProto
) override
{
31 return KeyboardEvent_Binding::Wrap(aCx
, this, aGivenProto
);
34 bool AltKey(CallerType aCallerType
= CallerType::System
);
35 bool CtrlKey(CallerType aCallerType
= CallerType::System
);
36 bool ShiftKey(CallerType aCallerType
= CallerType::System
);
39 // Returns true if the modifier state of the event matches the modifier state
41 bool IsMenuAccessKeyPressed() const;
42 Modifiers
GetModifiersForMenuAccessKey() const;
44 bool GetModifierState(const nsAString
& aKey
,
45 CallerType aCallerType
= CallerType::System
) {
46 bool modifierState
= GetModifierStateInternal(aKey
);
48 if (!ShouldResistFingerprinting(aCallerType
)) {
52 Modifiers modifier
= WidgetInputEvent::GetModifier(aKey
);
53 return GetSpoofedModifierStates(modifier
, modifierState
);
58 void GetKey(nsAString
& aKey
) const;
59 uint32_t CharCode(CallerType aCallerType
= CallerType::System
);
60 uint32_t KeyCode(CallerType aCallerType
= CallerType::System
);
61 virtual uint32_t Which(CallerType aCallerType
= CallerType::System
) override
;
64 void GetCode(nsAString
& aCode
, CallerType aCallerType
= CallerType::System
);
65 void GetInitDict(KeyboardEventInit
& aParam
);
67 void InitKeyEventJS(const nsAString
& aType
, bool aCanBubble
, bool aCancelable
,
68 nsGlobalWindowInner
* aView
, bool aCtrlKey
, bool aAltKey
,
69 bool aShiftKey
, bool aMetaKey
, uint32_t aKeyCode
,
71 static bool IsInitKeyEventAvailable(JSContext
*, JSObject
*);
73 void InitKeyboardEventJS(const nsAString
& aType
, bool aCanBubble
,
74 bool aCancelable
, nsGlobalWindowInner
* aView
,
75 const nsAString
& aKey
, uint32_t aLocation
,
76 bool aCtrlKey
, bool aAltKey
, bool aShiftKey
,
80 ~KeyboardEvent() = default;
82 void InitWithKeyboardEventInit(EventTarget
* aOwner
, const nsAString
& aType
,
83 const KeyboardEventInit
& aParam
);
86 // True, if the instance is initialized by JS.
87 bool mInitializedByJS
;
88 // True, if the instance is initialized by Ctor.
89 bool mInitializedByCtor
;
91 // If the instance is created with Constructor(), which may have independent
92 // value. mInitializedWhichValue stores it. I.e., this is invalid when
93 // mInitializedByCtor is false.
94 uint32_t mInitializedWhichValue
;
96 // This method returns the boolean to indicate whether spoofing keyboard
97 // event for fingerprinting resistance. It will return true when pref
98 // 'privacy.resistFingerprinting' is true and the event target is content.
99 // Otherwise, it will return false.
100 bool ShouldResistFingerprinting(CallerType aCallerType
);
102 // This method returns the spoofed modifier state of the given modifier key
103 // for fingerprinting resistance.
104 bool GetSpoofedModifierStates(const Modifiers aModifierKey
,
105 const bool aRawModifierState
);
108 * ComputeTraditionalKeyCode() computes traditional keyCode value. I.e.,
109 * returns 0 if this event should return non-zero from CharCode().
110 * In spite of the name containing "traditional", this computes spoof
111 * keyCode value if user wants it.
113 * @param aKeyboardEvent Should be |*mEvent->AsKeyboardEvent()|.
114 * @param aCallerType Set caller type of KeyCode() or CharCode().
115 * @return If traditional charCode value is 0, returns
116 * the raw keyCode value or spoof keyCode value.
119 uint32_t ComputeTraditionalKeyCode(WidgetKeyboardEvent
& aKeyboardEvent
,
120 CallerType aCallerType
);
122 * ShouldUseSameValueForCharCodeAndKeyCode() returns true if KeyCode() and
123 * CharCode() should return same value.
125 bool ShouldUseSameValueForCharCodeAndKeyCode(
126 const WidgetKeyboardEvent
& aKeyboardEvent
, CallerType aCallerType
) const;
129 } // namespace mozilla::dom
131 already_AddRefed
<mozilla::dom::KeyboardEvent
> NS_NewDOMKeyboardEvent(
132 mozilla::dom::EventTarget
* aOwner
, nsPresContext
* aPresContext
,
133 mozilla::WidgetKeyboardEvent
* aEvent
);
135 #endif // mozilla_dom_KeyboardEvent_h_