Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(240)

Side by Side Diff: content/browser/renderer_host/input/web_input_event_builders_mac.mm

Issue 2439953005: Support NSFlagsChanged in ui::EventFromNative. (Closed)
Patch Set: Unify NSFlagsChanged handling between content and ui/events. Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | ui/events/cocoa/cocoa_event_utils.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 /* 5 /*
6 * Copyright (C) 2004, 2006, 2007 Apple Inc. All rights reserved. 6 * Copyright (C) 2004, 2006, 2007 Apple Inc. All rights reserved.
7 * Copyright (C) 2006-2009 Google Inc. 7 * Copyright (C) 2006-2009 Google Inc.
8 * 8 *
9 * Redistribution and use in source and binary forms, with or without 9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions 10 * modification, are permitted provided that the following conditions
(...skipping 30 matching lines...) Expand all
41 #include "ui/base/cocoa/cocoa_base_utils.h" 41 #include "ui/base/cocoa/cocoa_base_utils.h"
42 #include "ui/events/blink/blink_event_util.h" 42 #include "ui/events/blink/blink_event_util.h"
43 #import "ui/events/cocoa/cocoa_event_utils.h" 43 #import "ui/events/cocoa/cocoa_event_utils.h"
44 #include "ui/events/keycodes/keyboard_code_conversion.h" 44 #include "ui/events/keycodes/keyboard_code_conversion.h"
45 #include "ui/events/keycodes/keyboard_code_conversion_mac.h" 45 #include "ui/events/keycodes/keyboard_code_conversion_mac.h"
46 46
47 namespace content { 47 namespace content {
48 48
49 namespace { 49 namespace {
50 50
51 // Return true if the target modifier key is up. OS X has an "official" flag
52 // to test whether either left or right versions of a modifier key are held,
53 // and "unofficial" flags for the left and right versions independently. This
54 // function verifies that |target_key_mask| and |otherKeyMask| (which should be
55 // the left and right versions of a modifier) are consistent with with the
56 // state of |eitherKeyMask| (which should be the corresponding ""official"
57 // flag). If they are consistent, it tests |target_key_mask|; otherwise it tests
58 // |either_key_mask|.
59 inline bool IsModifierKeyUp(unsigned int flags,
60 unsigned int target_key_mask,
61 unsigned int other_key_mask,
62 unsigned int either_key_mask) {
63 bool either_key_down = (flags & either_key_mask) != 0;
64 bool target_key_down = (flags & target_key_mask) != 0;
65 bool other_key_down = (flags & other_key_mask) != 0;
66 if (either_key_down != (target_key_down || other_key_down))
67 return !either_key_down;
68 return !target_key_down;
69 }
70
71 bool IsKeyUpEvent(NSEvent* event) {
72 if ([event type] != NSFlagsChanged)
73 return [event type] == NSKeyUp;
74
75 // Unofficial bit-masks for left- and right-hand versions of modifier keys.
76 // These values were determined empirically.
77 const unsigned int kLeftControlKeyMask = 1 << 0;
78 const unsigned int kLeftShiftKeyMask = 1 << 1;
79 const unsigned int kRightShiftKeyMask = 1 << 2;
80 const unsigned int kLeftCommandKeyMask = 1 << 3;
81 const unsigned int kRightCommandKeyMask = 1 << 4;
82 const unsigned int kLeftAlternateKeyMask = 1 << 5;
83 const unsigned int kRightAlternateKeyMask = 1 << 6;
84 const unsigned int kRightControlKeyMask = 1 << 13;
85
86 switch ([event keyCode]) {
87 case 54: // Right Command
88 return IsModifierKeyUp([event modifierFlags], kRightCommandKeyMask,
89 kLeftCommandKeyMask, NSCommandKeyMask);
90 case 55: // Left Command
91 return IsModifierKeyUp([event modifierFlags], kLeftCommandKeyMask,
92 kRightCommandKeyMask, NSCommandKeyMask);
93
94 case 57: // Capslock
95 return ([event modifierFlags] & NSAlphaShiftKeyMask) == 0;
96
97 case 56: // Left Shift
98 return IsModifierKeyUp([event modifierFlags], kLeftShiftKeyMask,
99 kRightShiftKeyMask, NSShiftKeyMask);
100 case 60: // Right Shift
101 return IsModifierKeyUp([event modifierFlags], kRightShiftKeyMask,
102 kLeftShiftKeyMask, NSShiftKeyMask);
103
104 case 58: // Left Alt
105 return IsModifierKeyUp([event modifierFlags], kLeftAlternateKeyMask,
106 kRightAlternateKeyMask, NSAlternateKeyMask);
107 case 61: // Right Alt
108 return IsModifierKeyUp([event modifierFlags], kRightAlternateKeyMask,
109 kLeftAlternateKeyMask, NSAlternateKeyMask);
110
111 case 59: // Left Ctrl
112 return IsModifierKeyUp([event modifierFlags], kLeftControlKeyMask,
113 kRightControlKeyMask, NSControlKeyMask);
114 case 62: // Right Ctrl
115 return IsModifierKeyUp([event modifierFlags], kRightControlKeyMask,
116 kLeftControlKeyMask, NSControlKeyMask);
117
118 case 63: // Function
119 return ([event modifierFlags] & NSFunctionKeyMask) == 0;
120 }
121 return false;
122 }
123
124 inline NSString* FilterSpecialCharacter(NSString* str) { 51 inline NSString* FilterSpecialCharacter(NSString* str) {
125 if ([str length] != 1) 52 if ([str length] != 1)
126 return str; 53 return str;
127 unichar c = [str characterAtIndex:0]; 54 unichar c = [str characterAtIndex:0];
128 NSString* result = str; 55 NSString* result = str;
129 if (c == 0x7F) { 56 if (c == 0x7F) {
130 // Backspace should be 8 57 // Backspace should be 8
131 result = @"\x8"; 58 result = @"\x8";
132 } else if (c >= 0xF700 && c <= 0xF7FF) { 59 } else if (c >= 0xF700 && c <= 0xF7FF) {
133 // Mac private use characters should be @"\0" (@"" won't work) 60 // Mac private use characters should be @"\0" (@"" won't work)
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after
269 if (key != ui::DomKey::NONE) 196 if (key != ui::DomKey::NONE)
270 return key; 197 return key;
271 return ui::DomKey::UNIDENTIFIED; 198 return ui::DomKey::UNIDENTIFIED;
272 } 199 }
273 200
274 } // namespace 201 } // namespace
275 202
276 blink::WebKeyboardEvent WebKeyboardEventBuilder::Build(NSEvent* event) { 203 blink::WebKeyboardEvent WebKeyboardEventBuilder::Build(NSEvent* event) {
277 blink::WebKeyboardEvent result; 204 blink::WebKeyboardEvent result;
278 205
279 result.type = IsKeyUpEvent(event) ? blink::WebInputEvent::KeyUp 206 result.type = ui::IsKeyUpEvent(event) ? blink::WebInputEvent::KeyUp
280 : blink::WebInputEvent::RawKeyDown; 207 : blink::WebInputEvent::RawKeyDown;
281 208
282 result.modifiers = ModifiersFromEvent(event); 209 result.modifiers = ModifiersFromEvent(event);
283 210
284 if (([event type] != NSFlagsChanged) && [event isARepeat]) 211 if (([event type] != NSFlagsChanged) && [event isARepeat])
285 result.modifiers |= blink::WebInputEvent::IsAutoRepeat; 212 result.modifiers |= blink::WebInputEvent::IsAutoRepeat;
286 213
287 ui::DomCode dom_code = ui::DomCodeFromNSEvent(event); 214 ui::DomCode dom_code = ui::DomCodeFromNSEvent(event);
288 result.windowsKeyCode = 215 result.windowsKeyCode =
289 ui::LocatedToNonLocatedKeyboardCode(ui::KeyboardCodeFromNSEvent(event)); 216 ui::LocatedToNonLocatedKeyboardCode(ui::KeyboardCodeFromNSEvent(event));
290 result.modifiers |= ui::DomCodeToWebInputEventModifiers(dom_code); 217 result.modifiers |= ui::DomCodeToWebInputEventModifiers(dom_code);
(...skipping 324 matching lines...) Expand 10 before | Expand all | Expand 10 after
615 break; 542 break;
616 default: 543 default:
617 NOTIMPLEMENTED(); 544 NOTIMPLEMENTED();
618 result.type = blink::WebInputEvent::Undefined; 545 result.type = blink::WebInputEvent::Undefined;
619 } 546 }
620 547
621 return result; 548 return result;
622 } 549 }
623 550
624 } // namespace content 551 } // namespace content
OLDNEW
« no previous file with comments | « no previous file | ui/events/cocoa/cocoa_event_utils.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698