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
|
|
|
|
|
|
|
#include "nsDOMKeyboardEvent.h"
|
2013-07-03 08:56:26 -07:00
|
|
|
#include "prtime.h"
|
2013-09-25 04:21:19 -07:00
|
|
|
#include "mozilla/TextEvents.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-10-01 00:22:58 -07:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2013-03-09 03:34:29 -08:00
|
|
|
nsDOMKeyboardEvent::nsDOMKeyboardEvent(mozilla::dom::EventTarget* aOwner,
|
|
|
|
nsPresContext* aPresContext,
|
2013-10-01 00:22:58 -07:00
|
|
|
WidgetKeyboardEvent* aEvent)
|
2013-03-09 03:34:29 -08:00
|
|
|
: nsDOMUIEvent(aOwner, aPresContext, aEvent ? aEvent :
|
2013-10-01 00:22:58 -07:00
|
|
|
new WidgetKeyboardEvent(false, 0, nullptr))
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(nsDOMKeyboardEvent, nsDOMUIEvent)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(nsDOMKeyboardEvent, nsDOMUIEvent)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN(nsDOMKeyboardEvent)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIDOMKeyEvent)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(nsDOMUIEvent)
|
|
|
|
|
2013-10-17 23:10:26 -07:00
|
|
|
bool
|
|
|
|
nsDOMKeyboardEvent::AltKey()
|
|
|
|
{
|
|
|
|
return mEvent->AsKeyboardEvent()->IsAlt();
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
nsDOMKeyboardEvent::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
|
|
|
|
nsDOMKeyboardEvent::CtrlKey()
|
|
|
|
{
|
|
|
|
return mEvent->AsKeyboardEvent()->IsControl();
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
nsDOMKeyboardEvent::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
|
|
|
|
nsDOMKeyboardEvent::ShiftKey()
|
|
|
|
{
|
|
|
|
return mEvent->AsKeyboardEvent()->IsShift();
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
nsDOMKeyboardEvent::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
|
|
|
|
nsDOMKeyboardEvent::MetaKey()
|
|
|
|
{
|
|
|
|
return mEvent->AsKeyboardEvent()->IsMeta();
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
nsDOMKeyboardEvent::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;
|
|
|
|
}
|
|
|
|
|
2012-04-24 20:00:02 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDOMKeyboardEvent::GetModifierState(const nsAString& aKey,
|
|
|
|
bool* aState)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
nsDOMKeyboardEvent::GetKey(nsAString& aKeyName)
|
|
|
|
{
|
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
|
2012-08-22 08:56:38 -07:00
|
|
|
nsDOMKeyboardEvent::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
|
|
|
|
nsDOMKeyboardEvent::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
|
2012-08-22 08:56:38 -07:00
|
|
|
nsDOMKeyboardEvent::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
|
|
|
|
nsDOMKeyboardEvent::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
|
|
|
|
nsDOMKeyboardEvent::Which()
|
|
|
|
{
|
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
|
2012-08-22 08:56:38 -07:00
|
|
|
nsDOMKeyboardEvent::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
|
|
|
|
nsDOMKeyboardEvent::Location()
|
|
|
|
{
|
|
|
|
return mEvent->AsKeyboardEvent()->location;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
nsDOMKeyboardEvent::InitKeyEvent(const nsAString& aType, bool aCanBubble, bool aCancelable,
|
|
|
|
nsIDOMWindow* aView, bool aCtrlKey, bool aAltKey,
|
|
|
|
bool aShiftKey, bool aMetaKey,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t aKeyCode, uint32_t aCharCode)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsresult rv = nsDOMUIEvent::InitUIEvent(aType, aCanBubble, aCancelable, aView, 0);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult NS_NewDOMKeyboardEvent(nsIDOMEvent** aInstancePtrResult,
|
2013-03-09 03:34:29 -08:00
|
|
|
mozilla::dom::EventTarget* aOwner,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsPresContext* aPresContext,
|
2013-10-01 00:22:58 -07:00
|
|
|
WidgetKeyboardEvent* aEvent)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-03-09 03:34:29 -08:00
|
|
|
nsDOMKeyboardEvent* it = new nsDOMKeyboardEvent(aOwner, aPresContext, aEvent);
|
2007-03-22 10:30:00 -07:00
|
|
|
return CallQueryInterface(it, aInstancePtrResult);
|
|
|
|
}
|