1 // Copyright (c) 2019 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 "ash/keyboard/keyboard_util.h"
6 
7 #include "ash/public/cpp/keyboard/keyboard_controller.h"
8 #include "ash/shell.h"
9 #include "ash/system/model/system_tray_model.h"
10 #include "ash/system/model/virtual_keyboard_model.h"
11 #include "ash/wm/window_util.h"
12 
13 namespace ash {
14 
15 namespace keyboard_util {
16 
IsArrowKeyCode(const ui::KeyboardCode key_code)17 bool IsArrowKeyCode(const ui::KeyboardCode key_code) {
18   return key_code == ui::VKEY_DOWN || key_code == ui::VKEY_RIGHT ||
19          key_code == ui::VKEY_LEFT || key_code == ui::VKEY_UP;
20 }
21 
CloseKeyboardIfActive()22 bool CloseKeyboardIfActive() {
23   // Close the Chrome VK if it is visible.
24   auto* keyboard_controller = KeyboardController::Get();
25   if (keyboard_controller->IsKeyboardVisible()) {
26     keyboard_controller->HideKeyboard(HideReason::kUser);
27     return true;
28   }
29 
30   // Close the Android VK if it is visible by sending a back event.
31   if (Shell::Get()->system_tray_model()->virtual_keyboard()->visible()) {
32     window_util::SendBackKeyEvent(Shell::GetPrimaryRootWindow());
33     return true;
34   }
35 
36   return false;
37 }
38 
39 }  // namespace keyboard_util
40 
41 }  // namespace ash
42