2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-02-27 02:51:13 -08:00
|
|
|
#include "mozilla/dom/KeyboardEvent.h"
|
2013-09-25 04:21:19 -07:00
|
|
|
#include "mozilla/TextEvents.h"
|
2014-02-27 02:51:13 -08:00
|
|
|
#include "prtime.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-02-27 02:51:13 -08:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
2013-10-01 00:22:58 -07:00
|
|
|
|
2014-02-27 02:51:13 -08:00
|
|
|
KeyboardEvent::KeyboardEvent(EventTarget* aOwner,
|
|
|
|
nsPresContext* aPresContext,
|
|
|
|
WidgetKeyboardEvent* aEvent)
|
2014-02-28 06:58:43 -08:00
|
|
|
: UIEvent(aOwner, aPresContext,
|
|
|
|
aEvent ? aEvent : new WidgetKeyboardEvent(false, 0, nullptr))
|
2014-04-13 23:37:47 -07:00
|
|
|
, mInitializedByCtor(false)
|
|
|
|
, mInitialzedWhichValue(0)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(mEvent->eventStructType == NS_KEY_EVENT, "event type mismatch");
|
|
|
|
|
|
|
|
if (aEvent) {
|
2011-10-17 07:59:28 -07:00
|
|
|
mEventIsInternal = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
else {
|
2011-10-17 07:59:28 -07:00
|
|
|
mEventIsInternal = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
mEvent->time = PR_Now();
|
2013-10-28 21:14:42 -07:00
|
|
|
mEvent->AsKeyboardEvent()->mKeyNameIndex = KEY_NAME_INDEX_USE_STRING;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-28 06:58:43 -08:00
|
|
|
NS_IMPL_ADDREF_INHERITED(KeyboardEvent, UIEvent)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(KeyboardEvent, UIEvent)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-02-27 02:51:13 -08:00
|
|
|
NS_INTERFACE_MAP_BEGIN(KeyboardEvent)
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIDOMKeyEvent)
|
2014-02-28 06:58:43 -08:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(UIEvent)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-10-17 23:10:26 -07:00
|
|
|
bool
|
2014-02-27 02:51:13 -08:00
|
|
|
KeyboardEvent::AltKey()
|
2013-10-17 23:10:26 -07:00
|
|
|
{
|
|
|
|
return mEvent->AsKeyboardEvent()->IsAlt();
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
2014-02-27 02:51:13 -08:00
|
|
|
KeyboardEvent::GetAltKey(bool* aIsDown)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aIsDown);
|
2013-04-20 16:48:55 -07:00
|
|
|
*aIsDown = AltKey();
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-10-17 23:10:26 -07:00
|
|
|
bool
|
2014-02-27 02:51:13 -08:00
|
|
|
KeyboardEvent::CtrlKey()
|
2013-10-17 23:10:26 -07:00
|
|
|
{
|
|
|
|
return mEvent->AsKeyboardEvent()->IsControl();
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
2014-02-27 02:51:13 -08:00
|
|
|
KeyboardEvent::GetCtrlKey(bool* aIsDown)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aIsDown);
|
2013-04-20 16:48:55 -07:00
|
|
|
*aIsDown = CtrlKey();
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-10-17 23:10:26 -07:00
|
|
|
bool
|
2014-02-27 02:51:13 -08:00
|
|
|
KeyboardEvent::ShiftKey()
|
2013-10-17 23:10:26 -07:00
|
|
|
{
|
|
|
|
return mEvent->AsKeyboardEvent()->IsShift();
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
2014-02-27 02:51:13 -08:00
|
|
|
KeyboardEvent::GetShiftKey(bool* aIsDown)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aIsDown);
|
2013-04-20 16:48:55 -07:00
|
|
|
*aIsDown = ShiftKey();
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-10-17 23:10:26 -07:00
|
|
|
bool
|
2014-02-27 02:51:13 -08:00
|
|
|
KeyboardEvent::MetaKey()
|
2013-10-17 23:10:26 -07:00
|
|
|
{
|
|
|
|
return mEvent->AsKeyboardEvent()->IsMeta();
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
2014-02-27 02:51:13 -08:00
|
|
|
KeyboardEvent::GetMetaKey(bool* aIsDown)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aIsDown);
|
2013-04-20 16:48:55 -07:00
|
|
|
*aIsDown = MetaKey();
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-11-07 03:17:32 -08:00
|
|
|
bool
|
2014-02-27 02:51:13 -08:00
|
|
|
KeyboardEvent::Repeat()
|
2013-11-07 03:17:32 -08:00
|
|
|
{
|
|
|
|
return mEvent->AsKeyboardEvent()->mIsRepeat;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-02-27 02:51:13 -08:00
|
|
|
KeyboardEvent::GetRepeat(bool* aIsRepeat)
|
2013-11-07 03:17:32 -08:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aIsRepeat);
|
|
|
|
*aIsRepeat = Repeat();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-04-10 00:11:36 -07:00
|
|
|
bool
|
|
|
|
KeyboardEvent::IsComposing()
|
|
|
|
{
|
|
|
|
return mEvent->AsKeyboardEvent()->mIsComposing;
|
|
|
|
}
|
|
|
|
|
2012-04-24 20:00:02 -07:00
|
|
|
NS_IMETHODIMP
|
2014-02-27 02:51:13 -08:00
|
|
|
KeyboardEvent::GetModifierState(const nsAString& aKey,
|
|
|
|
bool* aState)
|
2012-04-24 20:00:02 -07:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aState);
|
|
|
|
|
2013-04-20 16:48:55 -07:00
|
|
|
*aState = GetModifierState(aKey);
|
2012-04-24 20:00:02 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-04-23 20:49:46 -07:00
|
|
|
NS_IMETHODIMP
|
2014-02-27 02:51:13 -08:00
|
|
|
KeyboardEvent::GetKey(nsAString& aKeyName)
|
2013-04-23 20:49:46 -07:00
|
|
|
{
|
2013-10-28 21:14:42 -07:00
|
|
|
mEvent->AsKeyboardEvent()->GetDOMKeyName(aKeyName);
|
2013-04-23 20:49:46 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
2014-02-27 02:51:13 -08:00
|
|
|
KeyboardEvent::GetCharCode(uint32_t* aCharCode)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aCharCode);
|
2013-04-20 16:48:55 -07:00
|
|
|
*aCharCode = CharCode();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-04-20 16:48:55 -07:00
|
|
|
uint32_t
|
2014-02-27 02:51:13 -08:00
|
|
|
KeyboardEvent::CharCode()
|
2013-04-20 16:48:55 -07:00
|
|
|
{
|
2014-04-13 23:37:47 -07:00
|
|
|
// If this event is initialized with ctor, we shouldn't check event type.
|
|
|
|
if (mInitializedByCtor) {
|
|
|
|
return mEvent->AsKeyboardEvent()->charCode;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
switch (mEvent->message) {
|
|
|
|
case NS_KEY_UP:
|
|
|
|
case NS_KEY_DOWN:
|
2013-04-20 16:48:55 -07:00
|
|
|
return 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
case NS_KEY_PRESS:
|
2013-10-17 23:10:24 -07:00
|
|
|
return mEvent->AsKeyboardEvent()->charCode;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2013-04-20 16:48:55 -07:00
|
|
|
return 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-02-27 02:51:13 -08:00
|
|
|
KeyboardEvent::GetKeyCode(uint32_t* aKeyCode)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aKeyCode);
|
2013-04-20 16:48:55 -07:00
|
|
|
*aKeyCode = KeyCode();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-04-20 16:48:55 -07:00
|
|
|
uint32_t
|
2014-02-27 02:51:13 -08:00
|
|
|
KeyboardEvent::KeyCode()
|
2013-04-20 16:48:55 -07:00
|
|
|
{
|
2014-04-13 23:37:47 -07:00
|
|
|
// If this event is initialized with ctor, we shouldn't check event type.
|
|
|
|
if (mInitializedByCtor) {
|
|
|
|
return mEvent->AsKeyboardEvent()->keyCode;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
switch (mEvent->message) {
|
|
|
|
case NS_KEY_UP:
|
|
|
|
case NS_KEY_PRESS:
|
|
|
|
case NS_KEY_DOWN:
|
2013-10-17 23:10:24 -07:00
|
|
|
return mEvent->AsKeyboardEvent()->keyCode;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2013-04-20 16:48:55 -07:00
|
|
|
return 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-04-20 16:48:55 -07:00
|
|
|
uint32_t
|
2014-02-27 02:51:13 -08:00
|
|
|
KeyboardEvent::Which()
|
2013-04-20 16:48:55 -07:00
|
|
|
{
|
2014-04-13 23:37:47 -07:00
|
|
|
// If this event is initialized with ctor, which can have independent value.
|
|
|
|
if (mInitializedByCtor) {
|
|
|
|
return mInitialzedWhichValue;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
switch (mEvent->message) {
|
|
|
|
case NS_KEY_UP:
|
|
|
|
case NS_KEY_DOWN:
|
2013-04-20 16:48:55 -07:00
|
|
|
return KeyCode();
|
2007-03-22 10:30:00 -07:00
|
|
|
case NS_KEY_PRESS:
|
|
|
|
//Special case for 4xp bug 62878. Try to make value of which
|
|
|
|
//more closely mirror the values that 4.x gave for RETURN and BACKSPACE
|
|
|
|
{
|
2013-10-17 23:10:24 -07:00
|
|
|
uint32_t keyCode = mEvent->AsKeyboardEvent()->keyCode;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (keyCode == NS_VK_RETURN || keyCode == NS_VK_BACK) {
|
2013-04-20 16:48:55 -07:00
|
|
|
return keyCode;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2013-04-20 16:48:55 -07:00
|
|
|
return CharCode();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-20 16:48:55 -07:00
|
|
|
return 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-05-03 01:35:01 -07:00
|
|
|
NS_IMETHODIMP
|
2014-02-27 02:51:13 -08:00
|
|
|
KeyboardEvent::GetLocation(uint32_t* aLocation)
|
2012-05-03 01:35:01 -07:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aLocation);
|
|
|
|
|
2013-04-20 16:48:55 -07:00
|
|
|
*aLocation = Location();
|
2012-05-03 01:35:01 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-10-17 23:10:24 -07:00
|
|
|
uint32_t
|
2014-02-27 02:51:13 -08:00
|
|
|
KeyboardEvent::Location()
|
2013-10-17 23:10:24 -07:00
|
|
|
{
|
|
|
|
return mEvent->AsKeyboardEvent()->location;
|
|
|
|
}
|
|
|
|
|
2014-04-13 23:37:47 -07:00
|
|
|
// static
|
|
|
|
already_AddRefed<KeyboardEvent>
|
|
|
|
KeyboardEvent::Constructor(const GlobalObject& aGlobal,
|
|
|
|
const nsAString& aType,
|
|
|
|
const KeyboardEventInit& aParam,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
nsCOMPtr<EventTarget> target = do_QueryInterface(aGlobal.GetAsSupports());
|
|
|
|
nsRefPtr<KeyboardEvent> newEvent =
|
|
|
|
new KeyboardEvent(target, nullptr, nullptr);
|
|
|
|
bool trusted = newEvent->Init(target);
|
|
|
|
aRv = newEvent->InitKeyEvent(aType, aParam.mBubbles, aParam.mCancelable,
|
|
|
|
aParam.mView, aParam.mCtrlKey, aParam.mAltKey,
|
|
|
|
aParam.mShiftKey, aParam.mMetaKey,
|
|
|
|
aParam.mKeyCode, aParam.mCharCode);
|
|
|
|
newEvent->SetTrusted(trusted);
|
|
|
|
newEvent->mDetail = aParam.mDetail;
|
|
|
|
newEvent->mInitializedByCtor = true;
|
|
|
|
newEvent->mInitialzedWhichValue = aParam.mWhich;
|
|
|
|
|
|
|
|
WidgetKeyboardEvent* internalEvent = newEvent->mEvent->AsKeyboardEvent();
|
|
|
|
internalEvent->location = aParam.mLocation;
|
|
|
|
internalEvent->mIsRepeat = aParam.mRepeat;
|
|
|
|
internalEvent->mIsComposing = aParam.mIsComposing;
|
|
|
|
internalEvent->mKeyNameIndex = KEY_NAME_INDEX_USE_STRING;
|
|
|
|
internalEvent->mKeyValue = aParam.mKey;
|
|
|
|
|
|
|
|
return newEvent.forget();
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
2014-02-27 02:51:13 -08:00
|
|
|
KeyboardEvent::InitKeyEvent(const nsAString& aType,
|
|
|
|
bool aCanBubble,
|
|
|
|
bool aCancelable,
|
|
|
|
nsIDOMWindow* aView,
|
|
|
|
bool aCtrlKey,
|
|
|
|
bool aAltKey,
|
|
|
|
bool aShiftKey,
|
|
|
|
bool aMetaKey,
|
|
|
|
uint32_t aKeyCode,
|
|
|
|
uint32_t aCharCode)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-02-28 06:58:43 -08:00
|
|
|
nsresult rv = UIEvent::InitUIEvent(aType, aCanBubble, aCancelable, aView, 0);
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2013-10-17 23:10:24 -07:00
|
|
|
WidgetKeyboardEvent* keyEvent = mEvent->AsKeyboardEvent();
|
2012-04-24 20:00:02 -07:00
|
|
|
keyEvent->InitBasicModifiers(aCtrlKey, aAltKey, aShiftKey, aMetaKey);
|
2007-03-22 10:30:00 -07:00
|
|
|
keyEvent->keyCode = aKeyCode;
|
|
|
|
keyEvent->charCode = aCharCode;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-02-27 02:51:13 -08:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::dom;
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
NS_NewDOMKeyboardEvent(nsIDOMEvent** aInstancePtrResult,
|
|
|
|
EventTarget* aOwner,
|
|
|
|
nsPresContext* aPresContext,
|
|
|
|
WidgetKeyboardEvent* aEvent)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-02-27 02:51:13 -08:00
|
|
|
KeyboardEvent* it = new KeyboardEvent(aOwner, aPresContext, aEvent);
|
2014-03-17 13:00:11 -07:00
|
|
|
NS_ADDREF(it);
|
|
|
|
*aInstancePtrResult = static_cast<Event*>(it);
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|