OLD | NEW |
| (Empty) |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 /* | |
6 * Copyright (C) 2004, 2006, 2007 Apple Inc. All rights reserved. | |
7 * Copyright (C) 2006-2009 Google Inc. | |
8 * | |
9 * Redistribution and use in source and binary forms, with or without | |
10 * modification, are permitted provided that the following conditions | |
11 * are met: | |
12 * 1. Redistributions of source code must retain the above copyright | |
13 * notice, this list of conditions and the following disclaimer. | |
14 * 2. Redistributions in binary form must reproduce the above copyright | |
15 * notice, this list of conditions and the following disclaimer in the | |
16 * documentation and/or other materials provided with the distribution. | |
17 * | |
18 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY | |
19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR | |
22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
25 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | |
26 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
29 */ | |
30 | |
31 #include "content/browser/renderer_host/input/web_input_event_builders_mac.h" | |
32 | |
33 #import <ApplicationServices/ApplicationServices.h> | |
34 #import <Cocoa/Cocoa.h> | |
35 | |
36 #include "base/mac/sdk_forward_declarations.h" | |
37 #include "base/strings/string_util.h" | |
38 #include "content/browser/renderer_host/input/web_input_event_util.h" | |
39 #include "third_party/WebKit/public/web/WebInputEvent.h" | |
40 #include "ui/events/keycodes/keyboard_code_conversion.h" | |
41 #include "ui/events/keycodes/keyboard_code_conversion_mac.h" | |
42 | |
43 namespace content { | |
44 | |
45 namespace { | |
46 | |
47 // Return true if the target modifier key is up. OS X has an "official" flag | |
48 // to test whether either left or right versions of a modifier key are held, | |
49 // and "unofficial" flags for the left and right versions independently. This | |
50 // function verifies that |target_key_mask| and |otherKeyMask| (which should be | |
51 // the left and right versions of a modifier) are consistent with with the | |
52 // state of |eitherKeyMask| (which should be the corresponding ""official" | |
53 // flag). If they are consistent, it tests |target_key_mask|; otherwise it tests | |
54 // |either_key_mask|. | |
55 inline bool IsModifierKeyUp(unsigned int flags, | |
56 unsigned int target_key_mask, | |
57 unsigned int other_key_mask, | |
58 unsigned int either_key_mask) { | |
59 bool either_key_down = (flags & either_key_mask) != 0; | |
60 bool target_key_down = (flags & target_key_mask) != 0; | |
61 bool other_key_down = (flags & other_key_mask) != 0; | |
62 if (either_key_down != (target_key_down || other_key_down)) | |
63 return !either_key_down; | |
64 return !target_key_down; | |
65 } | |
66 | |
67 bool IsKeyUpEvent(NSEvent* event) { | |
68 if ([event type] != NSFlagsChanged) | |
69 return [event type] == NSKeyUp; | |
70 | |
71 // Unofficial bit-masks for left- and right-hand versions of modifier keys. | |
72 // These values were determined empirically. | |
73 const unsigned int kLeftControlKeyMask = 1 << 0; | |
74 const unsigned int kLeftShiftKeyMask = 1 << 1; | |
75 const unsigned int kRightShiftKeyMask = 1 << 2; | |
76 const unsigned int kLeftCommandKeyMask = 1 << 3; | |
77 const unsigned int kRightCommandKeyMask = 1 << 4; | |
78 const unsigned int kLeftAlternateKeyMask = 1 << 5; | |
79 const unsigned int kRightAlternateKeyMask = 1 << 6; | |
80 const unsigned int kRightControlKeyMask = 1 << 13; | |
81 | |
82 switch ([event keyCode]) { | |
83 case 54: // Right Command | |
84 return IsModifierKeyUp([event modifierFlags], kRightCommandKeyMask, | |
85 kLeftCommandKeyMask, NSCommandKeyMask); | |
86 case 55: // Left Command | |
87 return IsModifierKeyUp([event modifierFlags], kLeftCommandKeyMask, | |
88 kRightCommandKeyMask, NSCommandKeyMask); | |
89 | |
90 case 57: // Capslock | |
91 return ([event modifierFlags] & NSAlphaShiftKeyMask) == 0; | |
92 | |
93 case 56: // Left Shift | |
94 return IsModifierKeyUp([event modifierFlags], kLeftShiftKeyMask, | |
95 kRightShiftKeyMask, NSShiftKeyMask); | |
96 case 60: // Right Shift | |
97 return IsModifierKeyUp([event modifierFlags], kRightShiftKeyMask, | |
98 kLeftShiftKeyMask, NSShiftKeyMask); | |
99 | |
100 case 58: // Left Alt | |
101 return IsModifierKeyUp([event modifierFlags], kLeftAlternateKeyMask, | |
102 kRightAlternateKeyMask, NSAlternateKeyMask); | |
103 case 61: // Right Alt | |
104 return IsModifierKeyUp([event modifierFlags], kRightAlternateKeyMask, | |
105 kLeftAlternateKeyMask, NSAlternateKeyMask); | |
106 | |
107 case 59: // Left Ctrl | |
108 return IsModifierKeyUp([event modifierFlags], kLeftControlKeyMask, | |
109 kRightControlKeyMask, NSControlKeyMask); | |
110 case 62: // Right Ctrl | |
111 return IsModifierKeyUp([event modifierFlags], kRightControlKeyMask, | |
112 kLeftControlKeyMask, NSControlKeyMask); | |
113 | |
114 case 63: // Function | |
115 return ([event modifierFlags] & NSFunctionKeyMask) == 0; | |
116 } | |
117 return false; | |
118 } | |
119 | |
120 inline NSString* TextFromEvent(NSEvent* event) { | |
121 if ([event type] == NSFlagsChanged) | |
122 return @""; | |
123 return [event characters]; | |
124 } | |
125 | |
126 inline NSString* UnmodifiedTextFromEvent(NSEvent* event) { | |
127 if ([event type] == NSFlagsChanged) | |
128 return @""; | |
129 return [event charactersIgnoringModifiers]; | |
130 } | |
131 | |
132 NSString* KeyIdentifierForKeyEvent(NSEvent* event) { | |
133 if ([event type] == NSFlagsChanged) { | |
134 switch ([event keyCode]) { | |
135 case 54: // Right Command | |
136 case 55: // Left Command | |
137 return @"Meta"; | |
138 | |
139 case 57: // Capslock | |
140 return @"CapsLock"; | |
141 | |
142 case 56: // Left Shift | |
143 case 60: // Right Shift | |
144 return @"Shift"; | |
145 | |
146 case 58: // Left Alt | |
147 case 61: // Right Alt | |
148 return @"Alt"; | |
149 | |
150 case 59: // Left Ctrl | |
151 case 62: // Right Ctrl | |
152 return @"Control"; | |
153 | |
154 // Begin non-Apple addition/modification | |
155 // -------------------------------------- | |
156 case 63: // Function | |
157 return @"Function"; | |
158 | |
159 default: // Unknown, but this may be a strange/new keyboard. | |
160 return @"Unidentified"; | |
161 // End non-Apple addition/modification | |
162 // ---------------------------------------- | |
163 } | |
164 } | |
165 | |
166 NSString* s = [event charactersIgnoringModifiers]; | |
167 if ([s length] != 1) | |
168 return @"Unidentified"; | |
169 | |
170 unichar c = [s characterAtIndex:0]; | |
171 switch (c) { | |
172 // Each identifier listed in the DOM spec is listed here. | |
173 // Many are simply commented out since they do not appear on standard | |
174 // Macintosh keyboards | |
175 // or are on a key that doesn't have a corresponding character. | |
176 | |
177 // "Accept" | |
178 // "AllCandidates" | |
179 | |
180 // "Alt" | |
181 case NSMenuFunctionKey: | |
182 return @"Alt"; | |
183 | |
184 // "Apps" | |
185 // "BrowserBack" | |
186 // "BrowserForward" | |
187 // "BrowserHome" | |
188 // "BrowserRefresh" | |
189 // "BrowserSearch" | |
190 // "BrowserStop" | |
191 // "CapsLock" | |
192 | |
193 // "Clear" | |
194 case NSClearLineFunctionKey: | |
195 return @"Clear"; | |
196 | |
197 // "CodeInput" | |
198 // "Compose" | |
199 // "Control" | |
200 // "Crsel" | |
201 // "Convert" | |
202 // "Copy" | |
203 // "Cut" | |
204 | |
205 // "Down" | |
206 case NSDownArrowFunctionKey: | |
207 return @"Down"; | |
208 // "End" | |
209 case NSEndFunctionKey: | |
210 return @"End"; | |
211 // "Enter" | |
212 case 0x3: | |
213 case 0xA: | |
214 case 0xD: // Macintosh calls the one on the main keyboard Return, but | |
215 // Windows calls it Enter, so we'll do the same for the DOM | |
216 return @"Enter"; | |
217 | |
218 // "EraseEof" | |
219 | |
220 // "Execute" | |
221 case NSExecuteFunctionKey: | |
222 return @"Execute"; | |
223 | |
224 // "Exsel" | |
225 | |
226 // "F1" | |
227 case NSF1FunctionKey: | |
228 return @"F1"; | |
229 // "F2" | |
230 case NSF2FunctionKey: | |
231 return @"F2"; | |
232 // "F3" | |
233 case NSF3FunctionKey: | |
234 return @"F3"; | |
235 // "F4" | |
236 case NSF4FunctionKey: | |
237 return @"F4"; | |
238 // "F5" | |
239 case NSF5FunctionKey: | |
240 return @"F5"; | |
241 // "F6" | |
242 case NSF6FunctionKey: | |
243 return @"F6"; | |
244 // "F7" | |
245 case NSF7FunctionKey: | |
246 return @"F7"; | |
247 // "F8" | |
248 case NSF8FunctionKey: | |
249 return @"F8"; | |
250 // "F9" | |
251 case NSF9FunctionKey: | |
252 return @"F9"; | |
253 // "F10" | |
254 case NSF10FunctionKey: | |
255 return @"F10"; | |
256 // "F11" | |
257 case NSF11FunctionKey: | |
258 return @"F11"; | |
259 // "F12" | |
260 case NSF12FunctionKey: | |
261 return @"F12"; | |
262 // "F13" | |
263 case NSF13FunctionKey: | |
264 return @"F13"; | |
265 // "F14" | |
266 case NSF14FunctionKey: | |
267 return @"F14"; | |
268 // "F15" | |
269 case NSF15FunctionKey: | |
270 return @"F15"; | |
271 // "F16" | |
272 case NSF16FunctionKey: | |
273 return @"F16"; | |
274 // "F17" | |
275 case NSF17FunctionKey: | |
276 return @"F17"; | |
277 // "F18" | |
278 case NSF18FunctionKey: | |
279 return @"F18"; | |
280 // "F19" | |
281 case NSF19FunctionKey: | |
282 return @"F19"; | |
283 // "F20" | |
284 case NSF20FunctionKey: | |
285 return @"F20"; | |
286 // "F21" | |
287 case NSF21FunctionKey: | |
288 return @"F21"; | |
289 // "F22" | |
290 case NSF22FunctionKey: | |
291 return @"F22"; | |
292 // "F23" | |
293 case NSF23FunctionKey: | |
294 return @"F23"; | |
295 // "F24" | |
296 case NSF24FunctionKey: | |
297 return @"F24"; | |
298 | |
299 // "FinalMode" | |
300 | |
301 // "Find" | |
302 case NSFindFunctionKey: | |
303 return @"Find"; | |
304 | |
305 // "FullWidth" | |
306 // "HalfWidth" | |
307 // "HangulMode" | |
308 // "HanjaMode" | |
309 | |
310 // "Help" | |
311 case NSHelpFunctionKey: | |
312 return @"Help"; | |
313 | |
314 // "Hiragana" | |
315 | |
316 // "Home" | |
317 case NSHomeFunctionKey: | |
318 return @"Home"; | |
319 // "Insert" | |
320 case NSInsertFunctionKey: | |
321 return @"Insert"; | |
322 | |
323 // "JapaneseHiragana" | |
324 // "JapaneseKatakana" | |
325 // "JapaneseRomaji" | |
326 // "JunjaMode" | |
327 // "KanaMode" | |
328 // "KanjiMode" | |
329 // "Katakana" | |
330 // "LaunchApplication1" | |
331 // "LaunchApplication2" | |
332 // "LaunchMail" | |
333 | |
334 // "Left" | |
335 case NSLeftArrowFunctionKey: | |
336 return @"Left"; | |
337 | |
338 // "Meta" | |
339 // "MediaNextTrack" | |
340 // "MediaPlayPause" | |
341 // "MediaPreviousTrack" | |
342 // "MediaStop" | |
343 | |
344 // "ModeChange" | |
345 case NSModeSwitchFunctionKey: | |
346 return @"ModeChange"; | |
347 | |
348 // "Nonconvert" | |
349 // "NumLock" | |
350 | |
351 // "PageDown" | |
352 case NSPageDownFunctionKey: | |
353 return @"PageDown"; | |
354 // "PageUp" | |
355 case NSPageUpFunctionKey: | |
356 return @"PageUp"; | |
357 | |
358 // "Paste" | |
359 | |
360 // "Pause" | |
361 case NSPauseFunctionKey: | |
362 return @"Pause"; | |
363 | |
364 // "Play" | |
365 // "PreviousCandidate" | |
366 | |
367 // "PrintScreen" | |
368 case NSPrintScreenFunctionKey: | |
369 return @"PrintScreen"; | |
370 | |
371 // "Process" | |
372 // "Props" | |
373 | |
374 // "Right" | |
375 case NSRightArrowFunctionKey: | |
376 return @"Right"; | |
377 | |
378 // "RomanCharacters" | |
379 | |
380 // "Scroll" | |
381 case NSScrollLockFunctionKey: | |
382 return @"Scroll"; | |
383 // "Select" | |
384 case NSSelectFunctionKey: | |
385 return @"Select"; | |
386 | |
387 // "SelectMedia" | |
388 // "Shift" | |
389 | |
390 // "Stop" | |
391 case NSStopFunctionKey: | |
392 return @"Stop"; | |
393 // "Up" | |
394 case NSUpArrowFunctionKey: | |
395 return @"Up"; | |
396 // "Undo" | |
397 case NSUndoFunctionKey: | |
398 return @"Undo"; | |
399 | |
400 // "VolumeDown" | |
401 // "VolumeMute" | |
402 // "VolumeUp" | |
403 // "Win" | |
404 // "Zoom" | |
405 | |
406 // More function keys, not in the key identifier specification. | |
407 case NSF25FunctionKey: | |
408 return @"F25"; | |
409 case NSF26FunctionKey: | |
410 return @"F26"; | |
411 case NSF27FunctionKey: | |
412 return @"F27"; | |
413 case NSF28FunctionKey: | |
414 return @"F28"; | |
415 case NSF29FunctionKey: | |
416 return @"F29"; | |
417 case NSF30FunctionKey: | |
418 return @"F30"; | |
419 case NSF31FunctionKey: | |
420 return @"F31"; | |
421 case NSF32FunctionKey: | |
422 return @"F32"; | |
423 case NSF33FunctionKey: | |
424 return @"F33"; | |
425 case NSF34FunctionKey: | |
426 return @"F34"; | |
427 case NSF35FunctionKey: | |
428 return @"F35"; | |
429 | |
430 // Turn 0x7F into 0x08, because backspace needs to always be 0x08. | |
431 case 0x7F: | |
432 return @"U+0008"; | |
433 // Standard says that DEL becomes U+007F. | |
434 case NSDeleteFunctionKey: | |
435 return @"U+007F"; | |
436 | |
437 // Always use 0x09 for tab instead of AppKit's backtab character. | |
438 case NSBackTabCharacter: | |
439 return @"U+0009"; | |
440 | |
441 case NSBeginFunctionKey: | |
442 case NSBreakFunctionKey: | |
443 case NSClearDisplayFunctionKey: | |
444 case NSDeleteCharFunctionKey: | |
445 case NSDeleteLineFunctionKey: | |
446 case NSInsertCharFunctionKey: | |
447 case NSInsertLineFunctionKey: | |
448 case NSNextFunctionKey: | |
449 case NSPrevFunctionKey: | |
450 case NSPrintFunctionKey: | |
451 case NSRedoFunctionKey: | |
452 case NSResetFunctionKey: | |
453 case NSSysReqFunctionKey: | |
454 case NSSystemFunctionKey: | |
455 case NSUserFunctionKey: | |
456 // FIXME: We should use something other than the vendor-area Unicode values | |
457 // for the above keys. | |
458 // For now, just fall through to the default. | |
459 default: | |
460 return [NSString stringWithFormat:@"U+%04X", base::ToUpperASCII(c)]; | |
461 } | |
462 } | |
463 | |
464 // End Apple code. | |
465 // ---------------------------------------------------------------------------- | |
466 | |
467 int ModifiersFromEvent(NSEvent* event) { | |
468 int modifiers = 0; | |
469 | |
470 if ([event modifierFlags] & NSControlKeyMask) | |
471 modifiers |= blink::WebInputEvent::ControlKey; | |
472 if ([event modifierFlags] & NSShiftKeyMask) | |
473 modifiers |= blink::WebInputEvent::ShiftKey; | |
474 if ([event modifierFlags] & NSAlternateKeyMask) | |
475 modifiers |= blink::WebInputEvent::AltKey; | |
476 if ([event modifierFlags] & NSCommandKeyMask) | |
477 modifiers |= blink::WebInputEvent::MetaKey; | |
478 if ([event modifierFlags] & NSAlphaShiftKeyMask) | |
479 modifiers |= blink::WebInputEvent::CapsLockOn; | |
480 | |
481 // The return value of 1 << 0 corresponds to the left mouse button, | |
482 // 1 << 1 corresponds to the right mouse button, | |
483 // 1 << n, n >= 2 correspond to other mouse buttons. | |
484 NSUInteger pressed_buttons = [NSEvent pressedMouseButtons]; | |
485 | |
486 if (pressed_buttons & (1 << 0)) | |
487 modifiers |= blink::WebInputEvent::LeftButtonDown; | |
488 if (pressed_buttons & (1 << 1)) | |
489 modifiers |= blink::WebInputEvent::RightButtonDown; | |
490 if (pressed_buttons & (1 << 2)) | |
491 modifiers |= blink::WebInputEvent::MiddleButtonDown; | |
492 | |
493 return modifiers; | |
494 } | |
495 | |
496 void SetWebEventLocationFromEventInView(blink::WebMouseEvent* result, | |
497 NSEvent* event, | |
498 NSView* view) { | |
499 NSPoint window_local = [event locationInWindow]; | |
500 | |
501 NSPoint screen_local = [[view window] convertBaseToScreen:window_local]; | |
502 result->globalX = screen_local.x; | |
503 // Flip y. | |
504 NSScreen* primary_screen = ([[NSScreen screens] count] > 0) | |
505 ? [[NSScreen screens] objectAtIndex:0] | |
506 : nil; | |
507 if (primary_screen) | |
508 result->globalY = [primary_screen frame].size.height - screen_local.y; | |
509 else | |
510 result->globalY = screen_local.y; | |
511 | |
512 NSPoint content_local = [view convertPoint:window_local fromView:nil]; | |
513 result->x = content_local.x; | |
514 result->y = [view frame].size.height - content_local.y; // Flip y. | |
515 | |
516 result->windowX = result->x; | |
517 result->windowY = result->y; | |
518 | |
519 result->movementX = [event deltaX]; | |
520 result->movementY = [event deltaY]; | |
521 } | |
522 | |
523 bool IsSystemKeyEvent(const blink::WebKeyboardEvent& event) { | |
524 // Windows and Linux set |isSystemKey| if alt is down. Blink looks at this | |
525 // flag to decide if it should handle a key or not. E.g. alt-left/right | |
526 // shouldn't be used by Blink to scroll the current page, because we want | |
527 // to get that key back for it to do history navigation. Hence, the | |
528 // corresponding situation on OS X is to set this for cmd key presses. | |
529 // cmd-b and and cmd-i are system wide key bindings that OS X doesn't | |
530 // handle for us, so the editor handles them. | |
531 return event.modifiers & blink::WebInputEvent::MetaKey && | |
532 event.windowsKeyCode != ui::VKEY_B && | |
533 event.windowsKeyCode != ui::VKEY_I; | |
534 } | |
535 | |
536 blink::WebMouseWheelEvent::Phase PhaseForNSEventPhase( | |
537 NSEventPhase event_phase) { | |
538 uint32_t phase = blink::WebMouseWheelEvent::PhaseNone; | |
539 if (event_phase & NSEventPhaseBegan) | |
540 phase |= blink::WebMouseWheelEvent::PhaseBegan; | |
541 if (event_phase & NSEventPhaseStationary) | |
542 phase |= blink::WebMouseWheelEvent::PhaseStationary; | |
543 if (event_phase & NSEventPhaseChanged) | |
544 phase |= blink::WebMouseWheelEvent::PhaseChanged; | |
545 if (event_phase & NSEventPhaseEnded) | |
546 phase |= blink::WebMouseWheelEvent::PhaseEnded; | |
547 if (event_phase & NSEventPhaseCancelled) | |
548 phase |= blink::WebMouseWheelEvent::PhaseCancelled; | |
549 if (event_phase & NSEventPhaseMayBegin) | |
550 phase |= blink::WebMouseWheelEvent::PhaseMayBegin; | |
551 return static_cast<blink::WebMouseWheelEvent::Phase>(phase); | |
552 } | |
553 | |
554 blink::WebMouseWheelEvent::Phase PhaseForEvent(NSEvent* event) { | |
555 if (![event respondsToSelector:@selector(phase)]) | |
556 return blink::WebMouseWheelEvent::PhaseNone; | |
557 | |
558 NSEventPhase event_phase = [event phase]; | |
559 return PhaseForNSEventPhase(event_phase); | |
560 } | |
561 | |
562 blink::WebMouseWheelEvent::Phase MomentumPhaseForEvent(NSEvent* event) { | |
563 if (![event respondsToSelector:@selector(momentumPhase)]) | |
564 return blink::WebMouseWheelEvent::PhaseNone; | |
565 | |
566 NSEventPhase event_momentum_phase = [event momentumPhase]; | |
567 return PhaseForNSEventPhase(event_momentum_phase); | |
568 } | |
569 | |
570 } // namespace | |
571 | |
572 blink::WebKeyboardEvent WebKeyboardEventBuilder::Build(NSEvent* event) { | |
573 blink::WebKeyboardEvent result; | |
574 | |
575 result.type = IsKeyUpEvent(event) ? blink::WebInputEvent::KeyUp | |
576 : blink::WebInputEvent::RawKeyDown; | |
577 | |
578 result.modifiers = ModifiersFromEvent(event); | |
579 | |
580 if (([event type] != NSFlagsChanged) && [event isARepeat]) | |
581 result.modifiers |= blink::WebInputEvent::IsAutoRepeat; | |
582 | |
583 ui::DomCode dom_code = ui::CodeFromNSEvent(event); | |
584 result.windowsKeyCode = | |
585 ui::LocatedToNonLocatedKeyboardCode(ui::KeyboardCodeFromNSEvent(event)); | |
586 result.modifiers |= DomCodeToWebInputEventModifiers(dom_code); | |
587 result.nativeKeyCode = [event keyCode]; | |
588 result.domCode = static_cast<int>(dom_code); | |
589 NSString* text_str = TextFromEvent(event); | |
590 NSString* unmodified_str = UnmodifiedTextFromEvent(event); | |
591 NSString* identifier_str = KeyIdentifierForKeyEvent(event); | |
592 | |
593 // Begin Apple code, copied from KeyEventMac.mm | |
594 | |
595 // Always use 13 for Enter/Return -- we don't want to use AppKit's | |
596 // different character for Enter. | |
597 if (result.windowsKeyCode == '\r') { | |
598 text_str = @"\r"; | |
599 unmodified_str = @"\r"; | |
600 } | |
601 | |
602 // The adjustments below are only needed in backward compatibility mode, | |
603 // but we cannot tell what mode we are in from here. | |
604 | |
605 // Turn 0x7F into 8, because backspace needs to always be 8. | |
606 if ([text_str isEqualToString:@"\x7F"]) | |
607 text_str = @"\x8"; | |
608 if ([unmodified_str isEqualToString:@"\x7F"]) | |
609 unmodified_str = @"\x8"; | |
610 // Always use 9 for tab -- we don't want to use AppKit's different character | |
611 // for shift-tab. | |
612 if (result.windowsKeyCode == 9) { | |
613 text_str = @"\x9"; | |
614 unmodified_str = @"\x9"; | |
615 } | |
616 | |
617 // End Apple code. | |
618 | |
619 if ([text_str length] < blink::WebKeyboardEvent::textLengthCap && | |
620 [unmodified_str length] < blink::WebKeyboardEvent::textLengthCap) { | |
621 [text_str getCharacters:&result.text[0]]; | |
622 [unmodified_str getCharacters:&result.unmodifiedText[0]]; | |
623 } else | |
624 NOTIMPLEMENTED(); | |
625 | |
626 [identifier_str getCString:&result.keyIdentifier[0] | |
627 maxLength:sizeof(result.keyIdentifier) | |
628 encoding:NSASCIIStringEncoding]; | |
629 | |
630 result.timeStampSeconds = [event timestamp]; | |
631 result.isSystemKey = IsSystemKeyEvent(result); | |
632 | |
633 return result; | |
634 } | |
635 | |
636 // WebMouseEvent -------------------------------------------------------------- | |
637 | |
638 blink::WebMouseEvent WebMouseEventBuilder::Build(NSEvent* event, NSView* view) { | |
639 blink::WebMouseEvent result; | |
640 | |
641 result.clickCount = 0; | |
642 | |
643 switch ([event type]) { | |
644 case NSMouseExited: | |
645 result.type = blink::WebInputEvent::MouseLeave; | |
646 result.button = blink::WebMouseEvent::ButtonNone; | |
647 break; | |
648 case NSLeftMouseDown: | |
649 result.type = blink::WebInputEvent::MouseDown; | |
650 result.clickCount = [event clickCount]; | |
651 result.button = blink::WebMouseEvent::ButtonLeft; | |
652 break; | |
653 case NSOtherMouseDown: | |
654 result.type = blink::WebInputEvent::MouseDown; | |
655 result.clickCount = [event clickCount]; | |
656 result.button = blink::WebMouseEvent::ButtonMiddle; | |
657 break; | |
658 case NSRightMouseDown: | |
659 result.type = blink::WebInputEvent::MouseDown; | |
660 result.clickCount = [event clickCount]; | |
661 result.button = blink::WebMouseEvent::ButtonRight; | |
662 break; | |
663 case NSLeftMouseUp: | |
664 result.type = blink::WebInputEvent::MouseUp; | |
665 result.clickCount = [event clickCount]; | |
666 result.button = blink::WebMouseEvent::ButtonLeft; | |
667 break; | |
668 case NSOtherMouseUp: | |
669 result.type = blink::WebInputEvent::MouseUp; | |
670 result.clickCount = [event clickCount]; | |
671 result.button = blink::WebMouseEvent::ButtonMiddle; | |
672 break; | |
673 case NSRightMouseUp: | |
674 result.type = blink::WebInputEvent::MouseUp; | |
675 result.clickCount = [event clickCount]; | |
676 result.button = blink::WebMouseEvent::ButtonRight; | |
677 break; | |
678 case NSMouseMoved: | |
679 case NSMouseEntered: | |
680 result.type = blink::WebInputEvent::MouseMove; | |
681 break; | |
682 case NSLeftMouseDragged: | |
683 result.type = blink::WebInputEvent::MouseMove; | |
684 result.button = blink::WebMouseEvent::ButtonLeft; | |
685 break; | |
686 case NSOtherMouseDragged: | |
687 result.type = blink::WebInputEvent::MouseMove; | |
688 result.button = blink::WebMouseEvent::ButtonMiddle; | |
689 break; | |
690 case NSRightMouseDragged: | |
691 result.type = blink::WebInputEvent::MouseMove; | |
692 result.button = blink::WebMouseEvent::ButtonRight; | |
693 break; | |
694 default: | |
695 NOTIMPLEMENTED(); | |
696 } | |
697 | |
698 SetWebEventLocationFromEventInView(&result, event, view); | |
699 | |
700 result.modifiers = ModifiersFromEvent(event); | |
701 | |
702 result.timeStampSeconds = [event timestamp]; | |
703 | |
704 return result; | |
705 } | |
706 | |
707 // WebMouseWheelEvent --------------------------------------------------------- | |
708 | |
709 blink::WebMouseWheelEvent WebMouseWheelEventBuilder::Build( | |
710 NSEvent* event, | |
711 NSView* view, | |
712 bool can_rubberband_left, | |
713 bool can_rubberband_right) { | |
714 blink::WebMouseWheelEvent result; | |
715 | |
716 result.type = blink::WebInputEvent::MouseWheel; | |
717 result.button = blink::WebMouseEvent::ButtonNone; | |
718 | |
719 result.modifiers = ModifiersFromEvent(event); | |
720 | |
721 SetWebEventLocationFromEventInView(&result, event, view); | |
722 | |
723 result.canRubberbandLeft = can_rubberband_left; | |
724 result.canRubberbandRight = can_rubberband_right; | |
725 | |
726 // Of Mice and Men | |
727 // --------------- | |
728 // | |
729 // There are three types of scroll data available on a scroll wheel CGEvent. | |
730 // Apple's documentation ([1]) is rather vague in their differences, and not | |
731 // terribly helpful in deciding which to use. This is what's really going on. | |
732 // | |
733 // First, these events behave very differently depending on whether a standard | |
734 // wheel mouse is used (one that scrolls in discrete units) or a | |
735 // trackpad/Mighty Mouse is used (which both provide continuous scrolling). | |
736 // You must check to see which was used for the event by testing the | |
737 // kCGScrollWheelEventIsContinuous field. | |
738 // | |
739 // Second, these events refer to "axes". Axis 1 is the y-axis, and axis 2 is | |
740 // the x-axis. | |
741 // | |
742 // Third, there is a concept of mouse acceleration. Scrolling the same amount | |
743 // of physical distance will give you different results logically depending on | |
744 // whether you scrolled a little at a time or in one continuous motion. Some | |
745 // fields account for this while others do not. | |
746 // | |
747 // Fourth, for trackpads there is a concept of chunkiness. When scrolling | |
748 // continuously, events can be delivered in chunks. That is to say, lots of | |
749 // scroll events with delta 0 will be delivered, and every so often an event | |
750 // with a non-zero delta will be delivered, containing the accumulated deltas | |
751 // from all the intermediate moves. [2] | |
752 // | |
753 // For notchy wheel mice (kCGScrollWheelEventIsContinuous == 0) | |
754 // ------------------------------------------------------------ | |
755 // | |
756 // kCGScrollWheelEventDeltaAxis* | |
757 // This is the rawest of raw events. For each mouse notch you get a value of | |
758 // +1/-1. This does not take acceleration into account and thus is less | |
759 // useful for building UIs. | |
760 // | |
761 // kCGScrollWheelEventPointDeltaAxis* | |
762 // This is smarter. In general, for each mouse notch you get a value of | |
763 // +1/-1, but this _does_ take acceleration into account, so you will get | |
764 // larger values on longer scrolls. This field would be ideal for building | |
765 // UIs except for one nasty bug: when the shift key is pressed, this set of | |
766 // fields fails to move the value into the axis2 field (the other two types | |
767 // of data do). This wouldn't be so bad except for the fact that while the | |
768 // number of axes is used in the creation of a CGScrollWheelEvent, there is | |
769 // no way to get that information out of the event once created. | |
770 // | |
771 // kCGScrollWheelEventFixedPtDeltaAxis* | |
772 // This is a fixed value, and for each mouse notch you get a value of | |
773 // +0.1/-0.1 (but, like above, scaled appropriately for acceleration). This | |
774 // value takes acceleration into account, and in fact is identical to the | |
775 // results you get from -[NSEvent delta*]. (That is, if you linked on Tiger | |
776 // or greater; see [2] for details.) | |
777 // | |
778 // A note about continuous devices | |
779 // ------------------------------- | |
780 // | |
781 // There are two devices that provide continuous scrolling events (trackpads | |
782 // and Mighty Mouses) and they behave rather differently. The Mighty Mouse | |
783 // behaves a lot like a regular mouse. There is no chunking, and the | |
784 // FixedPtDelta values are the PointDelta values multiplied by 0.1. With the | |
785 // trackpad, though, there is chunking. While the FixedPtDelta values are | |
786 // reasonable (they occur about every fifth event but have values five times | |
787 // larger than usual) the Delta values are unreasonable. They don't appear to | |
788 // accumulate properly. | |
789 // | |
790 // For continuous devices (kCGScrollWheelEventIsContinuous != 0) | |
791 // ------------------------------------------------------------- | |
792 // | |
793 // kCGScrollWheelEventDeltaAxis* | |
794 // This provides values with no acceleration. With a trackpad, these values | |
795 // are chunked but each non-zero value does not appear to be cumulative. | |
796 // This seems to be a bug. | |
797 // | |
798 // kCGScrollWheelEventPointDeltaAxis* | |
799 // This provides values with acceleration. With a trackpad, these values are | |
800 // not chunked and are highly accurate. | |
801 // | |
802 // kCGScrollWheelEventFixedPtDeltaAxis* | |
803 // This provides values with acceleration. With a trackpad, these values are | |
804 // chunked but unlike Delta events are properly cumulative. | |
805 // | |
806 // Summary | |
807 // ------- | |
808 // | |
809 // In general the best approach to take is: determine if the event is | |
810 // continuous. If it is not, then use the FixedPtDelta events (or just stick | |
811 // with Cocoa events). They provide both acceleration and proper horizontal | |
812 // scrolling. If the event is continuous, then doing pixel scrolling with the | |
813 // PointDelta is the way to go. In general, avoid the Delta events. They're | |
814 // the oldest (dating back to 10.4, before CGEvents were public) but they lack | |
815 // acceleration and precision, making them useful only in specific edge cases. | |
816 // | |
817 // References | |
818 // ---------- | |
819 // | |
820 // [1] | |
821 // <http://developer.apple.com/documentation/Carbon/Reference/QuartzEventServi
cesRef/Reference/reference.html> | |
822 // [2] <http://developer.apple.com/releasenotes/Cocoa/AppKitOlderNotes.html> | |
823 // Scroll to the section headed "NSScrollWheel events". | |
824 // | |
825 // P.S. The "smooth scrolling" option in the system preferences is utterly | |
826 // unrelated to any of this. | |
827 | |
828 CGEventRef cg_event = [event CGEvent]; | |
829 DCHECK(cg_event); | |
830 | |
831 // Wheel ticks are supposed to be raw, unaccelerated values, one per physical | |
832 // mouse wheel notch. The delta event is perfect for this (being a good | |
833 // "specific edge case" as mentioned above). Trackpads, unfortunately, do | |
834 // event chunking, and sending mousewheel events with 0 ticks causes some | |
835 // websites to malfunction. Therefore, for all continuous input devices we use | |
836 // the point delta data instead, since we cannot distinguish trackpad data | |
837 // from data from any other continuous device. | |
838 | |
839 // Conversion between wheel delta amounts and number of pixels to scroll. | |
840 static const double kScrollbarPixelsPerCocoaTick = 40.0; | |
841 | |
842 if (CGEventGetIntegerValueField(cg_event, kCGScrollWheelEventIsContinuous)) { | |
843 result.deltaX = CGEventGetIntegerValueField( | |
844 cg_event, kCGScrollWheelEventPointDeltaAxis2); | |
845 result.deltaY = CGEventGetIntegerValueField( | |
846 cg_event, kCGScrollWheelEventPointDeltaAxis1); | |
847 result.wheelTicksX = result.deltaX / kScrollbarPixelsPerCocoaTick; | |
848 result.wheelTicksY = result.deltaY / kScrollbarPixelsPerCocoaTick; | |
849 result.hasPreciseScrollingDeltas = true; | |
850 } else { | |
851 result.deltaX = [event deltaX] * kScrollbarPixelsPerCocoaTick; | |
852 result.deltaY = [event deltaY] * kScrollbarPixelsPerCocoaTick; | |
853 result.wheelTicksY = | |
854 CGEventGetIntegerValueField(cg_event, kCGScrollWheelEventDeltaAxis1); | |
855 result.wheelTicksX = | |
856 CGEventGetIntegerValueField(cg_event, kCGScrollWheelEventDeltaAxis2); | |
857 } | |
858 | |
859 result.timeStampSeconds = [event timestamp]; | |
860 | |
861 result.phase = PhaseForEvent(event); | |
862 result.momentumPhase = MomentumPhaseForEvent(event); | |
863 | |
864 return result; | |
865 } | |
866 | |
867 blink::WebGestureEvent WebGestureEventBuilder::Build(NSEvent* event, | |
868 NSView* view) { | |
869 blink::WebGestureEvent result; | |
870 | |
871 // Use a temporary WebMouseEvent to get the location. | |
872 blink::WebMouseEvent temp; | |
873 | |
874 SetWebEventLocationFromEventInView(&temp, event, view); | |
875 result.x = temp.x; | |
876 result.y = temp.y; | |
877 result.globalX = temp.globalX; | |
878 result.globalY = temp.globalY; | |
879 | |
880 result.modifiers = ModifiersFromEvent(event); | |
881 result.timeStampSeconds = [event timestamp]; | |
882 | |
883 result.sourceDevice = blink::WebGestureDeviceTouchpad; | |
884 switch ([event type]) { | |
885 case NSEventTypeMagnify: | |
886 result.type = blink::WebInputEvent::GesturePinchUpdate; | |
887 result.data.pinchUpdate.scale = [event magnification] + 1.0; | |
888 break; | |
889 case NSEventTypeSmartMagnify: | |
890 // Map the Cocoa "double-tap with two fingers" zoom gesture to regular | |
891 // GestureDoubleTap, because the effect is similar to single-finger | |
892 // double-tap zoom on mobile platforms. Note that tapCount is set to 1 | |
893 // because the gesture type already encodes that information. | |
894 result.type = blink::WebInputEvent::GestureDoubleTap; | |
895 result.data.tap.tapCount = 1; | |
896 break; | |
897 case NSEventTypeBeginGesture: | |
898 case NSEventTypeEndGesture: | |
899 // The specific type of a gesture is not defined when the gesture begin | |
900 // and end NSEvents come in. Leave them undefined. The caller will need | |
901 // to specify them when the gesture is differentiated. | |
902 result.type = blink::WebInputEvent::Undefined; | |
903 break; | |
904 default: | |
905 NOTIMPLEMENTED(); | |
906 result.type = blink::WebInputEvent::Undefined; | |
907 } | |
908 | |
909 return result; | |
910 } | |
911 | |
912 } // namespace content | |
OLD | NEW |