diff options
author | thakis@chromium.org <thakis@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-11-06 20:34:24 +0000 |
---|---|---|
committer | thakis@chromium.org <thakis@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-11-06 20:34:24 +0000 |
commit | bfabbaae87c820d3dbf18c4e576589859f66ea11 (patch) | |
tree | 4abcd39f2e80c48979ac60aef1880a4c1c907519 /chrome | |
parent | 5ce27c24b9c5d78f2f5b7d414e5e96518fa8f59f (diff) | |
download | chromium_src-bfabbaae87c820d3dbf18c4e576589859f66ea11.zip chromium_src-bfabbaae87c820d3dbf18c4e576589859f66ea11.tar.gz chromium_src-bfabbaae87c820d3dbf18c4e576589859f66ea11.tar.bz2 |
Send keypress() events for ctrl-key and cmd-key in addition to keydown().
The ctrl-key behavior matches what Safari does: We first send a keydown for ctrl-key, and only if the key is not an emacs shortcut, we send a keypress.
The cmd-key behavior is slightly different from Safari: Safari triggers menu items after the keypress command has not been swallowed by javascript. We trigger menu items after keydown. That means that if the user hits cmd-key, we send a keydown and only send a keypress if the shortcut doesn't trigger a menu item. Safari always sends both keydown and keypress.
BUG=25249
TEST=Go to http://unixpapa.com/js/testkey.html . Hit ctrl-a, only a keydown should be generated. Hit ctrl-s, both keydown and keypress should be generated. Hit cmd-a, only a keydown should be generated. Hit cmd-shift-a, both keypress and keydown should be generated. Also, ctrl-1 now makes something a heading in google docs. Cmd-s and Cmd-f should still work in docs.
Review URL: http://codereview.chromium.org/293019
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@31287 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
-rw-r--r-- | chrome/browser/renderer_host/render_widget_host_view_mac.h | 1 | ||||
-rw-r--r-- | chrome/browser/renderer_host/render_widget_host_view_mac.mm | 27 |
2 files changed, 27 insertions, 1 deletions
diff --git a/chrome/browser/renderer_host/render_widget_host_view_mac.h b/chrome/browser/renderer_host/render_widget_host_view_mac.h index 99a9d47..9d6ffbb 100644 --- a/chrome/browser/renderer_host/render_widget_host_view_mac.h +++ b/chrome/browser/renderer_host/render_widget_host_view_mac.h @@ -44,6 +44,7 @@ class RWHVMEditCommandHelper; NSString* toolTip_; BOOL ignoreKeyEvents_; + scoped_nsobject<NSEvent> lastKeyPressedEvent_; } - (void)setCanBeKeyView:(BOOL)can; diff --git a/chrome/browser/renderer_host/render_widget_host_view_mac.mm b/chrome/browser/renderer_host/render_widget_host_view_mac.mm index 2824592..6e14cbaf 100644 --- a/chrome/browser/renderer_host/render_widget_host_view_mac.mm +++ b/chrome/browser/renderer_host/render_widget_host_view_mac.mm @@ -22,10 +22,20 @@ #include "webkit/api/public/WebInputEvent.h" #include "webkit/glue/webmenurunner_mac.h" +using WebKit::WebInputEvent; using WebKit::WebInputEventFactory; using WebKit::WebMouseEvent; using WebKit::WebMouseWheelEvent; +static inline int ToWebKitModifiers(NSUInteger flags) { + int modifiers = 0; + if (flags & NSControlKeyMask) modifiers |= WebInputEvent::ControlKey; + if (flags & NSShiftKeyMask) modifiers |= WebInputEvent::ShiftKey; + if (flags & NSAlternateKeyMask) modifiers |= WebInputEvent::AltKey; + if (flags & NSCommandKeyMask) modifiers |= WebInputEvent::MetaKey; + return modifiers; +} + @interface RenderWidgetHostViewCocoa (Private) + (BOOL)shouldAutohideCursorForEvent:(NSEvent*)event; - (id)initWithRenderWidgetHostViewMac:(RenderWidgetHostViewMac*)r; @@ -652,9 +662,22 @@ void RenderWidgetHostViewMac::SetBackground(const SkBitmap& background) { // closure. Were it not for that single reference, this object would // already be deallocated. In that case, there's no point in calling // -interpretKeyEvents:. - if ([self retainCount] > 1 && [theEvent type] == NSKeyDown) + if ([self retainCount] > 1 && [theEvent type] == NSKeyDown) { [self interpretKeyEvents:[NSArray arrayWithObject:theEvent]]; + // We don't get insertText: calls if ctrl is down, so synthesize a keypress + // event for that case. Note that this makes our behavior deviate from the + // windows and linux versions of chrome (however, see http://crbug.com/13891 + // ), but it makes us similar to how Safari behaves. + if ([theEvent modifierFlags] & (NSControlKeyMask | NSCommandKeyMask) && + lastKeyPressedEvent_.get() != theEvent) { + NativeWebKeyboardEvent event([[theEvent characters] characterAtIndex:0], + ToWebKitModifiers([theEvent modifierFlags]), + base::Time::Now().ToDoubleT()); + renderWidgetHostView_->render_widget_host_->ForwardKeyboardEvent(event); + } + } + // Possibly autohide the cursor. if ([RenderWidgetHostViewCocoa shouldAutohideCursorForEvent:theEvent]) [NSCursor setHiddenUntilMouseMoves:YES]; @@ -1284,6 +1307,7 @@ extern NSString *NSTextInputReplacementRangeAttributeName; // character to onkeypress() event handlers. // TODO(hbono): need to handle more commands? if (selector == @selector(insertNewline:)) { + lastKeyPressedEvent_.reset([[NSApp currentEvent] retain]); NativeWebKeyboardEvent event('\r', renderWidgetHostView_->im_modifiers_, base::Time::Now().ToDoubleT()); renderWidgetHostView_->render_widget_host_->ForwardKeyboardEvent(event); @@ -1304,6 +1328,7 @@ extern NSString *NSTextInputReplacementRangeAttributeName; BOOL isAttributedString = [string isKindOfClass:[NSAttributedString class]]; NSString* im_text = isAttributedString ? [string string] : string; if (!renderWidgetHostView_->im_composing_ && [im_text length] == 1) { + lastKeyPressedEvent_.reset([[NSApp currentEvent] retain]); NativeWebKeyboardEvent event([im_text characterAtIndex:0], renderWidgetHostView_->im_modifiers_, base::Time::Now().ToDoubleT()); |