2 * Copyright (C) 2001 Peter Kelly (pmk@post.com)
3 * Copyright (C) 2001 Tobias Anton (anton@stud.fbi.fh-darmstadt.de)
4 * Copyright (C) 2006 Samuel Weinig (sam.weinig@gmail.com)
5 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public License
18 * along with this library; see the file COPYING.LIB. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
24 #ifndef KeyboardEvent_h
25 #define KeyboardEvent_h
27 #include "EventDispatchMediator.h"
28 #include "UIEventWithKeyState.h"
29 #include <wtf/Vector.h>
33 class EventDispatcher;
35 class PlatformKeyboardEvent;
38 struct KeypressCommand {
40 KeypressCommand(const String& commandName) : commandName(commandName) { ASSERT(isASCIILower(commandName[0U])); }
41 KeypressCommand(const String& commandName, const String& text) : commandName(commandName), text(text) { ASSERT(commandName == "insertText:"); }
43 String commandName; // Actually, a selector name - it may have a trailing colon, and a name that can be different from an editor command name.
48 // Introduced in DOM Level 3
49 class KeyboardEvent : public UIEventWithKeyState {
51 enum KeyLocationCode {
52 DOM_KEY_LOCATION_STANDARD = 0x00,
53 DOM_KEY_LOCATION_LEFT = 0x01,
54 DOM_KEY_LOCATION_RIGHT = 0x02,
55 DOM_KEY_LOCATION_NUMPAD = 0x03
58 static PassRefPtr<KeyboardEvent> create()
60 return adoptRef(new KeyboardEvent);
62 static PassRefPtr<KeyboardEvent> create(const PlatformKeyboardEvent& platformEvent, AbstractView* view)
64 return adoptRef(new KeyboardEvent(platformEvent, view));
66 static PassRefPtr<KeyboardEvent> create(const AtomicString& type, bool canBubble, bool cancelable, AbstractView* view,
67 const String& keyIdentifier, unsigned keyLocation,
68 bool ctrlKey, bool altKey, bool shiftKey, bool metaKey, bool altGraphKey)
70 return adoptRef(new KeyboardEvent(type, canBubble, cancelable, view, keyIdentifier, keyLocation,
71 ctrlKey, altKey, shiftKey, metaKey, altGraphKey));
73 virtual ~KeyboardEvent();
75 void initKeyboardEvent(const AtomicString& type, bool canBubble, bool cancelable, AbstractView*,
76 const String& keyIdentifier, unsigned keyLocation,
77 bool ctrlKey, bool altKey, bool shiftKey, bool metaKey, bool altGraphKey = false);
79 const String& keyIdentifier() const { return m_keyIdentifier; }
80 unsigned keyLocation() const { return m_keyLocation; }
82 bool getModifierState(const String& keyIdentifier) const;
84 bool altGraphKey() const { return m_altGraphKey; }
86 const PlatformKeyboardEvent* keyEvent() const { return m_keyEvent.get(); }
88 int keyCode() const; // key code for keydown and keyup, character for keypress
89 int charCode() const; // character code for keypress, 0 for keydown and keyup
91 virtual bool isKeyboardEvent() const;
92 virtual int which() const;
95 // We only have this need to store keypress command info on the Mac.
96 Vector<KeypressCommand>& keypressCommands() { return m_keypressCommands; }
101 KeyboardEvent(const PlatformKeyboardEvent&, AbstractView*);
102 KeyboardEvent(const AtomicString& type, bool canBubble, bool cancelable, AbstractView*,
103 const String& keyIdentifier, unsigned keyLocation,
104 bool ctrlKey, bool altKey, bool shiftKey, bool metaKey, bool altGraphKey);
106 OwnPtr<PlatformKeyboardEvent> m_keyEvent;
107 String m_keyIdentifier;
108 unsigned m_keyLocation;
109 bool m_altGraphKey : 1;
112 // Commands that were sent by AppKit when interpreting the event. Doesn't include input method commands.
113 Vector<KeypressCommand> m_keypressCommands;
117 KeyboardEvent* findKeyboardEvent(Event*);
119 class KeyboardEventDispatchMediator : public EventDispatchMediator {
121 static PassRefPtr<KeyboardEventDispatchMediator> create(PassRefPtr<KeyboardEvent>);
123 explicit KeyboardEventDispatchMediator(PassRefPtr<KeyboardEvent>);
124 virtual bool dispatchEvent(EventDispatcher*) const;
127 } // namespace WebCore
129 #endif // KeyboardEvent_h