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_KeyEventHandler_h_
8 #define mozilla_KeyEventHandler_h_
10 #include "mozilla/EventForwards.h"
11 #include "mozilla/MemoryReporting.h"
15 #include "nsIController.h"
16 #include "nsIWeakReference.h"
17 #include "nsCycleCollectionParticipant.h"
18 #include "js/TypeDecls.h"
19 #include "mozilla/ShortcutKeys.h"
24 class KeyboardShortcut
;
27 struct IgnoreModifierState
;
38 // Values of the reserved attribute. When unset, the default value depends on
39 // the permissions.default.shortcuts preference.
40 enum ReservedKey
: uint8_t {
41 ReservedKey_False
= 0,
43 ReservedKey_Unset
= 2,
46 class KeyEventHandler final
{
48 // This constructor is used only by XUL key handlers (e.g., <key>)
49 explicit KeyEventHandler(dom::Element
* aHandlerElement
,
50 ReservedKey aReserved
);
52 // This constructor is used for keyboard handlers for browser, editor, input
53 // and textarea elements.
54 explicit KeyEventHandler(ShortcutKeyData
* aKeyData
);
59 * Try and convert this XBL handler into an APZ KeyboardShortcut for handling
60 * key events on the compositor thread. This only works for XBL handlers that
61 * represent scroll commands.
63 * @param aOut the converted KeyboardShortcut, must be non null
64 * @return whether the handler was converted into a KeyboardShortcut
66 bool TryConvertToKeyboardShortcut(layers::KeyboardShortcut
* aOut
) const;
68 bool EventTypeEquals(nsAtom
* aEventType
) const {
69 return mEventName
== aEventType
;
72 // if aCharCode is not zero, it is used instead of the charCode of
74 bool KeyEventMatched(dom::KeyboardEvent
* aDomKeyboardEvent
,
76 const IgnoreModifierState
& aIgnoreModifierState
);
78 already_AddRefed
<dom::Element
> GetHandlerElement();
80 ReservedKey
GetIsReserved() { return mReserved
; }
82 KeyEventHandler
* GetNextHandler() { return mNextHandler
; }
83 void SetNextHandler(KeyEventHandler
* aHandler
) { mNextHandler
= aHandler
; }
86 nsresult
ExecuteHandler(dom::EventTarget
* aTarget
, dom::Event
* aEvent
);
88 size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf
) const;
91 static uint32_t gRefCnt
;
97 // Get the primary accelerator key.
102 already_AddRefed
<nsIController
> GetController(dom::EventTarget
* aTarget
);
104 inline int32_t GetMatchingKeyCode(const nsAString
& aKeyName
);
105 void ConstructPrototype(dom::Element
* aKeyElement
,
106 const char16_t
* aEvent
= nullptr,
107 const char16_t
* aCommand
= nullptr,
108 const char16_t
* aKeyCode
= nullptr,
109 const char16_t
* aCharCode
= nullptr,
110 const char16_t
* aModifiers
= nullptr);
111 void BuildModifiers(nsAString
& aModifiers
);
113 void ReportKeyConflict(const char16_t
* aKey
, const char16_t
* aModifiers
,
114 dom::Element
* aKeyElement
, const char* aMessageName
);
115 void GetEventType(nsAString
& aEvent
);
116 bool ModifiersMatchMask(dom::UIEvent
* aEvent
,
117 const IgnoreModifierState
& aIgnoreModifierState
);
119 nsresult
DispatchXBLCommand(dom::EventTarget
* aTarget
, dom::Event
* aEvent
);
121 nsresult
DispatchXULKeyCommand(dom::Event
* aEvent
);
123 Modifiers
GetModifiers() const;
124 Modifiers
GetModifiersMask() const;
126 static int32_t KeyToMask(int32_t key
);
127 static int32_t AccelKeyMask();
129 static int32_t kMenuAccessKey
;
130 static void InitAccessKeys();
132 static const int32_t cShift
;
133 static const int32_t cAlt
;
134 static const int32_t cControl
;
135 static const int32_t cMeta
;
136 static const int32_t cOS
;
138 static const int32_t cShiftMask
;
139 static const int32_t cAltMask
;
140 static const int32_t cControlMask
;
141 static const int32_t cMetaMask
;
142 static const int32_t cOSMask
;
144 static const int32_t cAllModifiers
;
149 mHandlerElement
; // For XUL <key> element handlers. [STRONG]
150 char16_t
* mCommand
; // For built-in shortcuts the command to execute.
153 // The following four values make up 32 bits.
154 bool mIsXULKey
; // This handler is either for a XUL <key> element or it is
155 // a command dispatcher.
156 uint8_t mMisc
; // Miscellaneous extra information. For key events,
157 // stores whether or not we're a key code or char code.
158 // For mouse events, stores the clickCount.
160 ReservedKey mReserved
; // <key> is reserved for chrome. Not used by handlers.
162 int32_t mKeyMask
; // Which modifier keys this event handler expects to have
163 // down in order to be matched.
165 // The primary filter information for mouse/key events.
166 int32_t mDetail
; // For key events, contains a charcode or keycode. For
167 // mouse events, stores the button info.
169 // Prototype handlers are chained. We own the next handler in the chain.
170 KeyEventHandler
* mNextHandler
;
171 RefPtr
<nsAtom
> mEventName
; // The type of the event, e.g., "keypress"
174 } // namespace mozilla