OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 #include "ui/base/test/ui_controls.h" | 5 #include "ui/base/test/ui_controls.h" |
6 | 6 |
7 #import <Cocoa/Cocoa.h> | 7 #import <Cocoa/Cocoa.h> |
8 #include <mach/mach_time.h> | 8 #include <mach/mach_time.h> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 unichar shifted_character; | 89 unichar shifted_character; |
90 int macKeycode = ui::MacKeyCodeForWindowsKeyCode( | 90 int macKeycode = ui::MacKeyCodeForWindowsKeyCode( |
91 keycode, flags, &shifted_character, &character); | 91 keycode, flags, &shifted_character, &character); |
92 | 92 |
93 if (macKeycode < 0) | 93 if (macKeycode < 0) |
94 return nil; | 94 return nil; |
95 | 95 |
96 // Note that, in line with AppKit's documentation (and tracing "real" events), | 96 // Note that, in line with AppKit's documentation (and tracing "real" events), |
97 // -[NSEvent charactersIngoringModifiers]" are "the characters generated by | 97 // -[NSEvent charactersIngoringModifiers]" are "the characters generated by |
98 // the receiving key event as if no modifier key (except for Shift)". | 98 // the receiving key event as if no modifier key (except for Shift)". |
99 // So |charactersIgnoringModifiers| uses |shifted_characters|, while | 99 // So |charactersIgnoringModifiers| uses |shifted_character|. |
100 // |characters| uses keyboard characters. | |
101 NSString* charactersIgnoringModifiers = | 100 NSString* charactersIgnoringModifiers = |
102 [[[NSString alloc] initWithCharacters:&shifted_character | 101 [[[NSString alloc] initWithCharacters:&shifted_character |
103 length:1] autorelease]; | 102 length:1] autorelease]; |
104 NSString* characters = | 103 NSString* characters; |
105 [[[NSString alloc] initWithCharacters:&character length:1] autorelease]; | 104 // The following were determined empirically on OSX 10.9. |
| 105 if (flags & NSControlKeyMask) { |
| 106 // If Ctrl is pressed, Cocoa always puts an empty string into |characters|. |
| 107 characters = [NSString string]; |
| 108 } else if (flags & NSCommandKeyMask) { |
| 109 // If Cmd is pressed, Cocoa puts a lowercase character into |characters|, |
| 110 // regardless of Shift. If, however, Alt is also pressed then shift *is* |
| 111 // preserved, but re-mappings for Alt are not implemented. Although we still |
| 112 // need to support Alt for things like Alt+Left/Right which don't care. |
| 113 characters = |
| 114 [[[NSString alloc] initWithCharacters:&character length:1] autorelease]; |
| 115 } else { |
| 116 // If just Shift or nothing is pressed, |characters| will match |
| 117 // |charactersIgnoringModifiers|. Alt puts a special character into |
| 118 // |characters| (not |charactersIgnoringModifiers|), but they're not mapped |
| 119 // here. |
| 120 characters = charactersIgnoringModifiers; |
| 121 } |
106 | 122 |
107 NSEventType type = (keyDown ? NSKeyDown : NSKeyUp); | 123 NSEventType type = (keyDown ? NSKeyDown : NSKeyUp); |
108 | 124 |
109 // Modifier keys generate NSFlagsChanged event rather than | 125 // Modifier keys generate NSFlagsChanged event rather than |
110 // NSKeyDown/NSKeyUp events. | 126 // NSKeyDown/NSKeyUp events. |
111 if (keycode == ui::VKEY_CONTROL || keycode == ui::VKEY_SHIFT || | 127 if (keycode == ui::VKEY_CONTROL || keycode == ui::VKEY_SHIFT || |
112 keycode == ui::VKEY_MENU || keycode == ui::VKEY_COMMAND) | 128 keycode == ui::VKEY_MENU || keycode == ui::VKEY_COMMAND) |
113 type = NSFlagsChanged; | 129 type = NSFlagsChanged; |
114 | 130 |
115 // For events other than mouse moved, [event locationInWindow] is | 131 // For events other than mouse moved, [event locationInWindow] is |
116 // UNDEFINED if the event is not NSMouseMoved. Thus, the (0,0) | 132 // UNDEFINED if the event is not NSMouseMoved. Thus, the (0,0) |
117 // location should be fine. | 133 // location should be fine. |
118 NSEvent* event = | 134 NSEvent* event = [NSEvent keyEventWithType:type |
119 [NSEvent keyEventWithType:type | 135 location:NSZeroPoint |
120 location:NSZeroPoint | 136 modifierFlags:flags |
121 modifierFlags:flags | 137 timestamp:TimeIntervalSinceSystemStartup() |
122 timestamp:TimeIntervalSinceSystemStartup() | 138 windowNumber:[window windowNumber] |
123 windowNumber:[window windowNumber] | 139 context:nil |
124 context:nil | 140 characters:characters |
125 characters:characters | 141 charactersIgnoringModifiers:charactersIgnoringModifiers |
126 charactersIgnoringModifiers:charactersIgnoringModifiers | 142 isARepeat:NO |
127 isARepeat:NO | 143 keyCode:(unsigned short)macKeycode]; |
128 keyCode:(unsigned short)macKeycode]; | |
129 | 144 |
130 return event; | 145 return event; |
131 } | 146 } |
132 | 147 |
133 // Creates the proper sequence of autoreleased key events for a key down + up. | 148 // Creates the proper sequence of autoreleased key events for a key down + up. |
134 void SynthesizeKeyEventsSequence(NSWindow* window, | 149 void SynthesizeKeyEventsSequence(NSWindow* window, |
135 ui::KeyboardCode keycode, | 150 ui::KeyboardCode keycode, |
136 bool control, | 151 bool control, |
137 bool shift, | 152 bool shift, |
138 bool alt, | 153 bool alt, |
(...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
411 void RunClosureAfterAllPendingUIEvents(const base::Closure& closure) { | 426 void RunClosureAfterAllPendingUIEvents(const base::Closure& closure) { |
412 base::MessageLoop::current()->PostTask( | 427 base::MessageLoop::current()->PostTask( |
413 FROM_HERE, base::Bind(&EventQueueWatcher, closure)); | 428 FROM_HERE, base::Bind(&EventQueueWatcher, closure)); |
414 } | 429 } |
415 | 430 |
416 bool IsFullKeyboardAccessEnabled() { | 431 bool IsFullKeyboardAccessEnabled() { |
417 return [NSApp isFullKeyboardAccessEnabled]; | 432 return [NSApp isFullKeyboardAccessEnabled]; |
418 } | 433 } |
419 | 434 |
420 } // namespace ui_controls | 435 } // namespace ui_controls |
OLD | NEW |