themes: Workaround for bug where a background color of RGB 0,0,0 in Black color schem...
[ntk.git] / src / Fl_get_key_mac.cxx
blob167a7066597d03d7805f7d37f0f0fd740f9805e6
1 //
2 // "$Id: Fl_get_key_mac.cxx 8624 2011-04-26 17:28:10Z manolo $"
3 //
4 // MacOS keyboard state routines for the Fast Light Tool Kit (FLTK).
5 //
6 // Copyright 1998-2010 by Bill Spitzak and others.
7 //
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Library General Public
10 // License as published by the Free Software Foundation; either
11 // version 2 of the License, or (at your option) any later version.
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Library General Public License for more details.
18 // You should have received a copy of the GNU Library General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 // USA.
23 // Please report all bugs and problems on the following page:
25 // http://www.fltk.org/str.php
28 // Return the current state of a key. Keys are named by fltk symbols,
29 // which are actually X keysyms. So this has to translate to macOS
30 // symbols.
32 #include <FL/Fl.H>
33 #include <FL/x.H>
34 #include <config.h>
36 // convert an FLTK (X) keysym to a MacOS symbol:
37 // See also the inverse converter in table macKeyLookUp of Fl_cocoa.mm
38 // This table is in numeric order by FLTK symbol order for binary search.
39 // The list of Mac OS virtual keycodes appears with OS 10.5 in
40 // ...../Carbon.framework/Frameworks/HIToolbox.framework/Headers/Events.h
42 static const struct {unsigned short vk, fltk;} vktab[] = {
43 { 49, ' ' }, { 39, '\'' }, { 43, ',' }, { 27, '-' }, { 47, '.' }, { 44, '/' },
44 { 29, '0' }, { 18, '1' }, { 19, '2' }, { 20, '3' },
45 { 21, '4' }, { 23, '5' }, { 22, '6' }, { 26, '7' },
46 { 28, '8' }, { 25, '9' }, { 41, ';' }, { 24, '=' },
47 { 0, 'A' }, { 11, 'B' }, { 8, 'C' }, { 2, 'D' },
48 { 14, 'E' }, { 3, 'F' }, { 5, 'G' }, { 4, 'H' },
49 { 34, 'I' }, { 38, 'J' }, { 40, 'K' }, { 37, 'L' },
50 { 46, 'M' }, { 45, 'N' }, { 31, 'O' }, { 35, 'P' },
51 { 12, 'Q' }, { 15, 'R' }, { 1, 'S' }, { 17, 'T' },
52 { 32, 'U' }, { 9, 'V' }, { 13, 'W' }, { 7, 'X' },
53 { 16, 'Y' }, { 6, 'Z' },
54 { 33, '[' }, { 30, ']' }, { 50, '`' }, { 42, '\\' },
55 { 51, FL_BackSpace }, { 48, FL_Tab }, { 36, FL_Enter }, { 0x7F, FL_Pause },
56 { 0x7F, FL_Scroll_Lock }, { 53, FL_Escape }, { 0x73, FL_Home }, { 123, FL_Left },
57 { 126, FL_Up }, { 124, FL_Right }, { 125, FL_Down }, { 0x74, FL_Page_Up },
58 { 0x79, FL_Page_Down }, { 119, FL_End }, { 0x7F, FL_Print }, { 0x7F, FL_Insert },
59 { 0x6e, FL_Menu }, { 114, FL_Help }, { 0x47, FL_Num_Lock },
60 { 76, FL_KP_Enter }, { 67, FL_KP+'*' }, { 69, FL_KP+'+'}, { 78, FL_KP+'-' }, { 65, FL_KP+'.' }, { 75, FL_KP+'/' },
61 { 82, FL_KP+'0' }, { 83, FL_KP+'1' }, { 84, FL_KP+'2' }, { 85, FL_KP+'3' },
62 { 86, FL_KP+'4' }, { 87, FL_KP+'5' }, { 88, FL_KP+'6' }, { 89, FL_KP+'7' },
63 { 91, FL_KP+'8' }, { 92, FL_KP+'9' }, { 81, FL_KP+'=' },
64 { 0x7a, FL_F+1 }, { 0x78, FL_F+2 }, { 0x63, FL_F+3 }, { 0x76, FL_F+4 },
65 { 0x60, FL_F+5 }, { 0x61, FL_F+6 }, { 0x62, FL_F+7 }, { 0x64, FL_F+8 },
66 { 0x65, FL_F+9 }, { 0x6D, FL_F+10 }, { 0x67, FL_F+11 }, { 0x6f, FL_F+12 },
67 { 0x69, FL_F+13 }, { 0x6B, FL_F+14 }, { 0x71, FL_F+15 }, { 0x6A, FL_F+16 },
68 { 0x38, FL_Shift_L }, { 0x3C, FL_Shift_R }, { 0x3B, FL_Control_L }, { 0x3E, FL_Control_R },
69 { 0x39, FL_Caps_Lock }, { 0x37, FL_Meta_L }, { 0x36, FL_Meta_R },
70 { 0x3A, FL_Alt_L }, { 0x3D, FL_Alt_R }, { 0x75, FL_Delete },
73 static int fltk2mac(int fltk) {
74 int a = 0;
75 int b = sizeof(vktab)/sizeof(*vktab);
76 while (a < b) {
77 int c = (a+b)/2;
78 if (vktab[c].fltk == fltk) return vktab[c].vk;
79 if (vktab[c].fltk < fltk) a = c+1; else b = c;
81 return 127;
84 //: returns true, if that key was pressed during the last event
85 int Fl::event_key(int k) {
86 return get_key(k);
89 //: returns true, if that key is pressed right now
90 int Fl::get_key(int k) {
91 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
92 if(CGEventSourceKeyState != NULL) {
93 return (int)CGEventSourceKeyState(kCGEventSourceStateCombinedSessionState, fltk2mac(k) );
95 else
96 #endif
98 typedef UInt32 fl_KeyMap[4];
99 fl_KeyMap foo;
100 // use the GetKeys Carbon function
101 typedef void (*keymap_f)(fl_KeyMap);
102 static keymap_f f = NULL;
103 if (!f) f = ( keymap_f )Fl_X::get_carbon_function("GetKeys");
104 (*f)(foo);
105 #ifdef MAC_TEST_FOR_KEYCODES
106 static int cnt = 0;
107 if (cnt++>1024) {
108 cnt = 0;
109 printf("%08x %08x %08x %08x\n", (ulong*)(foo)[3], (ulong*)(foo)[2], (ulong*)(foo)[1], (ulong*)(foo)[0]);
111 #endif
112 unsigned char *b = (unsigned char*)foo;
113 // KP_Enter can be at different locations for Powerbooks vs. desktop Macs
114 if (k==FL_KP_Enter) {
115 return (((b[0x34>>3]>>(0x34&7))&1)||((b[0x4c>>3]>>(0x4c&7))&1));
117 int i = fltk2mac(k);
118 return (b[i>>3]>>(i&7))&1;
123 // End of "$Id: Fl_get_key_mac.cxx 8624 2011-04-26 17:28:10Z manolo $".