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
3 // found in the LICENSE file.
5 #include "chrome/browser/chromeos/events/keyboard_driven_event_rewriter.h"
7 #include "chrome/browser/chromeos/events/event_rewriter.h"
8 #include "chrome/browser/chromeos/system/input_device_settings.h"
9 #include "components/user_manager/user_manager.h"
10 #include "ui/events/event.h"
11 #include "ui/events/event_utils.h"
17 const int kModifierMask
= ui::EF_SHIFT_DOWN
;
19 // Returns true if and only if it is on login screen (i.e. user is not logged
20 // in) and the keyboard driven flag in the OEM manifest is on.
21 bool ShouldStripModifiersForArrowKeysAndEnter() {
22 if (user_manager::UserManager::IsInitialized() &&
23 !user_manager::UserManager::Get()->IsSessionStarted()) {
24 return system::InputDeviceSettings::Get()
25 ->ForceKeyboardDrivenUINavigation();
33 KeyboardDrivenEventRewriter::KeyboardDrivenEventRewriter() {
36 KeyboardDrivenEventRewriter::~KeyboardDrivenEventRewriter() {
39 ui::EventRewriteStatus
KeyboardDrivenEventRewriter::RewriteForTesting(
40 const ui::Event
& event
,
41 scoped_ptr
<ui::Event
>* rewritten_event
) {
42 return Rewrite(event
, rewritten_event
);
45 ui::EventRewriteStatus
KeyboardDrivenEventRewriter::RewriteEvent(
46 const ui::Event
& event
,
47 scoped_ptr
<ui::Event
>* rewritten_event
) {
48 if (!ShouldStripModifiersForArrowKeysAndEnter())
49 return ui::EVENT_REWRITE_CONTINUE
;
51 return Rewrite(event
, rewritten_event
);
54 ui::EventRewriteStatus
KeyboardDrivenEventRewriter::NextDispatchEvent(
55 const ui::Event
& last_event
,
56 scoped_ptr
<ui::Event
>* new_event
) {
58 return ui::EVENT_REWRITE_CONTINUE
;
61 ui::EventRewriteStatus
KeyboardDrivenEventRewriter::Rewrite(
62 const ui::Event
& event
,
63 scoped_ptr
<ui::Event
>* rewritten_event
) {
64 int flags
= event
.flags();
65 if ((flags
& kModifierMask
) != kModifierMask
)
66 return ui::EVENT_REWRITE_CONTINUE
;
68 DCHECK(event
.type() == ui::ET_KEY_PRESSED
||
69 event
.type() == ui::ET_KEY_RELEASED
)
70 << "Unexpected event type " << event
.type();
71 const ui::KeyEvent
& key_event
= static_cast<const ui::KeyEvent
&>(event
);
72 ui::KeyboardCode key_code
= key_event
.key_code();
74 if (key_code
!= ui::VKEY_LEFT
&& key_code
!= ui::VKEY_RIGHT
&&
75 key_code
!= ui::VKEY_UP
&& key_code
!= ui::VKEY_DOWN
&&
76 key_code
!= ui::VKEY_RETURN
&& key_code
!= ui::VKEY_F6
) {
77 return ui::EVENT_REWRITE_CONTINUE
;
80 chromeos::EventRewriter::MutableKeyState state
= {
81 flags
& ~(ui::EF_CONTROL_DOWN
| ui::EF_ALT_DOWN
| ui::EF_SHIFT_DOWN
),
83 key_event
.GetDomKey(),
84 key_event
.GetCharacter(),
85 key_event
.key_code()};
87 chromeos::EventRewriter::BuildRewrittenKeyEvent(key_event
, state
,
89 return ui::EVENT_REWRITE_REWRITTEN
;
92 } // namespace chromeos