fltk 1.3.0rc3
About: FLTK (Fast Light Tool Kit) is a cross-platform C++ GUI toolkit for UNIX/Linux (X11), Microsoft Windows, and MacOS X. Release candidate.
  SfR Fresh Dox: fltk-1.3.0rc3-source.tar.gz ("inofficial" and yet experimental doxygen-generated source code documentation)  

Fl_get_key_mac.cxx

Go to the documentation of this file.
00001 //
00002 // "$Id: Fl_get_key_mac.cxx 8173 2011-01-03 16:50:34Z manolo $"
00003 //
00004 // MacOS keyboard state routines for the Fast Light Tool Kit (FLTK).
00005 //
00006 // Copyright 1998-2010 by Bill Spitzak and others.
00007 //
00008 // This library is free software; you can redistribute it and/or
00009 // modify it under the terms of the GNU Library General Public
00010 // License as published by the Free Software Foundation; either
00011 // version 2 of the License, or (at your option) any later version.
00012 //
00013 // This library is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016 // Library General Public License for more details.
00017 //
00018 // You should have received a copy of the GNU Library General Public
00019 // License along with this library; if not, write to the Free Software
00020 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00021 // USA.
00022 //
00023 // Please report all bugs and problems on the following page:
00024 //
00025 //     http://www.fltk.org/str.php
00026 //
00027 
00028 // Return the current state of a key.  Keys are named by fltk symbols,
00029 // which are actually X keysyms.  So this has to translate to macOS
00030 // symbols.
00031 
00032 #include <FL/Fl.H>
00033 #include <FL/x.H>
00034 #include <config.h>
00035 
00036 // convert an FLTK (X) keysym to a MacOS symbol:
00037 // See also the inverse converter in table macKeyLookUp of Fl_cocoa.mm
00038 // This table is in numeric order by FLTK symbol order for binary search:
00039 
00040 static const struct {unsigned short vk, fltk;} vktab[] = {
00041   { 49, ' ' }, { 39, '\'' }, { 43, ',' }, { 27, '-' }, { 47, '.' }, { 44, '/' }, 
00042   { 29, '0' }, { 18, '1'  }, { 19, '2'  }, { 20, '3'  }, 
00043   { 21, '4' }, { 23, '5'  }, { 22, '6'  }, { 26, '7'  }, 
00044   { 28, '8' }, { 25, '9'  }, { 41, ';'  }, { 24, '='  },
00045   {  0, 'A' }, { 11, 'B'  }, {  8, 'C'  }, {  2, 'D'  }, 
00046   { 14, 'E' }, {  3, 'F'  }, {  5, 'G'  }, {  4, 'H'  }, 
00047   { 34, 'I' }, { 38, 'J'  }, { 40, 'K'  }, { 37, 'L'  }, 
00048   { 46, 'M' }, { 45, 'N'  }, { 31, 'O'  }, { 35, 'P'  }, 
00049   { 12, 'Q' }, { 15, 'R'  }, {  1, 'S'  }, { 17, 'T'  }, 
00050   { 32, 'U' }, {  9, 'V'  }, { 13, 'W'  }, {  7, 'X'  }, 
00051   { 16, 'Y' }, {  6, 'Z'  }, 
00052   { 33, '[' }, { 30, ']' }, { 50, '`' },  { 42, '|' },
00053   { 51, FL_BackSpace }, { 48, FL_Tab }, { 36, FL_Enter }, { 127, FL_Pause },
00054   { 107, FL_Scroll_Lock }, { 53, FL_Escape }, { 0x73, FL_Home }, { 123, FL_Left },
00055   { 126, FL_Up }, { 124, FL_Right }, { 125, FL_Down }, { 0x74, FL_Page_Up },
00056   { 0x79, FL_Page_Down },  { 119, FL_End }, { 0x71, FL_Print }, { 127, FL_Insert },
00057   { 0x6e, FL_Menu }, { 114, FL_Help }, { 0x47, FL_Num_Lock },
00058   { 76, FL_KP_Enter }, { 67, FL_KP+'*' }, { 69, FL_KP+'+'}, { 78, FL_KP+'-' }, { 65, FL_KP+'.' }, { 75, FL_KP+'/' }, 
00059   { 82, FL_KP+'0' }, { 83, FL_KP+'1' }, { 84, FL_KP+'2' }, { 85, FL_KP+'3' }, 
00060   { 86, FL_KP+'4' }, { 87, FL_KP+'5' }, { 88, FL_KP+'6' }, { 89, FL_KP+'7' }, 
00061   { 91, FL_KP+'8' }, { 92, FL_KP+'9' }, { 81, FL_KP+'=' }, 
00062   { 0x7a, FL_F+1 }, { 0x78, FL_F+2  }, { 0x63, FL_F+3  }, { 0x76, FL_F+4  }, 
00063   { 0x60, FL_F+5 }, { 0x61, FL_F+6  }, { 0x62, FL_F+7  }, { 0x64, FL_F+8  }, 
00064   { 0x65, FL_F+9 }, { 0x6D, FL_F+10 }, { 0x67, FL_F+11 }, { 0x6f, FL_F+12 }, 
00065   { 56, FL_Shift_L }, { 56, FL_Shift_R }, { 59, FL_Control_L }, { 59, FL_Control_R }, 
00066   { 57, FL_Caps_Lock }, { 55, FL_Meta_L }, { 55, FL_Meta_R },
00067   { 58, FL_Alt_L }, { 58, FL_Alt_R }, { 0x75, FL_Delete },
00068 };
00069 
00070 static int fltk2mac(int fltk) {
00071   int a = 0;
00072   int b = sizeof(vktab)/sizeof(*vktab);
00073   while (a < b) {
00074     int c = (a+b)/2;
00075     if (vktab[c].fltk == fltk) return vktab[c].vk;
00076     if (vktab[c].fltk < fltk) a = c+1; else b = c;
00077   }
00078   return 127;
00079 }
00080 
00081 //: returns true, if that key was pressed during the last event
00082 int Fl::event_key(int k) {
00083   return get_key(k);
00084 }
00085 
00086 //: returns true, if that key is pressed right now
00087 int Fl::get_key(int k) {
00088 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
00089   if(CGEventSourceKeyState != NULL) {
00090     return (int)CGEventSourceKeyState(kCGEventSourceStateCombinedSessionState, fltk2mac(k) );
00091     }
00092   else 
00093 #endif
00094   {
00095   typedef UInt32 fl_KeyMap[4];
00096   fl_KeyMap foo;
00097   // use the GetKeys Carbon function
00098   typedef void (*keymap_f)(fl_KeyMap);
00099   static keymap_f f = NULL;
00100   if (!f) f = ( keymap_f )Fl_X::get_carbon_function("GetKeys");
00101   (*f)(foo);
00102 #ifdef MAC_TEST_FOR_KEYCODES
00103  static int cnt = 0;
00104  if (cnt++>1024) {
00105   cnt = 0;
00106   printf("%08x %08x %08x %08x\n", (ulong*)(foo)[3], (ulong*)(foo)[2], (ulong*)(foo)[1], (ulong*)(foo)[0]);
00107  }
00108 #endif
00109   unsigned char *b = (unsigned char*)foo;
00110   // KP_Enter can be at different locations for Powerbooks vs. desktop Macs
00111   if (k==FL_KP_Enter) {
00112     return (((b[0x34>>3]>>(0x34&7))&1)||((b[0x4c>>3]>>(0x4c&7))&1));
00113   }
00114   int i = fltk2mac(k);
00115   return (b[i>>3]>>(i&7))&1;
00116   }
00117 }
00118 
00119 //
00120 // End of "$Id: Fl_get_key_mac.cxx 8173 2011-01-03 16:50:34Z manolo $".
00121 //