Home | History | Annotate | Download | only in platform
      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 
     32 #if PLATFORM(MAC)
     33 #include <wtf/RetainPtr.h>
     34 #ifdef __OBJC__
     35 @class NSEvent;
     36 #else
     37 class NSEvent;
     38 #endif
     39 #endif
     40 
     41 #if PLATFORM(WIN)
     42 typedef struct HWND__ *HWND;
     43 typedef unsigned WPARAM;
     44 typedef long LPARAM;
     45 #endif
     46 
     47 #if PLATFORM(GTK)
     48 typedef struct _GdkEventKey GdkEventKey;
     49 #endif
     50 
     51 #if PLATFORM(QT)
     52 QT_BEGIN_NAMESPACE
     53 class QKeyEvent;
     54 QT_END_NAMESPACE
     55 #endif
     56 
     57 #if PLATFORM(WX)
     58 class wxKeyEvent;
     59 #endif
     60 
     61 #if PLATFORM(HAIKU)
     62 class BMessage;
     63 #endif
     64 
     65 #if PLATFORM(EFL)
     66 typedef struct _Evas_Event_Key_Down Evas_Event_Key_Down;
     67 typedef struct _Evas_Event_Key_Up Evas_Event_Key_Up;
     68 #endif
     69 
     70 #if PLATFORM(BREWMP)
     71 typedef unsigned short    uint16;
     72 typedef unsigned long int uint32;
     73 #define AEEEvent uint16
     74 #endif
     75 
     76 namespace WebCore {
     77 
     78     class PlatformKeyboardEvent {
     79         WTF_MAKE_FAST_ALLOCATED;
     80     public:
     81         enum Type {
     82             // KeyDown is sent by platforms such as Mac OS X, gtk and Qt, and has information about both physical pressed key, and its translation.
     83             // For DOM processing, it needs to be disambiguated as RawKeyDown or Char event.
     84             KeyDown,
     85 
     86             // KeyUp is sent by all platforms.
     87             KeyUp,
     88 
     89             // These events are sent by platforms such as Windows and wxWidgets. RawKeyDown only has information about a physical key, and Char
     90             // only has information about a character it was translated into.
     91             RawKeyDown,
     92             Char
     93         };
     94 
     95         enum ModifierKey {
     96             AltKey = 1 << 0,
     97             CtrlKey = 1 << 1,
     98             MetaKey = 1 << 2,
     99             ShiftKey = 1 << 3
    100         };
    101 
    102         PlatformKeyboardEvent()
    103             : m_type(KeyDown)
    104             , m_autoRepeat(false)
    105             , m_windowsVirtualKeyCode(0)
    106             , m_nativeVirtualKeyCode(0)
    107             , m_isKeypad(false)
    108             , m_shiftKey(false)
    109             , m_ctrlKey(false)
    110             , m_altKey(false)
    111             , m_metaKey(false)
    112 #if PLATFORM(WIN) || PLATFORM(CHROMIUM)
    113             , m_isSystemKey(false)
    114 #endif
    115 #if PLATFORM(GTK)
    116             , m_gdkEventKey(0)
    117 #endif
    118 #if PLATFORM(QT)
    119             , m_qtEvent(0)
    120 #endif
    121         {
    122         }
    123 
    124         Type type() const { return m_type; }
    125         void disambiguateKeyDownEvent(Type, bool backwardCompatibilityMode = false); // Only used on platforms that need it, i.e. those that generate KeyDown events.
    126 
    127         // Text as as generated by processing a virtual key code with a keyboard layout
    128         // (in most cases, just a character code, but the layout can emit several
    129         // characters in a single keypress event on some platforms).
    130         // This may bear no resemblance to the ultimately inserted text if an input method
    131         // processes the input.
    132         // Will be null for KeyUp and RawKeyDown events.
    133         String text() const { return m_text; }
    134 
    135         // Text that would have been generated by the keyboard if no modifiers were pressed
    136         // (except for Shift); useful for shortcut (accelerator) key handling.
    137         // Otherwise, same as text().
    138         String unmodifiedText() const { return m_unmodifiedText; }
    139 
    140         // Most compatible Windows virtual key code associated with the event.
    141         // Zero for Char events.
    142         int windowsVirtualKeyCode() const { return m_windowsVirtualKeyCode; }
    143         void setWindowsVirtualKeyCode(int code) { m_windowsVirtualKeyCode = code; }
    144 
    145         int nativeVirtualKeyCode() const { return m_nativeVirtualKeyCode; }
    146         void setNativeVirtualKeyCode(int code) { m_nativeVirtualKeyCode = code; }
    147 
    148         String keyIdentifier() const { return m_keyIdentifier; }
    149         bool isAutoRepeat() const { return m_autoRepeat; }
    150         void setIsAutoRepeat(bool in) { m_autoRepeat = in; }
    151         bool isKeypad() const { return m_isKeypad; }
    152         bool shiftKey() const { return m_shiftKey; }
    153         bool ctrlKey() const { return m_ctrlKey; }
    154         bool altKey() const { return m_altKey; }
    155         bool metaKey() const { return m_metaKey; }
    156         unsigned modifiers() const {
    157             return (altKey() ? AltKey : 0)
    158                 | (ctrlKey() ? CtrlKey : 0)
    159                 | (metaKey() ? MetaKey : 0)
    160                 | (shiftKey() ? ShiftKey : 0);
    161         }
    162 
    163         static bool currentCapsLockState();
    164         static void getCurrentModifierState(bool& shiftKey, bool& ctrlKey, bool& altKey, bool& metaKey);
    165 
    166 #if PLATFORM(MAC)
    167         PlatformKeyboardEvent(NSEvent*);
    168         NSEvent* macEvent() const { return m_macEvent.get(); }
    169 #endif
    170 
    171 #if PLATFORM(WIN)
    172         PlatformKeyboardEvent(HWND, WPARAM, LPARAM, Type, bool);
    173 #endif
    174 
    175 #if PLATFORM(GTK)
    176         PlatformKeyboardEvent(GdkEventKey*);
    177         GdkEventKey* gdkEventKey() const;
    178 
    179         // Used by WebKit2
    180         static String keyIdentifierForGdkKeyCode(unsigned);
    181         static int windowsKeyCodeForGdkKeyCode(unsigned);
    182         static String singleCharacterString(unsigned);
    183 #endif
    184 
    185 #if PLATFORM(ANDROID)
    186         PlatformKeyboardEvent(int keyCode, UChar32 unichar, int repeatCount,
    187                     bool down, bool cap, bool alt, bool sym);
    188         UChar32 unichar() const { return m_unichar; }
    189         int repeatCount() const { return m_repeatCount; }
    190 #endif
    191 
    192 #if PLATFORM(QT)
    193         PlatformKeyboardEvent(QKeyEvent*);
    194         QKeyEvent* qtEvent() const { return m_qtEvent; }
    195         uint32_t nativeModifiers() const;
    196         uint32_t nativeScanCode() const;
    197 #endif
    198 
    199 #if PLATFORM(WX)
    200         PlatformKeyboardEvent(wxKeyEvent&);
    201 #endif
    202 
    203 #if PLATFORM(HAIKU)
    204         PlatformKeyboardEvent(BMessage*);
    205 #endif
    206 
    207 #if PLATFORM(BREWMP)
    208         PlatformKeyboardEvent(AEEEvent, uint16, uint32, Type);
    209 #endif
    210 
    211 #if PLATFORM(WIN) || PLATFORM(CHROMIUM)
    212         bool isSystemKey() const { return m_isSystemKey; }
    213 #endif
    214 
    215 #if PLATFORM(EFL)
    216         PlatformKeyboardEvent(const Evas_Event_Key_Down*);
    217         PlatformKeyboardEvent(const Evas_Event_Key_Up*);
    218 #endif
    219 
    220     protected:
    221         Type m_type;
    222         String m_text;
    223         String m_unmodifiedText;
    224         String m_keyIdentifier;
    225         bool m_autoRepeat;
    226         int m_windowsVirtualKeyCode;
    227         int m_nativeVirtualKeyCode;
    228         bool m_isKeypad;
    229         bool m_shiftKey;
    230         bool m_ctrlKey;
    231         bool m_altKey;
    232         bool m_metaKey;
    233 
    234 #if PLATFORM(ANDROID)
    235         /*  the actual repeatCount (as opposed to just a bool m_autoRepeat)
    236             0 for initial down and up
    237          */
    238         int     m_repeatCount;
    239         /*  The originall unichar value. Sometimes the m_text/m_unmodifiedText
    240             fields are stripped (e.g. for RawKeyDown), so we record it also here
    241             in case someone (e.g. plugins) want to sniff it w/o waiting for a
    242             Char event type.
    243          */
    244         UChar32 m_unichar;
    245 #endif
    246 
    247 #if PLATFORM(MAC)
    248         RetainPtr<NSEvent> m_macEvent;
    249 #endif
    250 #if PLATFORM(WIN) || PLATFORM(CHROMIUM)
    251         bool m_isSystemKey;
    252 #endif
    253 #if PLATFORM(GTK)
    254         GdkEventKey* m_gdkEventKey;
    255 #endif
    256 #if PLATFORM(QT)
    257         QKeyEvent* m_qtEvent;
    258 #endif
    259     };
    260 
    261 #if PLATFORM(QT)
    262 // Used by WebKit2.
    263 String keyIdentifierForQtKeyCode(int keyCode);
    264 int windowsKeyCodeForKeyEvent(unsigned int keycode, bool isKeypad = false);
    265 #endif
    266 
    267 } // namespace WebCore
    268 
    269 #endif // PlatformKeyboardEvent_h
    270