KeyboardHandler.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. // Permission is hereby granted, free of charge, to any person obtaining
  2. // a copy of this software and associated documentation files (the
  3. // "Software"), to deal in the Software without restriction, including
  4. // without limitation the rights to use, copy, modify, merge, publish,
  5. // distribute, sublicense, and/or sell copies of the Software, and to
  6. // permit persons to whom the Software is furnished to do so, subject to
  7. // the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be
  10. // included in all copies or substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  14. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  15. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  16. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  17. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  18. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. //
  20. // Copyright (c) 2007 Novell, Inc.
  21. //
  22. // Authors:
  23. // Geoff Norton ([email protected])
  24. //
  25. //
  26. using System;
  27. using System.Collections;
  28. using System.Text;
  29. using System.Globalization;
  30. using System.Runtime.InteropServices;
  31. namespace System.Windows.Forms.CarbonInternal {
  32. internal class KeyboardHandler : EventHandlerBase, IEventHandler {
  33. internal const uint kEventRawKeyDown = 1;
  34. internal const uint kEventRawKeyRepeat = 2;
  35. internal const uint kEventRawKeyUp = 3;
  36. internal const uint kEventRawKeyModifiersChanged = 4;
  37. internal const uint kEventHotKeyPressed = 5;
  38. internal const uint kEventHotKeyReleased = 6;
  39. internal const uint kEventParamKeyMacCharCodes = 1801676914;
  40. internal const uint kEventParamKeyCode = 1801678692;
  41. internal const uint kEventParamKeyModifiers = 1802334052;
  42. internal const uint kEventTextInputUnicodeForKeyEvent = 2;
  43. internal const uint kEventParamTextInputSendText = 1953723512;
  44. internal const uint typeChar = 1413830740;
  45. internal const uint typeUInt32 = 1835100014;
  46. internal const uint typeUnicodeText = 1970567284;
  47. internal static byte [] key_filter_table;
  48. internal static byte [] key_modifier_table;
  49. internal static byte [] key_translation_table;
  50. internal static byte [] char_translation_table;
  51. internal static bool translate_modifier = false;
  52. internal string ComposedString;
  53. static KeyboardHandler () {
  54. // our key filter table is a 256 byte array - if the corresponding byte
  55. // is set the key should be filtered from WM_CHAR (apple pushes unicode events
  56. // for some keys which win32 only handles as KEYDOWN
  57. // currently filtered:
  58. // fn+f* == 16
  59. // left == 28
  60. // right == 29
  61. // up == 30
  62. // down == 31
  63. // Please update this list as well as the table as more keys are found
  64. key_filter_table = new byte [256] {
  65. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  66. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01,
  67. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  68. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  69. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  70. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  71. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  72. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  73. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  74. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  75. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  76. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  77. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  78. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  79. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  80. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  81. };
  82. // our char translation table is a set of translations from mac char codes
  83. // to win32 vkey codes
  84. // most things map directly
  85. char_translation_table = new byte [256] {
  86. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  87. 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 0x25, 0x27, 0x26, 0x28,
  88. 32, 49, 34, 51, 52, 53, 55, 222, 57, 48, 56, 187, 188, 189, 190, 191,
  89. 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 186, 60, 61, 62, 63,
  90. 50, 65, 66, 67, 68, 187, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
  91. 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 219, 220, 221, 54, 189,
  92. 192, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
  93. 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 123, 124, 125, 126, 0x2e,
  94. 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143,
  95. 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159,
  96. 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175,
  97. 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
  98. 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207,
  99. 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223,
  100. 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239,
  101. 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255
  102. };
  103. key_translation_table = new byte [256] {
  104. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  105. 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
  106. 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
  107. 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
  108. 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
  109. 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
  110. 0x74, 0x75, 0x76, 0x72, 0x77, 0x78, 0x79, 103, 104, 105, 106, 107, 108, 109, 0x7a, 0x7b,
  111. 112, 113, 114, 115, 116, 117, 0x73, 119, 0x71, 121, 0x70, 123, 124, 125, 126, 127,
  112. 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143,
  113. 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159,
  114. 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175,
  115. 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
  116. 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207,
  117. 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223,
  118. 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239,
  119. 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255
  120. };
  121. // the key modifier table is a state table of the possible modifier keys
  122. // apple currently only goes up to 1 << 14 keys, we've extended this to 32
  123. // bytes as thats the size that apple uses
  124. key_modifier_table = new byte [32];
  125. }
  126. internal KeyboardHandler (XplatUICarbon driver) : base (driver) {}
  127. private void ModifierToVirtualKey (int i, ref MSG msg, bool down) {
  128. msg.hwnd = XplatUICarbon.FocusWindow;
  129. if (i == 9 || i == 13) {
  130. msg.message = (down ? Msg.WM_KEYDOWN : Msg.WM_KEYUP);
  131. msg.wParam = (IntPtr) VirtualKeys.VK_SHIFT;
  132. msg.lParam = IntPtr.Zero;
  133. return;
  134. }
  135. if (i == 12 || i == 14) {
  136. msg.message = (down ? Msg.WM_KEYDOWN : Msg.WM_KEYUP);
  137. msg.wParam = (IntPtr) VirtualKeys.VK_CONTROL;
  138. msg.lParam = IntPtr.Zero;
  139. return;
  140. }
  141. if (i == 8) {
  142. msg.message = (down ? Msg.WM_SYSKEYDOWN : Msg.WM_SYSKEYUP);
  143. msg.wParam = (IntPtr) VirtualKeys.VK_MENU;
  144. msg.lParam = new IntPtr (0x20000000);
  145. return;
  146. }
  147. return;
  148. }
  149. public void ProcessModifiers (IntPtr eventref, ref MSG msg) {
  150. // we get notified when modifiers change, but not specifically what changed
  151. UInt32 modifiers = 0;
  152. GetEventParameter (eventref, kEventParamKeyModifiers, typeUInt32, IntPtr.Zero, (uint)Marshal.SizeOf (typeof (UInt32)), IntPtr.Zero, ref modifiers);
  153. for (int i = 0; i < 32; i++) {
  154. if (key_modifier_table [i] == 0x01 && (modifiers & (1 << i)) == 0) {
  155. ModifierToVirtualKey (i, ref msg, false);
  156. key_modifier_table [i] = 0x00;
  157. return;
  158. } else if (key_modifier_table [i] == 0x00 && (modifiers & (1 << i)) == (1 << i)) {
  159. ModifierToVirtualKey (i, ref msg, true);
  160. key_modifier_table [i] = 0x01;
  161. return;
  162. }
  163. }
  164. return;
  165. }
  166. public void ProcessText (IntPtr eventref, ref MSG msg) {
  167. UInt32 size = 0;
  168. IntPtr buffer = IntPtr.Zero;
  169. byte [] bdata;
  170. // get the size of the unicode buffer
  171. GetEventParameter (eventref, kEventParamTextInputSendText, typeUnicodeText, IntPtr.Zero, 0, ref size, IntPtr.Zero);
  172. buffer = Marshal.AllocHGlobal ((int) size);
  173. bdata = new byte [size];
  174. // get the actual text buffer
  175. GetEventParameter (eventref, kEventParamTextInputSendText, typeUnicodeText, IntPtr.Zero, size, IntPtr.Zero, buffer);
  176. Marshal.Copy (buffer, bdata, 0, (int) size);
  177. Marshal.FreeHGlobal (buffer);
  178. if (key_filter_table [bdata [0]] == 0x00) {
  179. if (size == 1) {
  180. msg.message = Msg.WM_CHAR;
  181. msg.wParam = BitConverter.IsLittleEndian ? (IntPtr) bdata [0] : (IntPtr) bdata [size-1];
  182. msg.lParam = IntPtr.Zero;
  183. msg.hwnd = XplatUICarbon.FocusWindow;
  184. } else {
  185. msg.message = Msg.WM_IME_COMPOSITION;
  186. Encoding enc = BitConverter.IsLittleEndian ? Encoding.Unicode : Encoding.BigEndianUnicode;
  187. ComposedString = enc.GetString (bdata);
  188. msg.hwnd = XplatUICarbon.FocusWindow;
  189. }
  190. }
  191. }
  192. public void ProcessKeyPress (IntPtr eventref, ref MSG msg) {
  193. byte charCode = 0x0;
  194. byte keyCode = 0x0;
  195. GetEventParameter (eventref, kEventParamKeyMacCharCodes, typeChar, IntPtr.Zero, (uint)Marshal.SizeOf (typeof (byte)), IntPtr.Zero, ref charCode);
  196. GetEventParameter (eventref, kEventParamKeyCode, typeUInt32, IntPtr.Zero, (uint)Marshal.SizeOf (typeof (byte)), IntPtr.Zero, ref keyCode);
  197. msg.lParam = (IntPtr) charCode;
  198. msg.wParam = charCode == 0x10 ? (IntPtr) key_translation_table [keyCode] : (IntPtr) char_translation_table [charCode];
  199. msg.hwnd = XplatUICarbon.FocusWindow;
  200. }
  201. public bool ProcessEvent (IntPtr callref, IntPtr eventref, IntPtr handle, uint kind, ref MSG msg) {
  202. uint klass = EventHandler.GetEventClass (eventref);
  203. bool result = true;
  204. if (klass == EventHandler.kEventClassTextInput) {
  205. switch (kind) {
  206. case kEventTextInputUnicodeForKeyEvent:
  207. ProcessText (eventref, ref msg);
  208. break;
  209. default:
  210. Console.WriteLine ("WARNING: KeyboardHandler.ProcessEvent default handler for kEventClassTextInput should not be reached");
  211. break;
  212. }
  213. } else if (klass == EventHandler.kEventClassKeyboard) {
  214. switch (kind) {
  215. case kEventRawKeyDown:
  216. case kEventRawKeyRepeat:
  217. msg.message = Msg.WM_KEYDOWN;
  218. ProcessKeyPress (eventref, ref msg);
  219. break;
  220. case kEventRawKeyUp:
  221. msg.message = Msg.WM_KEYUP;
  222. ProcessKeyPress (eventref, ref msg);
  223. break;
  224. case kEventRawKeyModifiersChanged:
  225. ProcessModifiers (eventref, ref msg);
  226. break;
  227. default:
  228. Console.WriteLine ("WARNING: KeyboardHandler.ProcessEvent default handler for kEventClassKeyboard should not be reached");
  229. break;
  230. }
  231. } else {
  232. Console.WriteLine ("WARNING: KeyboardHandler.ProcessEvent default handler for kEventClassTextInput should not be reached");
  233. }
  234. return result;
  235. }
  236. public bool TranslateMessage (ref MSG msg) {
  237. bool res = false;
  238. if (msg.message >= Msg.WM_KEYFIRST && msg.message <= Msg.WM_KEYLAST)
  239. res = true;
  240. if (msg.message != Msg.WM_KEYDOWN && msg.message != Msg.WM_SYSKEYDOWN && msg.message != Msg.WM_KEYUP && msg.message != Msg.WM_SYSKEYUP && msg.message != Msg.WM_CHAR && msg.message != Msg.WM_SYSCHAR)
  241. return res;
  242. if (key_modifier_table [8] == 0x01 && key_modifier_table [12] == 0x00 && key_modifier_table [14] == 0x00) {
  243. if (msg.message == Msg.WM_KEYDOWN) {
  244. msg.message = Msg.WM_SYSKEYDOWN;
  245. } else if (msg.message == Msg.WM_CHAR) {
  246. msg.message = Msg.WM_SYSCHAR;
  247. translate_modifier = true;
  248. } else if (msg.message == Msg.WM_KEYUP) {
  249. msg.message = Msg.WM_SYSKEYUP;
  250. } else {
  251. return res;
  252. }
  253. msg.lParam = new IntPtr (0x20000000);
  254. } else if (msg.message == Msg.WM_SYSKEYUP && translate_modifier && msg.wParam == (IntPtr) 18) {
  255. msg.message = Msg.WM_KEYUP;
  256. msg.lParam = IntPtr.Zero;
  257. translate_modifier = false;
  258. }
  259. return res;
  260. }
  261. internal Keys ModifierKeys {
  262. get {
  263. Keys keys = Keys.None;
  264. if (key_modifier_table [9] == 0x01 || key_modifier_table [13] == 0x01) { keys |= Keys.Shift; }
  265. if (key_modifier_table [8] == 0x01) { keys |= Keys.Alt; }
  266. if (key_modifier_table [12] == 0x01 || key_modifier_table [14] == 0x01) { keys |= Keys.Control; }
  267. return keys;
  268. }
  269. }
  270. [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
  271. static extern int GetEventParameter (IntPtr eventref, uint name, uint type, IntPtr outtype, uint size, ref UInt32 outsize, IntPtr data);
  272. [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
  273. static extern int GetEventParameter (IntPtr eventref, uint name, uint type, IntPtr outtype, uint size, IntPtr outsize, IntPtr data);
  274. [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
  275. static extern int GetEventParameter (IntPtr eventref, uint name, uint type, IntPtr outtype, uint size, IntPtr outsize, ref byte data);
  276. [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
  277. static extern int GetEventParameter (IntPtr eventref, uint name, uint type, IntPtr outtype, uint size, IntPtr outsize, ref UInt32 data);
  278. }
  279. internal enum KeyboardModifiers : uint {
  280. activeFlag = 1 << 0,
  281. btnState = 1 << 7,
  282. cmdKey = 1 << 8,
  283. shiftKey = 1 << 9,
  284. alphaLock = 1 << 10,
  285. optionKey = 1 << 11,
  286. controlKey = 1 << 12,
  287. rightShiftKey = 1 << 13,
  288. rightOptionKey = 1 << 14,
  289. rightControlKey = 1 << 14,
  290. }
  291. }