KeyboardEvent.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "mozilla/dom/KeyboardEvent.h"
  6. #include "mozilla/TextEvents.h"
  7. #include "prtime.h"
  8. namespace mozilla {
  9. namespace dom {
  10. KeyboardEvent::KeyboardEvent(EventTarget* aOwner,
  11. nsPresContext* aPresContext,
  12. WidgetKeyboardEvent* aEvent)
  13. : UIEvent(aOwner, aPresContext,
  14. aEvent ? aEvent :
  15. new WidgetKeyboardEvent(false, eVoidEvent, nullptr))
  16. , mInitializedByCtor(false)
  17. , mInitializedWhichValue(0)
  18. {
  19. if (aEvent) {
  20. mEventIsInternal = false;
  21. }
  22. else {
  23. mEventIsInternal = true;
  24. mEvent->mTime = PR_Now();
  25. mEvent->AsKeyboardEvent()->mKeyNameIndex = KEY_NAME_INDEX_USE_STRING;
  26. }
  27. }
  28. NS_IMPL_ADDREF_INHERITED(KeyboardEvent, UIEvent)
  29. NS_IMPL_RELEASE_INHERITED(KeyboardEvent, UIEvent)
  30. NS_INTERFACE_MAP_BEGIN(KeyboardEvent)
  31. NS_INTERFACE_MAP_ENTRY(nsIDOMKeyEvent)
  32. NS_INTERFACE_MAP_END_INHERITING(UIEvent)
  33. bool
  34. KeyboardEvent::AltKey()
  35. {
  36. return mEvent->AsKeyboardEvent()->IsAlt();
  37. }
  38. NS_IMETHODIMP
  39. KeyboardEvent::GetAltKey(bool* aIsDown)
  40. {
  41. NS_ENSURE_ARG_POINTER(aIsDown);
  42. *aIsDown = AltKey();
  43. return NS_OK;
  44. }
  45. bool
  46. KeyboardEvent::CtrlKey()
  47. {
  48. return mEvent->AsKeyboardEvent()->IsControl();
  49. }
  50. NS_IMETHODIMP
  51. KeyboardEvent::GetCtrlKey(bool* aIsDown)
  52. {
  53. NS_ENSURE_ARG_POINTER(aIsDown);
  54. *aIsDown = CtrlKey();
  55. return NS_OK;
  56. }
  57. bool
  58. KeyboardEvent::ShiftKey()
  59. {
  60. return mEvent->AsKeyboardEvent()->IsShift();
  61. }
  62. NS_IMETHODIMP
  63. KeyboardEvent::GetShiftKey(bool* aIsDown)
  64. {
  65. NS_ENSURE_ARG_POINTER(aIsDown);
  66. *aIsDown = ShiftKey();
  67. return NS_OK;
  68. }
  69. bool
  70. KeyboardEvent::MetaKey()
  71. {
  72. return mEvent->AsKeyboardEvent()->IsMeta();
  73. }
  74. NS_IMETHODIMP
  75. KeyboardEvent::GetMetaKey(bool* aIsDown)
  76. {
  77. NS_ENSURE_ARG_POINTER(aIsDown);
  78. *aIsDown = MetaKey();
  79. return NS_OK;
  80. }
  81. bool
  82. KeyboardEvent::Repeat()
  83. {
  84. return mEvent->AsKeyboardEvent()->mIsRepeat;
  85. }
  86. NS_IMETHODIMP
  87. KeyboardEvent::GetRepeat(bool* aIsRepeat)
  88. {
  89. NS_ENSURE_ARG_POINTER(aIsRepeat);
  90. *aIsRepeat = Repeat();
  91. return NS_OK;
  92. }
  93. bool
  94. KeyboardEvent::IsComposing()
  95. {
  96. return mEvent->AsKeyboardEvent()->mIsComposing;
  97. }
  98. NS_IMETHODIMP
  99. KeyboardEvent::GetModifierState(const nsAString& aKey,
  100. bool* aState)
  101. {
  102. NS_ENSURE_ARG_POINTER(aState);
  103. *aState = GetModifierState(aKey);
  104. return NS_OK;
  105. }
  106. NS_IMETHODIMP
  107. KeyboardEvent::GetKey(nsAString& aKeyName)
  108. {
  109. mEvent->AsKeyboardEvent()->GetDOMKeyName(aKeyName);
  110. return NS_OK;
  111. }
  112. void
  113. KeyboardEvent::GetCode(nsAString& aCodeName)
  114. {
  115. mEvent->AsKeyboardEvent()->GetDOMCodeName(aCodeName);
  116. }
  117. void KeyboardEvent::GetInitDict(KeyboardEventInit& aParam)
  118. {
  119. GetKey(aParam.mKey);
  120. GetCode(aParam.mCode);
  121. aParam.mLocation = Location();
  122. aParam.mRepeat = Repeat();
  123. aParam.mIsComposing = IsComposing();
  124. // legacy attributes
  125. aParam.mKeyCode = KeyCode();
  126. aParam.mCharCode = CharCode();
  127. aParam.mWhich = Which();
  128. // modifiers from EventModifierInit
  129. aParam.mCtrlKey = CtrlKey();
  130. aParam.mShiftKey = ShiftKey();
  131. aParam.mAltKey = AltKey();
  132. aParam.mMetaKey = MetaKey();
  133. WidgetKeyboardEvent* internalEvent = mEvent->AsKeyboardEvent();
  134. aParam.mModifierAltGraph = internalEvent->IsAltGraph();
  135. aParam.mModifierCapsLock = internalEvent->IsCapsLocked();
  136. aParam.mModifierFn = internalEvent->IsFn();
  137. aParam.mModifierFnLock = internalEvent->IsFnLocked();
  138. aParam.mModifierNumLock = internalEvent->IsNumLocked();
  139. aParam.mModifierOS = internalEvent->IsOS();
  140. aParam.mModifierScrollLock = internalEvent->IsScrollLocked();
  141. aParam.mModifierSymbol = internalEvent->IsSymbol();
  142. aParam.mModifierSymbolLock = internalEvent->IsSymbolLocked();
  143. // EventInit
  144. aParam.mBubbles = internalEvent->mFlags.mBubbles;
  145. aParam.mCancelable = internalEvent->mFlags.mCancelable;
  146. }
  147. NS_IMETHODIMP
  148. KeyboardEvent::GetCharCode(uint32_t* aCharCode)
  149. {
  150. NS_ENSURE_ARG_POINTER(aCharCode);
  151. *aCharCode = CharCode();
  152. return NS_OK;
  153. }
  154. uint32_t
  155. KeyboardEvent::CharCode()
  156. {
  157. // If this event is initialized with ctor, we shouldn't check event type.
  158. if (mInitializedByCtor) {
  159. return mEvent->AsKeyboardEvent()->mCharCode;
  160. }
  161. switch (mEvent->mMessage) {
  162. case eBeforeKeyDown:
  163. case eKeyDown:
  164. case eKeyDownOnPlugin:
  165. case eAfterKeyDown:
  166. case eBeforeKeyUp:
  167. case eKeyUp:
  168. case eKeyUpOnPlugin:
  169. case eAfterKeyUp:
  170. return 0;
  171. case eKeyPress:
  172. case eAccessKeyNotFound:
  173. return mEvent->AsKeyboardEvent()->mCharCode;
  174. default:
  175. break;
  176. }
  177. return 0;
  178. }
  179. NS_IMETHODIMP
  180. KeyboardEvent::GetKeyCode(uint32_t* aKeyCode)
  181. {
  182. NS_ENSURE_ARG_POINTER(aKeyCode);
  183. *aKeyCode = KeyCode();
  184. return NS_OK;
  185. }
  186. uint32_t
  187. KeyboardEvent::KeyCode()
  188. {
  189. // If this event is initialized with ctor, we shouldn't check event type.
  190. if (mInitializedByCtor) {
  191. return mEvent->AsKeyboardEvent()->mKeyCode;
  192. }
  193. if (mEvent->HasKeyEventMessage()) {
  194. return mEvent->AsKeyboardEvent()->mKeyCode;
  195. }
  196. return 0;
  197. }
  198. uint32_t
  199. KeyboardEvent::Which()
  200. {
  201. // If this event is initialized with ctor, which can have independent value.
  202. if (mInitializedByCtor) {
  203. return mInitializedWhichValue;
  204. }
  205. switch (mEvent->mMessage) {
  206. case eBeforeKeyDown:
  207. case eKeyDown:
  208. case eKeyDownOnPlugin:
  209. case eAfterKeyDown:
  210. case eBeforeKeyUp:
  211. case eKeyUp:
  212. case eKeyUpOnPlugin:
  213. case eAfterKeyUp:
  214. return KeyCode();
  215. case eKeyPress:
  216. //Special case for 4xp bug 62878. Try to make value of which
  217. //more closely mirror the values that 4.x gave for RETURN and BACKSPACE
  218. {
  219. uint32_t keyCode = mEvent->AsKeyboardEvent()->mKeyCode;
  220. if (keyCode == NS_VK_RETURN || keyCode == NS_VK_BACK) {
  221. return keyCode;
  222. }
  223. return CharCode();
  224. }
  225. default:
  226. break;
  227. }
  228. return 0;
  229. }
  230. NS_IMETHODIMP
  231. KeyboardEvent::GetLocation(uint32_t* aLocation)
  232. {
  233. NS_ENSURE_ARG_POINTER(aLocation);
  234. *aLocation = Location();
  235. return NS_OK;
  236. }
  237. uint32_t
  238. KeyboardEvent::Location()
  239. {
  240. return mEvent->AsKeyboardEvent()->mLocation;
  241. }
  242. // static
  243. already_AddRefed<KeyboardEvent>
  244. KeyboardEvent::Constructor(const GlobalObject& aGlobal,
  245. const nsAString& aType,
  246. const KeyboardEventInit& aParam,
  247. ErrorResult& aRv)
  248. {
  249. nsCOMPtr<EventTarget> target = do_QueryInterface(aGlobal.GetAsSupports());
  250. RefPtr<KeyboardEvent> newEvent =
  251. new KeyboardEvent(target, nullptr, nullptr);
  252. newEvent->InitWithKeyboardEventInit(target, aType, aParam, aRv);
  253. return newEvent.forget();
  254. }
  255. void
  256. KeyboardEvent::InitWithKeyboardEventInit(EventTarget* aOwner,
  257. const nsAString& aType,
  258. const KeyboardEventInit& aParam,
  259. ErrorResult& aRv)
  260. {
  261. bool trusted = Init(aOwner);
  262. InitKeyEvent(aType, aParam.mBubbles, aParam.mCancelable,
  263. aParam.mView, false, false, false, false,
  264. aParam.mKeyCode, aParam.mCharCode);
  265. InitModifiers(aParam);
  266. SetTrusted(trusted);
  267. mDetail = aParam.mDetail;
  268. mInitializedByCtor = true;
  269. mInitializedWhichValue = aParam.mWhich;
  270. WidgetKeyboardEvent* internalEvent = mEvent->AsKeyboardEvent();
  271. internalEvent->mLocation = aParam.mLocation;
  272. internalEvent->mIsRepeat = aParam.mRepeat;
  273. internalEvent->mIsComposing = aParam.mIsComposing;
  274. internalEvent->mKeyNameIndex =
  275. WidgetKeyboardEvent::GetKeyNameIndex(aParam.mKey);
  276. if (internalEvent->mKeyNameIndex == KEY_NAME_INDEX_USE_STRING) {
  277. internalEvent->mKeyValue = aParam.mKey;
  278. }
  279. internalEvent->mCodeNameIndex =
  280. WidgetKeyboardEvent::GetCodeNameIndex(aParam.mCode);
  281. if (internalEvent->mCodeNameIndex == CODE_NAME_INDEX_USE_STRING) {
  282. internalEvent->mCodeValue = aParam.mCode;
  283. }
  284. }
  285. NS_IMETHODIMP
  286. KeyboardEvent::InitKeyEvent(const nsAString& aType,
  287. bool aCanBubble,
  288. bool aCancelable,
  289. mozIDOMWindow* aView,
  290. bool aCtrlKey,
  291. bool aAltKey,
  292. bool aShiftKey,
  293. bool aMetaKey,
  294. uint32_t aKeyCode,
  295. uint32_t aCharCode)
  296. {
  297. NS_ENSURE_TRUE(!mEvent->mFlags.mIsBeingDispatched, NS_OK);
  298. UIEvent::InitUIEvent(aType, aCanBubble, aCancelable, aView, 0);
  299. WidgetKeyboardEvent* keyEvent = mEvent->AsKeyboardEvent();
  300. keyEvent->InitBasicModifiers(aCtrlKey, aAltKey, aShiftKey, aMetaKey);
  301. keyEvent->mKeyCode = aKeyCode;
  302. keyEvent->mCharCode = aCharCode;
  303. return NS_OK;
  304. }
  305. } // namespace dom
  306. } // namespace mozilla
  307. using namespace mozilla;
  308. using namespace mozilla::dom;
  309. already_AddRefed<KeyboardEvent>
  310. NS_NewDOMKeyboardEvent(EventTarget* aOwner,
  311. nsPresContext* aPresContext,
  312. WidgetKeyboardEvent* aEvent)
  313. {
  314. RefPtr<KeyboardEvent> it = new KeyboardEvent(aOwner, aPresContext, aEvent);
  315. return it.forget();
  316. }