• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2004, 2005, 2006 Apple Computer, Inc.  All rights reserved.
3  * Copyright (C) 2008 Collabora, Ltd.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #ifndef PlatformKeyboardEvent_h
28 #define PlatformKeyboardEvent_h
29 
30 #include "PlatformString.h"
31 #include <wtf/Platform.h>
32 
33 #if PLATFORM(MAC)
34 #include <wtf/RetainPtr.h>
35 #ifdef __OBJC__
36 @class NSEvent;
37 #else
38 class NSEvent;
39 #endif
40 #endif
41 
42 #if PLATFORM(WIN)
43 typedef struct HWND__ *HWND;
44 typedef unsigned WPARAM;
45 typedef long LPARAM;
46 #endif
47 
48 #if PLATFORM(GTK)
49 typedef struct _GdkEventKey GdkEventKey;
50 #endif
51 
52 #if PLATFORM(QT)
53 QT_BEGIN_NAMESPACE
54 class QKeyEvent;
55 QT_END_NAMESPACE
56 #endif
57 
58 #if PLATFORM(WX)
59 class wxKeyEvent;
60 #endif
61 
62 namespace WebCore {
63 
64     class PlatformKeyboardEvent {
65     public:
66         enum Type {
67             // KeyDown is sent by platforms such as Mac OS X, gtk and Qt, and has information about both physical pressed key, and its translation.
68             // For DOM processing, it needs to be disambiguated as RawKeyDown or Char event.
69             KeyDown,
70 
71             // KeyUp is sent by all platforms.
72             KeyUp,
73 
74             // These events are sent by platforms such as Windows and wxWidgets. RawKeyDown only has information about a physical key, and Char
75             // only has information about a character it was translated into.
76             RawKeyDown,
77             Char
78         };
79 
80         enum ModifierKey {
81             AltKey = 1 << 0,
82             CtrlKey = 1 << 1,
83             MetaKey = 1 << 2,
84             ShiftKey = 1 << 3,
85         };
86 
type()87         Type type() const { return m_type; }
88         void disambiguateKeyDownEvent(Type, bool backwardCompatibilityMode = false); // Only used on platforms that need it, i.e. those that generate KeyDown events.
89 
90         // Text as as generated by processing a virtual key code with a keyboard layout
91         // (in most cases, just a character code, but the layout can emit several
92         // characters in a single keypress event on some platforms).
93         // This may bear no resemblance to the ultimately inserted text if an input method
94         // processes the input.
95         // Will be null for KeyUp and RawKeyDown events.
text()96         String text() const { return m_text; }
97 
98         // Text that would have been generated by the keyboard if no modifiers were pressed
99         // (except for Shift); useful for shortcut (accelerator) key handling.
100         // Otherwise, same as text().
unmodifiedText()101         String unmodifiedText() const { return m_unmodifiedText; }
102 
103         // Most compatible Windows virtual key code associated with the event.
104         // Zero for Char events.
windowsVirtualKeyCode()105         int windowsVirtualKeyCode() const { return m_windowsVirtualKeyCode; }
setWindowsVirtualKeyCode(int code)106         void setWindowsVirtualKeyCode(int code) { m_windowsVirtualKeyCode = code; }
107 
nativeVirtualKeyCode()108         int nativeVirtualKeyCode() const { return m_nativeVirtualKeyCode; }
setNativeVirtualKeyCode(int code)109         void setNativeVirtualKeyCode(int code) { m_nativeVirtualKeyCode = code; }
110 
keyIdentifier()111         String keyIdentifier() const { return m_keyIdentifier; }
isAutoRepeat()112         bool isAutoRepeat() const { return m_autoRepeat; }
setIsAutoRepeat(bool in)113         void setIsAutoRepeat(bool in) { m_autoRepeat = in; }
isKeypad()114         bool isKeypad() const { return m_isKeypad; }
shiftKey()115         bool shiftKey() const { return m_shiftKey; }
ctrlKey()116         bool ctrlKey() const { return m_ctrlKey; }
altKey()117         bool altKey() const { return m_altKey; }
metaKey()118         bool metaKey() const { return m_metaKey; }
modifiers()119         unsigned modifiers() const {
120             return (altKey() ? AltKey : 0)
121                 | (ctrlKey() ? CtrlKey : 0)
122                 | (metaKey() ? MetaKey : 0)
123                 | (shiftKey() ? ShiftKey : 0);
124         }
125 
126         static bool currentCapsLockState();
127 
128 #if PLATFORM(MAC)
129         PlatformKeyboardEvent(NSEvent*);
macEvent()130         NSEvent* macEvent() const { return m_macEvent.get(); }
131 #endif
132 
133 #if PLATFORM(WIN)
134         PlatformKeyboardEvent(HWND, WPARAM, LPARAM, Type, bool);
135 #endif
136 
137 #if PLATFORM(GTK)
138         PlatformKeyboardEvent(GdkEventKey*);
139         GdkEventKey* gdkEventKey() const;
140 #endif
141 
142 #if PLATFORM(ANDROID)
143         PlatformKeyboardEvent(int keyCode, UChar32 unichar, int repeatCount,
144                     bool down, bool cap, bool alt, bool sym);
unichar()145         UChar32 unichar() const { return m_unichar; }
repeatCount()146         int repeatCount() const { return m_repeatCount; }
147 #endif
148 
149 #if PLATFORM(QT)
150         PlatformKeyboardEvent(QKeyEvent*);
qtEvent()151         QKeyEvent* qtEvent() const { return m_qtEvent; }
152 #endif
153 
154 #if PLATFORM(WX)
155         PlatformKeyboardEvent(wxKeyEvent&);
156 #endif
157 
158 #if PLATFORM(WIN) || PLATFORM(CHROMIUM)
isSystemKey()159         bool isSystemKey() const { return m_isSystemKey; }
160 #endif
161 
162     protected:
163         Type m_type;
164         String m_text;
165         String m_unmodifiedText;
166         String m_keyIdentifier;
167         bool m_autoRepeat;
168         int m_windowsVirtualKeyCode;
169         int m_nativeVirtualKeyCode;
170         bool m_isKeypad;
171         bool m_shiftKey;
172         bool m_ctrlKey;
173         bool m_altKey;
174         bool m_metaKey;
175 
176 #if PLATFORM(ANDROID)
177         /*  the actual repeatCount (as opposed to just a bool m_autoRepeat)
178             0 for initial down and up
179          */
180         int     m_repeatCount;
181         /*  The originall unichar value. Sometimes the m_text/m_unmodifiedText
182             fields are stripped (e.g. for RawKeyDown), so we record it also here
183             in case someone (e.g. plugins) want to sniff it w/o waiting for a
184             Char event type.
185          */
186         UChar32 m_unichar;
187 #endif
188 
189 #if PLATFORM(MAC)
190         RetainPtr<NSEvent> m_macEvent;
191 #endif
192 #if PLATFORM(WIN) || PLATFORM(CHROMIUM)
193         bool m_isSystemKey;
194 #endif
195 #if PLATFORM(GTK)
196         GdkEventKey* m_gdkEventKey;
197 #endif
198 #if PLATFORM(QT)
199         QKeyEvent* m_qtEvent;
200 #endif
201     };
202 
203 } // namespace WebCore
204 
205 #endif // PlatformKeyboardEvent_h
206