Back to index

lightning-sunbird  0.9+nobinonly
nsKeyCode.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Peter Hartshorn <peter@igelaus.com.au>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "nsKeyCode.h"
00040 
00041 #include <X11/Xlib.h>
00042 #include <X11/Xutil.h>
00043 #include <X11/keysym.h>
00044 #include <X11/keysymdef.h>
00045 
00046 #include "nsGUIEvent.h" // For NS_VK
00047 
00048 struct nsKeyConverter 
00049 {
00050   int          vkCode; // Platform independent key code
00051   unsigned int keysym; // XK keysym key code
00052 };
00053 
00054 struct nsKeyConverter nsKeycodes[] =
00055 {
00056   { NS_VK_CANCEL,        XK_Cancel},
00057   { NS_VK_BACK,          XK_BackSpace},
00058   { NS_VK_TAB,           XK_Tab},
00059 #ifdef XK_ISO_Left_Tab
00060   { NS_VK_TAB,           XK_ISO_Left_Tab }, // SunOS 5.5.1 doesn't have it.
00061 #endif
00062   { NS_VK_CLEAR,         XK_Clear},
00063   { NS_VK_RETURN,        XK_Return},
00064   { NS_VK_ENTER,         XK_KP_Enter},
00065   { NS_VK_SHIFT,         XK_Shift_L},
00066   { NS_VK_SHIFT,         XK_Shift_R},
00067   { NS_VK_CONTROL,       XK_Control_L},
00068   { NS_VK_CONTROL,       XK_Control_R},
00069   { NS_VK_ALT,           XK_Alt_L},
00070   { NS_VK_ALT,           XK_Alt_R},
00071   { NS_VK_PAUSE,         XK_Pause},
00072   { NS_VK_CAPS_LOCK,     XK_Caps_Lock},
00073   { NS_VK_ESCAPE,        XK_Escape},
00074   { NS_VK_SPACE,         XK_KP_Space},
00075   { NS_VK_SPACE,         XK_space},
00076   { NS_VK_PAGE_UP,       XK_Page_Up},
00077   { NS_VK_PAGE_UP,       XK_KP_Page_Up},
00078   { NS_VK_PAGE_DOWN,     XK_Page_Down},
00079   { NS_VK_PAGE_DOWN,     XK_KP_Page_Down},
00080   { NS_VK_END,           XK_End},
00081   { NS_VK_END,           XK_KP_End},
00082   { NS_VK_HOME,          XK_Home},
00083   { NS_VK_HOME,          XK_KP_Home},
00084   { NS_VK_LEFT,          XK_Left},
00085   { NS_VK_LEFT,          XK_KP_Left},
00086   { NS_VK_UP,            XK_Up},
00087   { NS_VK_UP,            XK_KP_Up},
00088   { NS_VK_RIGHT,         XK_Right},
00089   { NS_VK_RIGHT,         XK_KP_Right},
00090   { NS_VK_DOWN,          XK_Down},
00091   { NS_VK_DOWN,          XK_KP_Down},
00092   { NS_VK_PRINTSCREEN,   XK_Print},
00093   { NS_VK_INSERT,        XK_Insert},
00094   { NS_VK_INSERT,        XK_KP_Insert},
00095   { NS_VK_DELETE,        XK_Delete},
00096   { NS_VK_DELETE,        XK_KP_Delete},
00097   { NS_VK_0,             XK_0},
00098   { NS_VK_1,             XK_1},
00099   { NS_VK_2,             XK_2},
00100   { NS_VK_3,             XK_3},
00101   { NS_VK_4,             XK_4},
00102   { NS_VK_5,             XK_5},
00103   { NS_VK_6,             XK_6},
00104   { NS_VK_7,             XK_7},
00105   { NS_VK_8,             XK_8},
00106   { NS_VK_9,             XK_9},
00107   { NS_VK_SEMICOLON,     XK_semicolon},
00108   { NS_VK_EQUALS,        XK_equal},
00109   { NS_VK_NUMPAD0,       XK_KP_0},
00110   { NS_VK_NUMPAD1,       XK_KP_1},
00111   { NS_VK_NUMPAD2,       XK_KP_2},
00112   { NS_VK_NUMPAD3,       XK_KP_3},
00113   { NS_VK_NUMPAD4,       XK_KP_4},
00114   { NS_VK_NUMPAD5,       XK_KP_5},
00115   { NS_VK_NUMPAD6,       XK_KP_6},
00116   { NS_VK_NUMPAD7,       XK_KP_7},
00117   { NS_VK_NUMPAD8,       XK_KP_8},
00118   { NS_VK_NUMPAD9,       XK_KP_9},
00119   { NS_VK_MULTIPLY,      XK_KP_Multiply},
00120   { NS_VK_ADD,           XK_KP_Add},
00121   { NS_VK_SEPARATOR,     XK_KP_Separator},
00122   { NS_VK_SUBTRACT,      XK_KP_Subtract},
00123   { NS_VK_DECIMAL,       XK_KP_Decimal},
00124   { NS_VK_DIVIDE,        XK_KP_Divide},
00125   { NS_VK_F1,            XK_F1},
00126   { NS_VK_F2,            XK_F2},
00127   { NS_VK_F3,            XK_F3},
00128   { NS_VK_F4,            XK_F4},
00129   { NS_VK_F5,            XK_F5},
00130   { NS_VK_F6,            XK_F6},
00131   { NS_VK_F7,            XK_F7},
00132   { NS_VK_F8,            XK_F8},
00133   { NS_VK_F9,            XK_F9},
00134   { NS_VK_F10,           XK_F10},
00135   { NS_VK_F11,           XK_F11},
00136   { NS_VK_F12,           XK_F12},
00137   { NS_VK_F13,           XK_F13},
00138   { NS_VK_F14,           XK_F14},
00139   { NS_VK_F15,           XK_F15},
00140   { NS_VK_F16,           XK_F16},
00141   { NS_VK_F17,           XK_F17},
00142   { NS_VK_F18,           XK_F18},
00143   { NS_VK_F19,           XK_F19},
00144   { NS_VK_F20,           XK_F20},
00145   { NS_VK_F21,           XK_F21},
00146   { NS_VK_F22,           XK_F22},
00147   { NS_VK_F23,           XK_F23},
00148   { NS_VK_F24,           XK_F24},
00149   { NS_VK_NUM_LOCK,      XK_Num_Lock},
00150   { NS_VK_SCROLL_LOCK,   XK_Scroll_Lock},
00151   { NS_VK_COMMA,         XK_comma},
00152   { NS_VK_PERIOD,        XK_period},
00153   { NS_VK_SLASH,         XK_slash},
00154   { NS_VK_BACK_QUOTE,    XK_grave},
00155   { NS_VK_OPEN_BRACKET,  XK_bracketleft},
00156   { NS_VK_BACK_SLASH,    XK_backslash},
00157   { NS_VK_CLOSE_BRACKET, XK_bracketright},
00158   { NS_VK_QUOTE,         XK_apostrophe}
00159 };
00160 
00161 PRInt32
00162 nsKeyCode::ConvertKeySymToVirtualKey(KeySym keysym)
00163 {
00164   unsigned int i;
00165   unsigned int length = sizeof(nsKeycodes) / sizeof(struct nsKeyConverter);
00166 
00167   if (keysym >= XK_a && keysym <= XK_z)
00168     return keysym - XK_a + NS_VK_A;
00169   if (keysym >= XK_A && keysym <= XK_Z)
00170     return keysym - XK_A + NS_VK_A;
00171 
00172   for (i = 0; i < length; i++) 
00173   {
00174     if (nsKeycodes[i].keysym == keysym)
00175     {
00176       return(nsKeycodes[i].vkCode);
00177     }
00178   }
00179   
00180   return((int)0);
00181 }
00182 
00184 /* static */ PRBool
00185 nsKeyCode::KeyCodeIsModifier(KeyCode aKeyCode)
00186 {
00187   if (aKeyCode == XK_Shift_L    ||
00188       aKeyCode == XK_Shift_R    ||
00189       aKeyCode == XK_Control_L  ||
00190       aKeyCode == XK_Control_R  ||
00191       aKeyCode == XK_Caps_Lock  ||
00192       aKeyCode == XK_Shift_Lock ||
00193       aKeyCode == XK_Meta_L     ||
00194       aKeyCode == XK_Meta_R     ||
00195       aKeyCode == XK_Alt_L      ||
00196       aKeyCode == XK_Alt_R)
00197   {
00198     return PR_TRUE;
00199   }
00200 
00201   return PR_FALSE;
00202 }
00204 /* static */ KeySym
00205 nsKeyCode::ConvertKeyCodeToKeySym(Display * aDisplay, KeyCode aKeyCode)
00206 {
00207   KeySym keysym = 0;
00208 
00209   keysym = XKeycodeToKeysym(aDisplay, aKeyCode, 0);
00210 
00211   return keysym;
00212 }