Skip to content

Commit

Permalink
Make the hotkey control display correct key
Browse files Browse the repository at this point in the history
Credit to @doterik for the idea in #700 (comment)

RApplies to #700
  • Loading branch information
bzoz committed Nov 20, 2019
1 parent 5615987 commit 254474d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
24 changes: 21 additions & 3 deletions src/common/settings_objects.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,19 +120,21 @@ namespace PowerToysSettings {
web::json::value parsed_json = web::json::value::parse(json);
return HotkeyObject(parsed_json);
}
static HotkeyObject from_settings(bool win_pressed, bool ctrl_pressed, bool alt_pressed, bool shift_pressed, UINT vk_code, const std::wstring& key) {
static HotkeyObject from_settings(bool win_pressed, bool ctrl_pressed, bool alt_pressed, bool shift_pressed, UINT vk_code) {
web::json::value json = web::json::value::object();
json.as_object()[L"win"] = web::json::value::boolean(win_pressed);
json.as_object()[L"ctrl"] = web::json::value::boolean(ctrl_pressed);
json.as_object()[L"alt"] = web::json::value::boolean(alt_pressed);
json.as_object()[L"shift"] = web::json::value::boolean(shift_pressed);
json.as_object()[L"code"] = web::json::value::number(vk_code);
json.as_object()[L"key"] = web::json::value::string(key);
json.as_object()[L"key"] = web::json::value::string(key_from_code(vk_code));
return HotkeyObject(json);
}
const web::json::value& get_json() const { return m_json; }

std::wstring get_key() { return m_json[L"key"].as_string(); }
std::wstring get_key() {
return key_from_code(get_code());
}
UINT get_code() { return m_json[L"code"].as_integer(); }
bool win_pressed() { return m_json[L"win"].as_bool(); }
bool ctrl_pressed() { return m_json[L"ctrl"].as_bool(); }
Expand All @@ -148,6 +150,22 @@ namespace PowerToysSettings {
return get_modifiers_repeat() | MOD_NOREPEAT;
}
protected:
static std::wstring key_from_code(UINT key_code) {
auto scan_code = MapVirtualKeyW(key_code, MAPVK_VK_TO_VSC);
auto layout = GetKeyboardLayout(0);
std::array<BYTE, 256> key_states{}; // zero-initialize, no key pressed
std::array<wchar_t, 256> output;
auto output_bytes = ToUnicodeEx(key_code, scan_code, key_states.data(), output.data(), (int)output.size() - 1, 0, layout);
if (output_bytes == 0) {
return L"(unknown)";
} else {
if (output_bytes == -1) {
output_bytes = 1;
}
output[output_bytes] = 0;
return output.data();
}
}
HotkeyObject(web::json::value hotkey_json) : m_json(hotkey_json) {};
web::json::value m_json;
};
Expand Down
2 changes: 1 addition & 1 deletion src/modules/fancyzones/lib/Settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ struct Settings
bool appLastZone_moveWindows = false;
bool use_cursorpos_editor_startupscreen = true;
std::wstring zoneHightlightColor = L"#0078D7";
PowerToysSettings::HotkeyObject editorHotkey = PowerToysSettings::HotkeyObject::from_settings(true, false, false, false, VK_OEM_3, L"~");
PowerToysSettings::HotkeyObject editorHotkey = PowerToysSettings::HotkeyObject::from_settings(true, false, false, false, VK_OEM_3);
std::wstring excludedApps = L"";
std::vector<std::wstring> excludedAppsArray;
};
Expand Down

0 comments on commit 254474d

Please sign in to comment.