1 // Copyright (c) 2011 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 #include "content/public/browser/native_web_keyboard_event.h" 6 7 #include "base/logging.h" 8 #include "content/browser/renderer_host/web_input_event_aura.h" 9 #include "ui/events/event.h" 10 11 namespace { 12 13 // We need to copy |os_event| in NativeWebKeyboardEvent because it is 14 // queued in RenderWidgetHost and may be passed and used 15 // RenderViewHostDelegate::HandledKeybardEvent after the original aura 16 // event is destroyed. 17 ui::Event* CopyEvent(ui::Event* event) { 18 return event ? new ui::KeyEvent(*static_cast<ui::KeyEvent*>(event)) : NULL; 19 } 20 21 int EventFlagsToWebInputEventModifiers(int flags) { 22 return 23 (flags & ui::EF_SHIFT_DOWN ? blink::WebInputEvent::ShiftKey : 0) | 24 (flags & ui::EF_CONTROL_DOWN ? blink::WebInputEvent::ControlKey : 0) | 25 (flags & ui::EF_CAPS_LOCK_DOWN ? blink::WebInputEvent::CapsLockOn : 0) | 26 (flags & ui::EF_ALT_DOWN ? blink::WebInputEvent::AltKey : 0); 27 } 28 29 } // namespace 30 31 using blink::WebKeyboardEvent; 32 33 namespace content { 34 35 NativeWebKeyboardEvent::NativeWebKeyboardEvent() 36 : os_event(NULL), 37 skip_in_browser(false), 38 match_edit_command(false) { 39 } 40 41 NativeWebKeyboardEvent::NativeWebKeyboardEvent(gfx::NativeEvent native_event) 42 : WebKeyboardEvent(MakeWebKeyboardEvent( 43 static_cast<ui::KeyEvent*>(native_event))), 44 os_event(CopyEvent(native_event)), 45 skip_in_browser(false), 46 match_edit_command(false) { 47 } 48 49 NativeWebKeyboardEvent::NativeWebKeyboardEvent( 50 const NativeWebKeyboardEvent& other) 51 : WebKeyboardEvent(other), 52 os_event(CopyEvent(other.os_event)), 53 skip_in_browser(other.skip_in_browser), 54 match_edit_command(false) { 55 } 56 57 NativeWebKeyboardEvent::NativeWebKeyboardEvent( 58 ui::EventType key_event_type, 59 bool is_char, 60 wchar_t character, 61 int state, 62 double time_stamp_seconds) 63 : os_event(NULL), 64 skip_in_browser(false), 65 match_edit_command(false) { 66 switch (key_event_type) { 67 case ui::ET_KEY_PRESSED: 68 type = is_char ? blink::WebInputEvent::Char : 69 blink::WebInputEvent::RawKeyDown; 70 break; 71 case ui::ET_KEY_RELEASED: 72 type = blink::WebInputEvent::KeyUp; 73 break; 74 default: 75 NOTREACHED(); 76 } 77 78 modifiers = EventFlagsToWebInputEventModifiers(state); 79 timeStampSeconds = time_stamp_seconds; 80 windowsKeyCode = character; 81 nativeKeyCode = character; 82 text[0] = character; 83 unmodifiedText[0] = character; 84 isSystemKey = 85 (state & ui::EF_ALT_DOWN) != 0 && (state & ui::EF_ALTGR_DOWN) == 0; 86 setKeyIdentifierFromWindowsKeyCode(); 87 } 88 89 NativeWebKeyboardEvent& NativeWebKeyboardEvent::operator=( 90 const NativeWebKeyboardEvent& other) { 91 WebKeyboardEvent::operator=(other); 92 delete os_event; 93 os_event = CopyEvent(other.os_event); 94 skip_in_browser = other.skip_in_browser; 95 match_edit_command = other.match_edit_command; 96 return *this; 97 } 98 99 NativeWebKeyboardEvent::~NativeWebKeyboardEvent() { 100 delete os_event; 101 } 102 103 } // namespace content 104