1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884
|
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/accelerators/accelerator_alias_converter.h"
#include <optional>
#include <vector>
#include "ash/constants/ash_features.h"
#include "ash/constants/ash_pref_names.h"
#include "ash/display/privacy_screen_controller.h"
#include "ash/session/session_controller_impl.h"
#include "ash/shell.h"
#include "ash/system/input_device_settings/input_device_settings_controller_impl.h"
#include "ash/system/input_device_settings/input_device_settings_utils.h"
#include "base/containers/fixed_flat_map.h"
#include "base/containers/fixed_flat_set.h"
#include "base/functional/bind.h"
#include "base/functional/callback_forward.h"
#include "base/notreached.h"
#include "chromeos/constants/devicetype.h"
#include "components/prefs/pref_service.h"
#include "ui/base/accelerators/accelerator.h"
#include "ui/events/ash/keyboard_capability.h"
#include "ui/events/ash/keyboard_layout_util.h"
#include "ui/events/ash/mojom/modifier_key.mojom-shared.h"
#include "ui/events/ash/mojom/six_pack_shortcut_modifier.mojom-shared.h"
#include "ui/events/ash/top_row_action_keys.h"
#include "ui/events/devices/device_data_manager.h"
#include "ui/events/devices/input_device.h"
#include "ui/events/devices/keyboard_device.h"
#include "ui/events/event_constants.h"
#include "ui/events/keycodes/keyboard_codes_posix.h"
namespace ash {
namespace {
using DeviceType = ui::KeyboardCapability::DeviceType;
bool IsChromeOSKeyboard(const ui::KeyboardDevice& keyboard) {
const auto device_type =
Shell::Get()->keyboard_capability()->GetDeviceType(keyboard);
return device_type == DeviceType::kDeviceInternalKeyboard ||
device_type == DeviceType::kDeviceExternalChromeOsKeyboard;
}
// Gets the most recently plugged in external keyboard. If there are no external
// keyboards, return the internal keyboard.
std::optional<ui::KeyboardDevice> GetPriorityExternalKeyboard() {
std::optional<ui::KeyboardDevice> priority_keyboard;
for (const ui::KeyboardDevice& keyboard :
ui::DeviceDataManager::GetInstance()->GetKeyboardDevices()) {
// If the input device settings controlled does not recognize the device as
// a keyboard, skip it.
if (features::IsInputDeviceSettingsSplitEnabled() &&
Shell::Get()->input_device_settings_controller()->GetKeyboardSettings(
keyboard.id) == nullptr) {
continue;
}
const auto device_type =
Shell::Get()->keyboard_capability()->GetDeviceType(keyboard);
switch (device_type) {
case DeviceType::kDeviceUnknown:
case DeviceType::kDeviceInternalKeyboard:
case DeviceType::kDeviceInternalRevenKeyboard:
break;
case DeviceType::kDeviceExternalChromeOsKeyboard:
case DeviceType::kDeviceExternalAppleKeyboard:
case DeviceType::kDeviceExternalGenericKeyboard:
case ui::KeyboardCapability::DeviceType::
kDeviceExternalNullTopRowChromeOsKeyboard:
case DeviceType::kDeviceExternalUnknown:
case DeviceType::kDeviceHotrodRemote:
case DeviceType::kDeviceVirtualCoreKeyboard:
if (!priority_keyboard || keyboard.id > priority_keyboard->id) {
priority_keyboard = keyboard;
}
break;
}
}
return priority_keyboard;
}
std::optional<ui::KeyboardDevice> GetInternalKeyboard() {
for (const ui::KeyboardDevice& keyboard :
ui::DeviceDataManager::GetInstance()->GetKeyboardDevices()) {
// If the input device settings controlled does not recognize the device as
// a keyboard, skip it.
if (features::IsInputDeviceSettingsSplitEnabled() &&
Shell::Get()->input_device_settings_controller()->GetKeyboardSettings(
keyboard.id) == nullptr) {
continue;
}
const auto device_type =
Shell::Get()->keyboard_capability()->GetDeviceType(keyboard);
switch (device_type) {
case DeviceType::kDeviceUnknown:
case DeviceType::kDeviceInternalKeyboard:
case DeviceType::kDeviceInternalRevenKeyboard:
return keyboard;
case DeviceType::kDeviceExternalChromeOsKeyboard:
case DeviceType::kDeviceExternalAppleKeyboard:
case DeviceType::kDeviceExternalGenericKeyboard:
case ui::KeyboardCapability::DeviceType::
kDeviceExternalNullTopRowChromeOsKeyboard:
case DeviceType::kDeviceExternalUnknown:
case DeviceType::kDeviceHotrodRemote:
case DeviceType::kDeviceVirtualCoreKeyboard:
break;
}
}
return std::nullopt;
}
// Identifies media keys which exist only on external keyboards.
bool IsMediaKey(ui::KeyboardCode key_code) {
static constexpr auto kMediaKeyCodes =
base::MakeFixedFlatSet<ui::KeyboardCode>(
{ui::VKEY_MEDIA_PAUSE, ui::VKEY_MEDIA_PLAY,
ui::VKEY_OEM_103, // Media Rewind
ui::VKEY_OEM_104, // Media Fast Forward
ui::VKEY_MEDIA_STOP});
return kMediaKeyCodes.contains(key_code);
}
// Some `TopRowActionKey` values must always be shown if there is an external
// keyboard even if the top row of the keyboard does not technically support
// them. This is because many of these keys are common on external keyboards and
// are unable to be deduced properly.
bool ShouldAlwaysShowWithExternalKeyboard(ui::TopRowActionKey action_key) {
switch (action_key) {
case ui::TopRowActionKey::kNone:
case ui::TopRowActionKey::kUnknown:
case ui::TopRowActionKey::kBack:
case ui::TopRowActionKey::kForward:
case ui::TopRowActionKey::kRefresh:
case ui::TopRowActionKey::kKeyboardBacklightToggle:
case ui::TopRowActionKey::kPrivacyScreenToggle:
case ui::TopRowActionKey::kAllApplications:
case ui::TopRowActionKey::kAccessibility:
case ui::TopRowActionKey::kDoNotDisturb:
return false;
case ui::TopRowActionKey::kDictation:
case ui::TopRowActionKey::kFullscreen:
case ui::TopRowActionKey::kOverview:
case ui::TopRowActionKey::kScreenBrightnessDown:
case ui::TopRowActionKey::kScreenBrightnessUp:
case ui::TopRowActionKey::kKeyboardBacklightDown:
case ui::TopRowActionKey::kKeyboardBacklightUp:
case ui::TopRowActionKey::kMicrophoneMute:
case ui::TopRowActionKey::kVolumeMute:
case ui::TopRowActionKey::kVolumeDown:
case ui::TopRowActionKey::kVolumeUp:
case ui::TopRowActionKey::kNextTrack:
case ui::TopRowActionKey::kPreviousTrack:
case ui::TopRowActionKey::kPlayPause:
case ui::TopRowActionKey::kScreenshot:
case ui::TopRowActionKey::kEmojiPicker:
return true;
}
}
bool MetaFKeyRewritesAreSuppressed(const ui::InputDevice& keyboard) {
if (!features::IsInputDeviceSettingsSplitEnabled()) {
return false;
}
const auto* settings =
Shell::Get()->input_device_settings_controller()->GetKeyboardSettings(
keyboard.id);
return settings && settings->suppress_meta_fkey_rewrites;
}
bool AreTopRowFKeys(const ui::InputDevice& keyboard) {
if (!features::IsInputDeviceSettingsSplitEnabled()) {
PrefService* pref_service =
Shell::Get()->session_controller()->GetActivePrefService();
return pref_service && pref_service->GetBoolean(prefs::kSendFunctionKeys);
}
const auto* settings =
Shell::Get()->input_device_settings_controller()->GetKeyboardSettings(
keyboard.id);
return settings && settings->top_row_are_fkeys;
}
bool ShouldShowExternalTopRowActionKeyAlias(
const ui::KeyboardDevice& keyboard,
ui::TopRowActionKey action_key,
const ui::Accelerator& accelerator) {
const bool should_show_action_key =
Shell::Get()->keyboard_capability()->HasTopRowActionKey(keyboard,
action_key) ||
ShouldAlwaysShowWithExternalKeyboard(action_key);
// If Meta + F-Key rewrites are suppressed for the priority keyboard and
// the accelerator contains the search key, we should not show the
// accelerator.
const bool alias_is_suppressed =
accelerator.IsCmdDown() && MetaFKeyRewritesAreSuppressed(keyboard);
return should_show_action_key && !alias_is_suppressed;
}
ui::mojom::SixPackShortcutModifier GetSixPackShortcutModifier(
ui::KeyboardCode key_code,
std::optional<int> device_id) {
if (!features::IsAltClickAndSixPackCustomizationEnabled() ||
!device_id.has_value()) {
return ui::mojom::SixPackShortcutModifier::kSearch;
}
CHECK(ui::KeyboardCapability::IsSixPackKey(key_code));
const auto* settings =
Shell::Get()->input_device_settings_controller()->GetKeyboardSettings(
device_id.value());
if (!settings || !settings->six_pack_key_remappings) {
return ui::mojom::SixPackShortcutModifier::kSearch;
}
switch (key_code) {
case ui::VKEY_DELETE:
return settings->six_pack_key_remappings->del;
case ui::VKEY_INSERT:
return settings->six_pack_key_remappings->insert;
case ui::VKEY_HOME:
return settings->six_pack_key_remappings->home;
case ui::VKEY_END:
return settings->six_pack_key_remappings->end;
case ui::VKEY_PRIOR:
return settings->six_pack_key_remappings->page_up;
case ui::VKEY_NEXT:
return settings->six_pack_key_remappings->page_down;
default:
NOTREACHED();
}
}
ui::mojom::ExtendedFkeysModifier GetExtendedFkeysModifier(
ui::KeyboardCode key_code,
std::optional<int> device_id) {
if (!features::IsInputDeviceSettingsSplitEnabled() ||
!::features::AreF11AndF12ShortcutsEnabled() || !device_id.has_value() ||
!ui::KeyboardCapability::IsF11OrF12(key_code)) {
return ui::mojom::ExtendedFkeysModifier::kDisabled;
}
auto* controller = Shell::Get()->input_device_settings_controller();
CHECK(controller);
const auto* settings = controller->GetKeyboardSettings(device_id.value());
// Settings are only supported for F11 and F12.
if (!settings) {
return ui::mojom::ExtendedFkeysModifier::kDisabled;
}
if (key_code == ui::VKEY_F11) {
return settings->f11.value();
}
return settings->f12.value();
}
bool HasQuickInsertKeyViaModifierRemapping(const ui::KeyboardDevice& keyboard) {
if (!features::IsInputDeviceSettingsSplitEnabled()) {
return false;
}
auto* settings =
Shell::Get()->input_device_settings_controller()->GetKeyboardSettings(
keyboard.id);
if (!settings) {
return false;
}
bool has_quick_insert_key = false;
for (const auto& [_, to] : settings->modifier_remappings) {
if (to == ui::mojom::ModifierKey::kQuickInsert) {
has_quick_insert_key = true;
break;
}
}
return has_quick_insert_key;
}
} // namespace
std::vector<ui::Accelerator> AcceleratorAliasConverter::CreateAcceleratorAlias(
const ui::Accelerator& accelerator) const {
std::optional<ui::KeyboardDevice> priority_external_keyboard =
GetPriorityExternalKeyboard();
std::optional<ui::KeyboardDevice> internal_keyboard = GetInternalKeyboard();
std::optional<int> device_id = std::nullopt;
if (priority_external_keyboard.has_value()) {
device_id = priority_external_keyboard->id;
} else if (internal_keyboard.has_value()) {
device_id = internal_keyboard->id;
}
// If the external and internal keyboards are either both non-chromeos
// keyboards (ex ChromeOS flex devices) or if they are both ChromeOS keyboards
// (ex ChromeOS external keyboard), do not show aliases for the internal
// keyboard.
if (priority_external_keyboard && internal_keyboard &&
(IsChromeOSKeyboard(*priority_external_keyboard) ==
IsChromeOSKeyboard(*internal_keyboard))) {
internal_keyboard = std::nullopt;
}
// Set is used to get rid of possible duplicate accelerators.
base::flat_set<ui::Accelerator> aliases_set;
// Generate aliases for both the priority external keyboard + the internal
// keyboard for top row action keys.
if (priority_external_keyboard) {
if (const auto alias =
CreateTopRowAliases(*priority_external_keyboard, accelerator);
alias) {
aliases_set.insert(*alias);
// Always add the original accelerator if an external keyboard is present.
aliases_set.insert(accelerator);
}
}
if (internal_keyboard) {
if (const auto alias = CreateTopRowAliases(*internal_keyboard, accelerator);
alias) {
aliases_set.insert(*alias);
}
}
if (!aliases_set.empty()) {
return FilterAliasBySupportedKeys(std::move(aliases_set).extract());
}
// Generate aliases for both the priority external keyboard + the internal
// keyboard for CapsLock key.
if (priority_external_keyboard) {
if (const auto alias =
CreateCapsLockAliases(*priority_external_keyboard, accelerator);
alias) {
aliases_set.insert(*alias);
// Always add the original accelerator if an external keyboard is present.
aliases_set.insert(accelerator);
}
}
if (internal_keyboard) {
if (const auto alias =
CreateCapsLockAliases(*internal_keyboard, accelerator);
alias) {
aliases_set.insert(*alias);
// Always add the original accelerator if an internal keyboard is present.
aliases_set.insert(accelerator);
}
}
if (!aliases_set.empty()) {
return FilterAliasBySupportedKeys(std::move(aliases_set).extract());
}
// Generate aliases for both the priority external keyboard + the internal
// keyboard for f-keys.
if (priority_external_keyboard) {
if (const auto alias =
CreateFunctionKeyAliases(*priority_external_keyboard, accelerator);
alias) {
aliases_set.insert(*alias);
}
}
if (internal_keyboard) {
if (const auto alias =
CreateFunctionKeyAliases(*internal_keyboard, accelerator);
alias) {
aliases_set.insert(*alias);
}
if (ui::KeyboardCapability::IsF11OrF12(accelerator.key_code()) &&
Shell::Get()->keyboard_capability()->IsChromeOSKeyboard(
internal_keyboard->id)) {
if (const auto alias = CreateExtendedFKeysAliases(
*internal_keyboard, accelerator, internal_keyboard->id);
alias) {
aliases_set.insert(*alias);
// If there is an external keyboard connected, we show both versions of
// the shortcut (base accelerator + alias).
if (priority_external_keyboard) {
aliases_set.insert(accelerator);
}
}
}
}
if (!aliases_set.empty()) {
return FilterAliasBySupportedKeys(std::move(aliases_set).extract());
}
// For |six_pack_key|, show both the base
// accelerator and the remapped accelerator if applicable. Otherwise, only
// show base accelerator.
std::vector<ui::Accelerator> aliases =
CreateSixPackAliases(accelerator, device_id);
// Add base accelerator.
aliases.push_back(accelerator);
return FilterAliasBySupportedKeys(aliases);
}
std::optional<ui::Accelerator>
AcceleratorAliasConverter::CreateFunctionKeyAliases(
const ui::KeyboardDevice& keyboard,
const ui::Accelerator& accelerator) const {
// Avoid remapping if [Search] is part of the original accelerator.
if (accelerator.IsCmdDown()) {
return std::nullopt;
}
// Only attempt to alias if the provided accelerator is for an F-Key.
if (accelerator.key_code() < ui::VKEY_F1 ||
accelerator.key_code() > ui::VKEY_F24) {
return std::nullopt;
}
// Attempt to get the corresponding `ui::TopRowActionKey` for the given F-Key.
std::optional<ui::TopRowActionKey> action_key =
Shell::Get()->keyboard_capability()->GetCorrespondingActionKeyForFKey(
keyboard, accelerator.key_code());
if (!action_key) {
return std::nullopt;
}
// Convert the `ui::TopRowActionKey` to the corresponding `ui::KeyboardCode`
std::optional<ui::KeyboardCode> action_vkey =
ui::KeyboardCapability::ConvertToKeyboardCode(*action_key);
if (!action_vkey) {
return std::nullopt;
}
const bool top_row_are_fkeys = AreTopRowFKeys(keyboard);
if (IsSplitModifierKeyboard(keyboard.id)) {
// If its a split modifier Keyboard, the UI should show the Action Key
// glyph. If `top_row_are_fkeys` is false, function key must be added so
// convert the "F-Key" into the action key.
if (top_row_are_fkeys) {
return {ui::Accelerator(*action_vkey, accelerator.modifiers(),
accelerator.key_state())};
} else {
return {ui::Accelerator(*action_vkey,
accelerator.modifiers() | ui::EF_FUNCTION_DOWN,
accelerator.key_state())};
}
} else if (IsChromeOSKeyboard(keyboard)) {
// If `priority_keyboard` is a ChromeOS keyboard, the UI should show the
// corresponding action key, the the F-Key glyph.
if (top_row_are_fkeys) {
return {ui::Accelerator(*action_vkey, accelerator.modifiers(),
accelerator.key_state())};
} else {
return {ui::Accelerator(*action_vkey,
accelerator.modifiers() | ui::EF_COMMAND_DOWN,
accelerator.key_state())};
}
} else {
// On a non-chromeos keyboard, UI should show the F-Key instead.
if (top_row_are_fkeys) {
return {accelerator};
} else {
return {ui::Accelerator(accelerator.key_code(),
accelerator.modifiers() | ui::EF_COMMAND_DOWN,
accelerator.key_state())};
}
}
}
std::optional<ui::Accelerator>
AcceleratorAliasConverter::CreateExtendedFKeysAliases(
const ui::KeyboardDevice& keyboard,
const ui::Accelerator& accelerator,
std::optional<int> device_id) const {
if (!::features::AreF11AndF12ShortcutsEnabled() ||
!ui::KeyboardCapability::IsF11OrF12(accelerator.key_code()) ||
!device_id.has_value()) {
return std::nullopt;
}
const ui::KeyboardCode accel_key_code = accelerator.key_code();
const ui::mojom::ExtendedFkeysModifier fkey_modifier =
GetExtendedFkeysModifier(accel_key_code, device_id);
int modifiers;
const bool top_row_are_fkeys = AreTopRowFKeys(keyboard);
bool avoid_remapping = false;
switch (fkey_modifier) {
case ui::mojom::ExtendedFkeysModifier::kDisabled:
avoid_remapping = true;
break;
case ui::mojom::ExtendedFkeysModifier::kAlt:
avoid_remapping = accelerator.IsAltDown();
modifiers = ui::EF_ALT_DOWN;
break;
case ui::mojom::ExtendedFkeysModifier::kShift:
avoid_remapping = accelerator.IsShiftDown();
modifiers = ui::EF_SHIFT_DOWN;
break;
case ui::mojom::ExtendedFkeysModifier::kCtrlShift:
avoid_remapping = accelerator.IsShiftDown() || accelerator.IsCtrlDown();
modifiers = (ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN);
break;
}
if (avoid_remapping) {
return std::nullopt;
}
ui::KeyboardCode key_code;
const auto* top_row_keys =
Shell::Get()->keyboard_capability()->GetTopRowActionKeys(keyboard);
if (!top_row_keys) {
return std::nullopt;
}
const int top_row_key_index = accel_key_code - ui::KeyboardCode::VKEY_F11;
CHECK(0 <= top_row_key_index && top_row_key_index <= 1);
key_code = Shell::Get()
->keyboard_capability()
->ConvertToKeyboardCode((*top_row_keys)[top_row_key_index])
.value();
modifiers = accelerator.modifiers() |
(top_row_are_fkeys ? modifiers : modifiers | ui::EF_COMMAND_DOWN);
return ui::Accelerator(key_code, modifiers);
}
std::optional<ui::Accelerator> AcceleratorAliasConverter::CreateCapsLockAliases(
const ui::KeyboardDevice& keyboard,
const ui::Accelerator& accelerator) const {
if (accelerator.key_code() != ui::VKEY_CAPITAL) {
return std::nullopt;
}
if (Shell::Get()->keyboard_capability()->HasFunctionKey(keyboard)) {
return {ui::Accelerator(ui::VKEY_QUICK_INSERT, ui::EF_FUNCTION_DOWN)};
}
return accelerator;
}
std::optional<ui::Accelerator> AcceleratorAliasConverter::CreateTopRowAliases(
const ui::KeyboardDevice& keyboard,
const ui::Accelerator& accelerator) const {
// Avoid remapping if [Search] is part of the original accelerator.
if (accelerator.IsCmdDown()) {
return std::nullopt;
}
// If the accelerator is not an action key, do no aliasing.
std::optional<ui::TopRowActionKey> action_key =
ui::KeyboardCapability::ConvertToTopRowActionKey(accelerator.key_code());
if (!action_key) {
return std::nullopt;
}
std::optional<ui::KeyboardCode> function_key =
Shell::Get()->keyboard_capability()->GetCorrespondingFunctionKey(
keyboard, *action_key);
if (!function_key.has_value()) {
return std::nullopt;
}
const bool top_row_are_fkeys = AreTopRowFKeys(keyboard);
if (IsSplitModifierKeyboard(keyboard.id)) {
// If its a split modifier Keyboard, the UI should show the Action Key
// glyph. If `top_row_are_fkeys` is true, function key must be added so
// convert the "F-Key" into the action key.
if (top_row_are_fkeys) {
return {ui::Accelerator(accelerator.key_code(),
accelerator.modifiers() | ui::EF_FUNCTION_DOWN,
accelerator.key_state())};
} else {
// Otherwise if `top_row_are_fkeys` is false, the identity accelerator
// should be returned.
return {accelerator};
}
} else if (IsChromeOSKeyboard(keyboard)) {
// If its a ChromeOS Keyboard, the UI should show the Action Key glyph. If
// `top_row_are_fkeys` is true, Search must be added so convert the "F-Key"
// into the action key.
if (top_row_are_fkeys) {
return {ui::Accelerator(accelerator.key_code(),
accelerator.modifiers() | ui::EF_COMMAND_DOWN,
accelerator.key_state())};
} else {
// Otherwise if `top_row_are_fkeys` is false, the identity accelerator
// should be returned.
return {accelerator};
}
} else {
// If its an external, the F-Key glyph should be shown. If
// `top_row_are_fkeys` is true, search must be added to convert the "F-Key"
// into the action key. Otherwise, the "F-Key" is implicitly the action key.
if (top_row_are_fkeys) {
return {ui::Accelerator(*function_key,
accelerator.modifiers() | ui::EF_COMMAND_DOWN,
accelerator.key_state())};
} else {
return {ui::Accelerator(*function_key, accelerator.modifiers(),
accelerator.key_state())};
}
}
}
std::vector<ui::Accelerator> AcceleratorAliasConverter::CreateSixPackAliases(
const ui::Accelerator& accelerator,
std::optional<int> device_id) const {
if (!::features::IsImprovedKeyboardShortcutsEnabled() ||
!ui::KeyboardCapability::IsSixPackKey(accelerator.key_code())) {
return std::vector<ui::Accelerator>();
}
if (features::IsAltClickAndSixPackCustomizationEnabled() &&
!device_id.has_value()) {
return std::vector<ui::Accelerator>();
}
if (device_id.has_value() && IsSplitModifierKeyboard(device_id.value())) {
const auto iter = ui::kSixPackKeyToFnKeyMap.find(accelerator.key_code());
// [Insert] is technically a six pack key but has no Fn based rewrite. Need
// to make sure we return no aliased accelerator for this case.
if (iter == ui::kSixPackKeyToFnKeyMap.end()) {
return std::vector<ui::Accelerator>();
}
return {ui::Accelerator(iter->second,
accelerator.modifiers() | ui::EF_FUNCTION_DOWN,
accelerator.key_state())};
}
// Edge cases:
// 1. [Shift] + [Delete] should not be remapped to [Shift] + [Search] +
// [Back] (aka, Insert).
// 2. For [Insert], avoid remapping if [Shift] is part of original
// accelerator.
if (accelerator.IsShiftDown() &&
(accelerator.key_code() == ui::KeyboardCode::VKEY_DELETE ||
accelerator.key_code() == ui::KeyboardCode::VKEY_INSERT)) {
return std::vector<ui::Accelerator>();
}
const ui::KeyboardCode accel_key_code = accelerator.key_code();
const ui::mojom::SixPackShortcutModifier six_pack_shortcut_modifier =
GetSixPackShortcutModifier(accel_key_code, device_id);
if (six_pack_shortcut_modifier == ui::mojom::SixPackShortcutModifier::kNone) {
return std::vector<ui::Accelerator>();
}
// For all |six_pack_keys|, avoid remapping if the six-pack remap modifier
// (Search or Alt) is part of the original accelerator.
if (ui::mojom::SixPackShortcutModifier::kSearch ==
six_pack_shortcut_modifier &&
accelerator.IsCmdDown()) {
return std::vector<ui::Accelerator>();
}
if (ui::mojom::SixPackShortcutModifier::kAlt == six_pack_shortcut_modifier &&
accelerator.IsAltDown()) {
return std::vector<ui::Accelerator>();
}
// For the Home and End Alt-based aliases, they additionally include the
// Ctrl modifier, so the original accelerator must not include Ctrl.
if (ui::mojom::SixPackShortcutModifier::kAlt == six_pack_shortcut_modifier &&
accelerator.IsCtrlDown() &&
(accel_key_code == ui::VKEY_HOME || accel_key_code == ui::VKEY_END)) {
return std::vector<ui::Accelerator>();
}
int modifiers;
ui::KeyboardCode key_code;
if (six_pack_shortcut_modifier ==
ui::mojom::SixPackShortcutModifier::kSearch) {
key_code = ui::kSixPackKeyToSearchSystemKeyMap.at(accel_key_code);
modifiers = ui::EF_COMMAND_DOWN;
} else {
key_code = ui::kSixPackKeyToAltSystemKeyMap.at(accel_key_code);
modifiers = (accel_key_code == ui::KeyboardCode::VKEY_END ||
accel_key_code == ui::KeyboardCode::VKEY_HOME)
? (ui::EF_ALT_DOWN | ui::EF_CONTROL_DOWN)
: ui::EF_ALT_DOWN;
}
// For Insert: [modifiers] = [Search] + [Shift] + [original_modifiers].
// For other |six_pack_keys|:
// [modifiers] = [Search/Alt] + [original_modifiers].
int updated_modifiers =
accel_key_code == ui::KeyboardCode::VKEY_INSERT
? accelerator.modifiers() | ui::EF_COMMAND_DOWN | ui::EF_SHIFT_DOWN
: accelerator.modifiers() | modifiers;
return {
ui::Accelerator(key_code, updated_modifiers, accelerator.key_state())};
}
std::vector<ui::Accelerator>
AcceleratorAliasConverter::FilterAliasBySupportedKeys(
const std::vector<ui::Accelerator>& accelerators) const {
const auto* keyboard_capability = Shell::Get()->keyboard_capability();
CHECK(keyboard_capability);
std::vector<ui::Accelerator> filtered_accelerators;
auto priority_keyboard = GetPriorityExternalKeyboard();
auto internal_keyboard = GetInternalKeyboard();
// If the external and internal keyboards are either both non-chromeos
// keyboards (ex ChromeOS flex devices) or if they are both ChromeOS keyboards
// (ex ChromeOS external keyboard), do not show aliases for the internal
// keyboard.
if (priority_keyboard && internal_keyboard &&
(IsChromeOSKeyboard(*priority_keyboard) ==
IsChromeOSKeyboard(*internal_keyboard))) {
internal_keyboard = std::nullopt;
}
for (const auto& accelerator : accelerators) {
// TODO(dpad): Handle privacy screen correctly. This can be simplified once
// drallion devices are properly handled in `KeyboardCapability`.
if (auto action_key = ui::KeyboardCapability::ConvertToTopRowActionKey(
accelerator.key_code());
action_key.has_value() &&
action_key != ui::TopRowActionKey::kPrivacyScreenToggle) {
// Accelerator should only be seen if the priority or internal keyboard
// have the key OR if there is an external keyboard and the `action_key`
// should always been shown when there is an external keyboard.
if (priority_keyboard &&
(ShouldShowExternalTopRowActionKeyAlias(*priority_keyboard,
*action_key, accelerator))) {
filtered_accelerators.push_back(accelerator);
} else if (internal_keyboard && keyboard_capability->HasTopRowActionKey(
*internal_keyboard, *action_key)) {
filtered_accelerators.push_back(accelerator);
}
continue;
}
// The Gemini launch app shortcut should not be disabled in the shortcut app
// and instead functions as a hidden shortcut.
if (accelerator.key_code() == ui::VKEY_F23 &&
accelerator.modifiers() == (ui::EF_COMMAND_DOWN | ui::EF_SHIFT_DOWN)) {
continue;
}
// If the accelerator is for an FKey + Search, make sure it is only shown if
// Meta + F-Key rewrites are allowed.
if (accelerator.key_code() > ui::VKEY_F1 &&
accelerator.key_code() < ui::VKEY_F24 && accelerator.IsCmdDown()) {
if (priority_keyboard &&
!MetaFKeyRewritesAreSuppressed(*priority_keyboard)) {
filtered_accelerators.push_back(accelerator);
}
continue;
}
if (ui::KeyboardCapability::IsSixPackKey(accelerator.key_code())) {
if (ui::KeyboardCapability::HasSixPackOnAnyKeyboard()) {
filtered_accelerators.push_back(accelerator);
}
continue;
}
if (accelerator.key_code() == ui::VKEY_ASSISTANT) {
if (priority_keyboard &&
keyboard_capability->HasAssistantKey(*priority_keyboard)) {
filtered_accelerators.push_back(accelerator);
}
continue;
}
if (accelerator.key_code() == ui::VKEY_MODECHANGE) {
if (keyboard_capability->HasGlobeKeyOnAnyKeyboard()) {
filtered_accelerators.push_back(accelerator);
}
continue;
}
// VKEY_MEDIA_LAUNCH_APP2 is the "Calculator" button on many external
// keyboards.
if (accelerator.key_code() == ui::VKEY_MEDIA_LAUNCH_APP2) {
if (keyboard_capability->HasCalculatorKeyOnAnyKeyboard()) {
filtered_accelerators.push_back(accelerator);
}
continue;
}
if (accelerator.key_code() == ui::VKEY_PRIVACY_SCREEN_TOGGLE) {
if (Shell::Get()->privacy_screen_controller()->IsSupported()) {
for (const ui::KeyboardDevice& keyboard :
ui::DeviceDataManager::GetInstance()->GetKeyboardDevices()) {
if (keyboard_capability->GetDeviceType(keyboard) ==
ui::KeyboardCapability::DeviceType::kDeviceInternalKeyboard) {
filtered_accelerators.push_back(accelerator);
break;
}
}
}
continue;
}
if (accelerator.key_code() == ui::VKEY_BROWSER_SEARCH) {
if (keyboard_capability->HasBrowserSearchKeyOnAnyKeyboard()) {
filtered_accelerators.push_back(accelerator);
}
continue;
}
if (IsMediaKey(accelerator.key_code())) {
if (keyboard_capability->HasMediaKeysOnAnyKeyboard()) {
filtered_accelerators.push_back(accelerator);
}
continue;
}
if (accelerator.key_code() == ui::VKEY_SETTINGS) {
if (keyboard_capability->HasSettingsKeyOnAnyKeyboard()) {
filtered_accelerators.push_back(accelerator);
}
continue;
}
if (accelerator.key_code() == ui::VKEY_HELP) {
if (keyboard_capability->HasHelpKeyOnAnyKeyboard()) {
filtered_accelerators.push_back(accelerator);
}
continue;
}
// If the accelerator is pressing Search + Alt to do capslock, only Alt +
// Search should be shown in the shortcuts app.
if (accelerator.key_code() == ui::VKEY_MENU &&
accelerator.modifiers() == ui::EF_COMMAND_DOWN) {
continue;
}
// Add [CapsLock] to the accelerators list if keyboard has CapsLock.
if (accelerator.key_code() == ui::VKEY_CAPITAL) {
if ((priority_keyboard &&
keyboard_capability->HasCapsLockKey(*priority_keyboard)) ||
(internal_keyboard &&
keyboard_capability->HasCapsLockKey(*internal_keyboard))) {
filtered_accelerators.push_back(accelerator);
}
continue;
}
// Add Alt + Search to the accelerators list.
if (accelerator.key_code() == ui::VKEY_LWIN &&
accelerator.modifiers() == ui::EF_ALT_DOWN) {
if ((internal_keyboard &&
!IsSplitModifierKeyboard(internal_keyboard->id)) ||
priority_keyboard) {
filtered_accelerators.push_back(accelerator);
}
continue;
}
if (accelerator.key_code() == ui::VKEY_QUICK_INSERT) {
if (internal_keyboard && IsSplitModifierKeyboard(internal_keyboard->id)) {
filtered_accelerators.push_back(accelerator);
} else if ((internal_keyboard &&
HasQuickInsertKeyViaModifierRemapping(*internal_keyboard)) ||
(priority_keyboard &&
HasQuickInsertKeyViaModifierRemapping(*priority_keyboard))) {
filtered_accelerators.push_back(accelerator);
}
continue;
}
if (accelerator.key_code() == ui::VKEY_CAMERA_ACCESS_TOGGLE) {
if (keyboard_capability->HasCameraAccessKeyOnAnyKeyboard()) {
filtered_accelerators.push_back(accelerator);
}
continue;
}
// Otherwise, always copy the accelerator.
filtered_accelerators.push_back(accelerator);
}
return filtered_accelerators;
}
} // namespace ash
|