WebCore/platform/PlatformKeyboardEvent.h
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     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 : public FastAllocBase {
       
    79     public:
       
    80         enum Type {
       
    81             // KeyDown is sent by platforms such as Mac OS X, gtk and Qt, and has information about both physical pressed key, and its translation.
       
    82             // For DOM processing, it needs to be disambiguated as RawKeyDown or Char event.
       
    83             KeyDown,
       
    84 
       
    85             // KeyUp is sent by all platforms.
       
    86             KeyUp,
       
    87 
       
    88             // These events are sent by platforms such as Windows and wxWidgets. RawKeyDown only has information about a physical key, and Char
       
    89             // only has information about a character it was translated into.
       
    90             RawKeyDown,
       
    91             Char
       
    92         };
       
    93 
       
    94         enum ModifierKey {
       
    95             AltKey = 1 << 0,
       
    96             CtrlKey = 1 << 1,
       
    97             MetaKey = 1 << 2,
       
    98             ShiftKey = 1 << 3,
       
    99         };
       
   100 
       
   101         PlatformKeyboardEvent()
       
   102             : m_type(KeyDown)
       
   103             , m_autoRepeat(false)
       
   104             , m_windowsVirtualKeyCode(0)
       
   105             , m_nativeVirtualKeyCode(0)
       
   106             , m_isKeypad(false)
       
   107             , m_shiftKey(false)
       
   108             , m_ctrlKey(false)
       
   109             , m_altKey(false)
       
   110             , m_metaKey(false)
       
   111 #if PLATFORM(WIN) || PLATFORM(CHROMIUM)
       
   112             , m_isSystemKey(false)
       
   113 #endif
       
   114 #if PLATFORM(GTK)
       
   115             , m_gdkEventKey(0)
       
   116 #endif
       
   117 #if PLATFORM(QT)
       
   118             , m_qtEvent(0)
       
   119 #endif
       
   120         {
       
   121         }
       
   122         
       
   123         Type type() const { return m_type; }
       
   124         void disambiguateKeyDownEvent(Type, bool backwardCompatibilityMode = false); // Only used on platforms that need it, i.e. those that generate KeyDown events.
       
   125 
       
   126         // Text as as generated by processing a virtual key code with a keyboard layout
       
   127         // (in most cases, just a character code, but the layout can emit several
       
   128         // characters in a single keypress event on some platforms).
       
   129         // This may bear no resemblance to the ultimately inserted text if an input method
       
   130         // processes the input.
       
   131         // Will be null for KeyUp and RawKeyDown events.
       
   132         String text() const { return m_text; }
       
   133 
       
   134         // Text that would have been generated by the keyboard if no modifiers were pressed
       
   135         // (except for Shift); useful for shortcut (accelerator) key handling.
       
   136         // Otherwise, same as text().
       
   137         String unmodifiedText() const { return m_unmodifiedText; }
       
   138 
       
   139         // Most compatible Windows virtual key code associated with the event.
       
   140         // Zero for Char events.
       
   141         int windowsVirtualKeyCode() const { return m_windowsVirtualKeyCode; }
       
   142         void setWindowsVirtualKeyCode(int code) { m_windowsVirtualKeyCode = code; }
       
   143 
       
   144         int nativeVirtualKeyCode() const { return m_nativeVirtualKeyCode; }
       
   145         void setNativeVirtualKeyCode(int code) { m_nativeVirtualKeyCode = code; }
       
   146 
       
   147         String keyIdentifier() const { return m_keyIdentifier; }
       
   148         bool isAutoRepeat() const { return m_autoRepeat; }
       
   149         void setIsAutoRepeat(bool in) { m_autoRepeat = in; }
       
   150         bool isKeypad() const { return m_isKeypad; }
       
   151         bool shiftKey() const { return m_shiftKey; }
       
   152         bool ctrlKey() const { return m_ctrlKey; }
       
   153         bool altKey() const { return m_altKey; }
       
   154         bool metaKey() const { return m_metaKey; }
       
   155         unsigned modifiers() const {
       
   156             return (altKey() ? AltKey : 0)
       
   157                 | (ctrlKey() ? CtrlKey : 0)
       
   158                 | (metaKey() ? MetaKey : 0)
       
   159                 | (shiftKey() ? ShiftKey : 0);
       
   160         }
       
   161 
       
   162         static bool currentCapsLockState();
       
   163         static void getCurrentModifierState(bool& shiftKey, bool& ctrlKey, bool& altKey, bool& metaKey);
       
   164 
       
   165 #if PLATFORM(MAC)
       
   166         PlatformKeyboardEvent(NSEvent*);
       
   167         NSEvent* macEvent() const { return m_macEvent.get(); }
       
   168 #endif
       
   169 
       
   170 #if PLATFORM(WIN)
       
   171         PlatformKeyboardEvent(HWND, WPARAM, LPARAM, Type, bool);
       
   172 #endif
       
   173 
       
   174 #if PLATFORM(GTK)
       
   175         PlatformKeyboardEvent(GdkEventKey*);
       
   176         GdkEventKey* gdkEventKey() const;
       
   177 #endif
       
   178 
       
   179 #if PLATFORM(QT)
       
   180         PlatformKeyboardEvent(QKeyEvent*);
       
   181         QKeyEvent* qtEvent() const { return m_qtEvent; }
       
   182 #endif
       
   183 
       
   184 #if PLATFORM(WX)
       
   185         PlatformKeyboardEvent(wxKeyEvent&);
       
   186 #endif
       
   187 
       
   188 #if PLATFORM(HAIKU)
       
   189         PlatformKeyboardEvent(BMessage*);
       
   190 #endif
       
   191 
       
   192 #if PLATFORM(BREWMP)
       
   193         PlatformKeyboardEvent(AEEEvent, uint16, uint32, Type);
       
   194 #endif
       
   195 
       
   196 #if PLATFORM(WIN) || PLATFORM(CHROMIUM)
       
   197         bool isSystemKey() const { return m_isSystemKey; }
       
   198 #endif
       
   199 
       
   200 #if PLATFORM(EFL)
       
   201         PlatformKeyboardEvent(const Evas_Event_Key_Down*);
       
   202         PlatformKeyboardEvent(const Evas_Event_Key_Up*);
       
   203 #endif
       
   204 
       
   205     protected:
       
   206         Type m_type;
       
   207         String m_text;
       
   208         String m_unmodifiedText;
       
   209         String m_keyIdentifier;
       
   210         bool m_autoRepeat;
       
   211         int m_windowsVirtualKeyCode;
       
   212         int m_nativeVirtualKeyCode;
       
   213         bool m_isKeypad;
       
   214         bool m_shiftKey;
       
   215         bool m_ctrlKey;
       
   216         bool m_altKey;
       
   217         bool m_metaKey;
       
   218 
       
   219 #if PLATFORM(MAC)
       
   220         RetainPtr<NSEvent> m_macEvent;
       
   221 #endif
       
   222 #if PLATFORM(WIN) || PLATFORM(CHROMIUM)
       
   223         bool m_isSystemKey;
       
   224 #endif
       
   225 #if PLATFORM(GTK)
       
   226         GdkEventKey* m_gdkEventKey;
       
   227 #endif
       
   228 #if PLATFORM(QT)
       
   229         QKeyEvent* m_qtEvent;
       
   230 #endif
       
   231     };
       
   232     
       
   233 #if PLATFORM(QT)
       
   234 // Used by WebKit2.
       
   235 String keyIdentifierForQtKeyCode(int keyCode);
       
   236 int windowsKeyCodeForKeyEvent(unsigned int keycode, bool isKeypad = false);    
       
   237 #endif
       
   238 
       
   239 } // namespace WebCore
       
   240 
       
   241 #endif // PlatformKeyboardEvent_h