Skip to content

Commit 63b6bba

Browse files
committed
Fixed SetTerminalInputKeyboardLayout
1 parent 6e3bb0e commit 63b6bba

File tree

5 files changed

+19
-12
lines changed

5 files changed

+19
-12
lines changed

.github/actions/spelling/expect/expect.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ ALIGNRIGHT
2828
allocing
2929
alpc
3030
ALTERNATENAME
31-
alternatename
3231
ALTF
3332
ALTGR
3433
ALTNUMPAD

src/terminal/adapter/ut_adapter/TestHook.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ LayoutGuard& LayoutGuard::operator=(LayoutGuard&& other) noexcept
9494
return *this;
9595
}
9696

97+
LayoutGuard::operator bool() const noexcept
98+
{
99+
return _layout != nullptr;
100+
}
101+
97102
LayoutGuard::operator HKL() const noexcept
98103
{
99104
return _layout;
@@ -103,6 +108,15 @@ LayoutGuard TestHook::SetTerminalInputKeyboardLayout(const wchar_t* klid)
103108
{
104109
THROW_HR_IF_MSG(E_UNEXPECTED, g_keyboardLayout != nullptr, "Nested layout test overrides are not supported");
105110

111+
// Check if the layout is installed. LoadKeyboardLayoutW silently returns the
112+
// current active layout if the requested one is missing.
113+
const auto keyPath = fmt::format(FMT_COMPILE(L"SYSTEM\\CurrentControlSet\\Control\\Keyboard Layouts\\{}"), klid);
114+
wil::unique_hkey key;
115+
if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, keyPath.c_str(), 0, KEY_READ, key.addressof()) != ERROR_SUCCESS)
116+
{
117+
return {};
118+
}
119+
106120
const auto layout = LoadKeyboardLayoutW(klid, KLF_NOTELLSHELL);
107121
THROW_LAST_ERROR_IF_NULL(layout);
108122

src/terminal/adapter/ut_adapter/TestHook.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ namespace TestHook
1313
LayoutGuard(LayoutGuard&& other) noexcept;
1414
LayoutGuard& operator=(LayoutGuard&& other) noexcept;
1515

16+
explicit operator bool() const noexcept;
1617
operator HKL() const noexcept;
1718

1819
private:

src/terminal/adapter/ut_adapter/inputTest.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -628,12 +628,8 @@ void InputTest::TerminalInputNullKeyTests()
628628
{
629629
using namespace std::string_view_literals;
630630

631-
TestHook::LayoutGuard layout;
632-
try
633-
{
634-
layout = TestHook::SetTerminalInputKeyboardLayout(L"00000409"); // US English
635-
}
636-
catch (...)
631+
auto layout = TestHook::SetTerminalInputKeyboardLayout(L"00000409"); // US English
632+
if (!layout)
637633
{
638634
Log::Result(TestResults::Result::Skipped);
639635
return;

src/terminal/adapter/ut_adapter/kittyKeyboardProtocol.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -258,11 +258,8 @@ class KittyKeyboardProtocolTests
258258

259259
TEST_CLASS_SETUP(ClassSetup)
260260
{
261-
try
262-
{
263-
layout = TestHook::SetTerminalInputKeyboardLayout(L"0001040c"); // French (Standard, AZERTY)
264-
}
265-
catch (...)
261+
layout = TestHook::SetTerminalInputKeyboardLayout(L"0001040c"); // French (Standard, AZERTY)
262+
if (!layout)
266263
{
267264
Log::Result(TestResults::Result::Skipped);
268265
}

0 commit comments

Comments
 (0)