Release KSwitcher v0.6.132 with snippet key recording

This commit is contained in:
agrynco committed 2026-11-23 23:38:58 +02:00
1 parent 777af43c66
commit 0de58834cf
18 files changed
+737 -258

No files matched your search

+283 -93
View File
@@ -1137,13 +1137,14 @@ LRESULT CALLBACK historyProc(HWND w, UINT msg, WPARAM wp, LPARAM lp)
#endif
#ifndef KS_RESIDENT_APP
constexpr int SnippetsPage = 4, SaveSnippets = 318, SnippetMacros = 320;
constexpr int SnippetsPage = 4, SnippetMacros = 320;
struct SnippetView
{
App *app{};
std::vector<Snippet> snippets, saved;
int selection = -1;
HWND pickerOutline{};
bool filling{};
};
struct SettingsView
{
@@ -1159,9 +1160,10 @@ struct SettingsView
bool rawMouseRegistered{};
SnippetView snippets;
};
bool snippetCommand(HWND, SnippetView *, WPARAM, bool embedded);
bool snippetCommand(HWND, SnippetView *, WPARAM);
void snippetWindowPicked(HWND w, WPARAM target, LPARAM phase);
bool confirmSnippetClose(HWND, SnippetView *);
bool snippetDraftChanged(HWND, SnippetView *);
bool mergeSnippetDraft(HWND, SnippetView *, Settings &);
void refreshSnippetDraft(HWND, SnippetView *);
void updateSettingsState(HWND w, SettingsView *v);
void selectSettingsPage(HWND w, SettingsView *v)
@@ -1862,11 +1864,13 @@ bool settingsChanged(HWND w, SettingsView *v)
}
void updateSettingsState(HWND w, SettingsView *v)
{
if (!v || !v->ready || v->filling || v->refreshing) return;
if (!v || !v->ready || v->filling || v->snippets.filling || v->refreshing) return;
v->refreshing = true;
bool changed = settingsChanged(w, v);
EnableWindow(GetDlgItem(w, IDOK), changed || (v->app->settings.autostart && !isLocalBuild()));
bool snippetsChanged = snippetDraftChanged(w, &v->snippets);
EnableWindow(GetDlgItem(w, IDOK), changed || snippetsChanged || (v->app->settings.autostart && !isLocalBuild()));
const wchar_t *text = changed ? L"Unsaved settings — click Save to apply." :
snippetsChanged ? L"Unsaved snippets — click Save to apply." :
v->visitedLan ? L"No unsaved settings. Network changes save automatically in the Computers window." : L"No unsaved settings.";
if (value(w, 261) != text) value(w, 261, text);
v->refreshing = false;
@@ -1882,12 +1886,20 @@ struct SettingsStateRefresh
updateSettingsState(window, reinterpret_cast<SettingsView *>(GetWindowLongPtrW(window, GWLP_USERDATA)));
}
};
void applySettingsDraft(HWND w, SettingsView *v)
{
if (v->app->busy)
throw std::runtime_error("Wait for the current text operation to finish");
auto settings = collectSettings(w, v);
mergeSnippetDraft(w, &v->snippets, settings);
v->app->applySettings(settings);
}
void closeSettings(HWND w, SettingsView *v)
{
if (settingsChanged(w, v))
if (settingsChanged(w, v) || snippetDraftChanged(w, &v->snippets))
{
int choice = MessageBoxW(w,
L"You have unsaved settings changes. Save them before closing?\n\n"
L"You have unsaved changes. Save them before closing?\n\n"
L"Yes: save and close.\nNo: discard changes.\nCancel: keep editing.",
L"KSwitcher — Unsaved settings", MB_YESNOCANCEL | MB_ICONWARNING | MB_DEFBUTTON3);
if (choice != IDYES && choice != IDNO)
@@ -1896,9 +1908,7 @@ void closeSettings(HWND w, SettingsView *v)
{
try
{
if (v->app->busy)
throw std::runtime_error("Wait for the current text operation to finish");
v->app->applySettings(collectSettings(w, v));
applySettingsDraft(w, v);
}
catch (const std::exception &e)
{
@@ -1907,8 +1917,7 @@ void closeSettings(HWND w, SettingsView *v)
}
}
}
if (confirmSnippetClose(w, &v->snippets))
DestroyWindow(w);
DestroyWindow(w);
}
LRESULT CALLBACK settingsProc(HWND w, UINT msg, WPARAM wp, LPARAM lp)
{
@@ -1957,6 +1966,7 @@ LRESULT CALLBACK settingsProc(HWND w, UINT msg, WPARAM wp, LPARAM lp)
v->draft = v->app->settings;
fillSettings(w, v);
refreshSnippetDraft(w, &v->snippets);
updateSettingsState(w, v);
return 0;
case WM_ENABLE:
if (wp) updateSettingsState(w, v);
@@ -1999,8 +2009,8 @@ LRESULT CALLBACK settingsProc(HWND w, UINT msg, WPARAM wp, LPARAM lp)
value(w, 283, L"Copied to clipboard.");
return 0;
}
if (((id >= 301 && id <= SaveSnippets) || id == SnippetMacros) &&
snippetCommand(w, &v->snippets, wp, true))
if (((id >= 301 && id <= 317) || id == SnippetMacros) &&
snippetCommand(w, &v->snippets, wp))
return 0;
if (id == 220 && HIWORD(wp) == LBN_SELCHANGE && !v->filling)
{
@@ -2039,11 +2049,8 @@ LRESULT CALLBACK settingsProc(HWND w, UINT msg, WPARAM wp, LPARAM lp)
}
if (id == IDOK)
{
if (!settingsChanged(w, v) && (!v->app->settings.autostart || isLocalBuild())) return 0;
if (!confirmSnippetClose(w, &v->snippets)) return 0;
if (v->app->busy)
throw std::runtime_error("Wait for the current text operation to finish");
v->app->applySettings(collectSettings(w, v));
if (!IsWindowEnabled(GetDlgItem(w, IDOK))) return 0;
applySettingsDraft(w, v);
DestroyWindow(w);
return 0;
}
@@ -2117,6 +2124,7 @@ void enableSnippetScopeControls(HWND w, bool enabled)
}
void updateSnippetEditor(HWND w, SnippetView *v)
{
v->filling = true;
bool selected = v->selection >= 0 && v->selection < (int)v->snippets.size();
value(w, 302, selected ? v->snippets[v->selection].trigger : L"");
value(w, 303, selected ? v->snippets[v->selection].text : L"");
@@ -2129,9 +2137,10 @@ void updateSnippetEditor(HWND w, SnippetView *v)
value(w, 315, apps);
bool restricted = selected && !v->snippets[v->selection].apps.empty();
CheckDlgButton(w, 314, restricted ? BST_CHECKED : BST_UNCHECKED);
for (int id : {302, 303, 311, 312, 313, 314})
for (int id : {302, 303, 311, 314, SnippetMacros})
EnableWindow(GetDlgItem(w, id), selected);
enableSnippetScopeControls(w, restricted);
v->filling = false;
}
void fillSnippets(HWND w, SnippetView *v)
{
@@ -2173,45 +2182,27 @@ bool sameSnippets(const std::vector<Snippet> &left, const std::vector<Snippet> &
return a.id == b.id && a.trigger == b.trigger && a.text == b.text && a.apps == b.apps;
});
}
void refreshSnippetDraft(HWND w, SnippetView *v)
bool snippetDraftChanged(HWND w, SnippetView *v)
{
collectSnippet(w, v);
if (!sameSnippets(v->snippets, v->saved) || sameSnippets(v->saved, v->app->settings.snippets))
return !sameSnippets(v->snippets, v->saved);
}
void refreshSnippetDraft(HWND w, SnippetView *v)
{
if (snippetDraftChanged(w, v) || sameSnippets(v->saved, v->app->settings.snippets))
return;
v->snippets = v->saved = v->app->settings.snippets;
v->selection = -1;
fillSnippets(w, v);
value(w, 319, L"");
}
void saveSnippetDraft(HWND w, SnippetView *v)
bool mergeSnippetDraft(HWND w, SnippetView *v, Settings &settings)
{
collectSnippet(w, v);
if (sameSnippets(v->snippets, v->saved)) return;
auto settings = v->app->settings;
if (v->app->busy)
throw std::runtime_error("Wait for the current operation");
if (!sameSnippets(settings.snippets, v->saved) && !sameSnippets(settings.snippets, v->snippets))
if (!snippetDraftChanged(w, v)) return false;
const auto &current = v->app->settings.snippets;
if (!sameSnippets(current, v->saved) && !sameSnippets(current, v->snippets))
throw std::runtime_error("Snippets changed elsewhere. Reopen Settings to load the current snippets.");
settings.snippets = v->snippets;
v->app->applySettings(settings);
v->snippets = v->saved = v->app->settings.snippets;
if (v->selection >= (int)v->snippets.size()) v->selection = -1;
if (auto parent = v->app->settingsWindow)
{
auto *view = reinterpret_cast<SettingsView *>(GetWindowLongPtrW(parent, GWLP_USERDATA));
view->draft.snippets = v->saved;
view->draft.snippetTombstones = v->app->settings.snippetTombstones;
updateSettingsState(parent, view);
}
}
bool confirmSnippetClose(HWND w, SnippetView *v)
{
collectSnippet(w, v);
if (sameSnippets(v->snippets, v->saved)) return true;
const int choice = MessageBoxW(w, L"Save your snippet changes before closing?",
L"KSwitcher — Unsaved snippets", MB_YESNOCANCEL | MB_ICONWARNING | MB_DEFBUTTON3);
if (choice == IDYES) saveSnippetDraft(w, v);
return choice == IDYES || choice == IDNO;
return true;
}
std::wstring snippetWindowExecutable(HWND window)
{
@@ -2234,12 +2225,232 @@ void snippetWindowPicked(HWND w, WPARAM target, LPARAM phase)
if (phase == 1 && target)
addSnippetApplication(w, snippetWindowExecutable(reinterpret_cast<HWND>(target)));
}
bool snippetCommand(HWND w, SnippetView *v, WPARAM wp, bool embedded)
constexpr struct SnippetInsertionOption
{
const wchar_t *name, *parameter, *initial;
} snippetInsertions[] = {
{L"Place cursor", L"", L""},
{L"Press Enter", L"", L""},
{L"Pause", L"&Milliseconds (0–10000)", L"50"},
{L"Tap a key", L"Record &key", L""},
{L"Press keys together", L"Record &keys", L""}
};
constexpr int InsertionType = 321, InsertionLabel = 322, InsertionParameter = 323, InsertionPreview = 324;
constexpr int NumericParameterWidth = 62, TextParameterWidth = 210;
constexpr UINT SnippetKeyEvent = WM_APP + 160;
struct SnippetKeyCapture
{
HWND window;
HHOOK hook{};
std::vector<WORD> keys, held;
bool releasing{};
};
thread_local SnippetKeyCapture *activeSnippetCapture{};
SnippetInsertion selectedSnippetInsertion(HWND w)
{
return static_cast<SnippetInsertion>(static_cast<int>(SendDlgItemMessageW(w, InsertionType, CB_GETCURSEL, 0, 0)));
}
bool recordsSnippetKeys(SnippetInsertion type)
{
return type == SnippetInsertion::Key || type == SnippetInsertion::Chord;
}
void stopSnippetCapture(SnippetKeyCapture &capture)
{
if (!capture.hook) return;
activeSnippetCapture = nullptr;
UnhookWindowsHookEx(capture.hook);
capture.hook = nullptr;
}
LRESULT CALLBACK snippetCaptureHook(int code, WPARAM message, LPARAM data)
{
auto *capture = activeSnippetCapture;
if (code == HC_ACTION && capture && GetForegroundWindow() == capture->window &&
GetFocus() == GetDlgItem(capture->window, InsertionParameter))
{
const auto &event = *reinterpret_cast<const KBDLLHOOKSTRUCT *>(data);
PostMessageW(capture->window, SnippetKeyEvent, event.vkCode, message == WM_KEYDOWN || message == WM_SYSKEYDOWN);
return 1;
}
return CallNextHookEx(nullptr, code, message, data);
}
std::wstring snippetInsertionText(HWND w)
{
return formatSnippetInsertion(selectedSnippetInsertion(w), value(w, InsertionParameter));
}
void updateSnippetInsertion(HWND w, bool changedType)
{
if (changedType)
{
const auto type = selectedSnippetInsertion(w);
const auto &option = snippetInsertions[static_cast<size_t>(type)];
const bool hasParameter = *option.parameter != L'\0';
const bool textParameter = recordsSnippetKeys(type);
const auto parameter = GetDlgItem(w, InsertionParameter);
SendMessageW(parameter, EM_SETREADONLY, textParameter, 0);
SendMessageW(parameter, EM_SETCUEBANNER, TRUE,
reinterpret_cast<LPARAM>(textParameter ? L"Click or press Space to record" : L""));
auto &controls = windowLayoutState(w)->controls;
const auto bounds = std::find_if(controls.begin(), controls.end(),
[parameter](const auto &control) { return control.first == parameter; });
if (bounds != controls.end())
{
auto &rect = bounds->second;
rect.right = rect.left + (textParameter ? TextParameterWidth : NumericParameterWidth);
arrangeWindow(w);
}
SendMessageW(parameter, EM_SETLIMITTEXT, textParameter ? 96 : 5, 0);
value(w, InsertionLabel, hasParameter ? option.parameter : L"Parameter");
value(w, InsertionParameter, option.initial);
EnableWindow(parameter, hasParameter);
}
const auto text = snippetInsertionText(w);
const auto *capture = reinterpret_cast<SnippetKeyCapture *>(GetWindowLongPtrW(w, GWLP_USERDATA));
const bool recording = capture && capture->hook;
const wchar_t *hint = recording ? L"Press the keys you want to record." :
recordsSnippetKeys(selectedSnippetInsertion(w)) ? L"" : L"Enter a valid duration (0–10000).";
value(w, InsertionPreview, text.empty() ? hint : text);
EnableWindow(GetDlgItem(w, IDOK), !recording && !text.empty());
}
void showRecordedSnippetKeys(const SnippetKeyCapture &capture)
{
std::wstring text;
for (auto recorded : capture.keys)
{
auto name = snippetKeyName(recorded);
if (name.empty()) { text.clear(); break; }
if (!text.empty()) text += L'+';
text += name;
}
value(capture.window, InsertionParameter, text);
updateSnippetInsertion(capture.window, false);
}
void recordSnippetKey(SnippetKeyCapture &capture, WORD key, bool down)
{
if (!capture.hook) return;
auto &held = capture.held;
auto position = std::find(held.begin(), held.end(), key);
if (down)
{
if (position != held.end()) return;
held.push_back(key);
if (!capture.releasing)
{
capture.keys.push_back(key);
showRecordedSnippetKeys(capture);
}
return;
}
if (position == held.end()) return;
held.erase(position);
capture.releasing = true;
if (!held.empty()) return;
stopSnippetCapture(capture);
updateSnippetInsertion(capture.window, false);
if (snippetInsertionText(capture.window).empty())
value(capture.window, InsertionPreview,
selectedSnippetInsertion(capture.window) == SnippetInsertion::Chord ?
L"Record at least two supported keys held together." : L"Record one supported key.");
}
LRESULT CALLBACK snippetParameterProc(HWND field, UINT msg, WPARAM wp, LPARAM lp, UINT_PTR, DWORD_PTR ref)
{
auto &capture = *reinterpret_cast<SnippetKeyCapture *>(ref);
const bool recordKeys = recordsSnippetKeys(selectedSnippetInsertion(capture.window));
if (recordKeys && ((msg == WM_LBUTTONUP) || (msg == WM_KEYUP && wp == VK_SPACE)))
{
if (msg == WM_LBUTTONUP) DefSubclassProc(field, msg, wp, lp);
if (!capture.hook)
{
capture.held.clear();
capture.keys.clear();
capture.releasing = false;
capture.hook = SetWindowsHookExW(WH_KEYBOARD_LL, snippetCaptureHook, GetModuleHandleW(nullptr), 0);
if (capture.hook)
{
activeSnippetCapture = &capture;
value(capture.window, InsertionParameter, L"");
updateSnippetInsertion(capture.window, false);
}
else value(capture.window, InsertionPreview, L"Could not start key recording. Try again.");
}
return 0;
}
if (msg == WM_KILLFOCUS && capture.hook)
{
stopSnippetCapture(capture);
value(capture.window, InsertionParameter, L"");
updateSnippetInsertion(capture.window, false);
}
return DefSubclassProc(field, msg, wp, lp);
}
LRESULT CALLBACK snippetInsertionProc(HWND w, UINT msg, WPARAM wp, LPARAM lp)
{
HWND parent = GetWindow(w, GW_OWNER);
auto *capture = reinterpret_cast<SnippetKeyCapture *>(GetWindowLongPtrW(w, GWLP_USERDATA));
if (msg == WM_DPICHANGED) { resizeForDpi(w, wp, lp); return 0; }
if (msg == SnippetKeyEvent && capture)
{
recordSnippetKey(*capture, static_cast<WORD>(wp), lp != 0);
return 0;
}
if (msg == WM_COMMAND)
{
const int id = LOWORD(wp);
if (id == InsertionType && HIWORD(wp) == CBN_SELCHANGE) updateSnippetInsertion(w, true);
if (id == InsertionParameter && HIWORD(wp) == EN_CHANGE) updateSnippetInsertion(w, false);
if (id == IDOK && IsWindowEnabled(GetDlgItem(w, IDOK)))
{
const auto text = snippetInsertionText(w);
DestroyWindow(w);
SendDlgItemMessageW(parent, 303, EM_REPLACESEL, TRUE, reinterpret_cast<LPARAM>(text.c_str()));
}
if (id == IDCANCEL) DestroyWindow(w);
return 0;
}
if (msg == WM_CLOSE) { DestroyWindow(w); return 0; }
if (msg == WM_NCDESTROY)
{
if (capture)
{
stopSnippetCapture(*capture);
delete capture;
SetWindowLongPtrW(w, GWLP_USERDATA, 0);
}
EnableWindow(parent, TRUE);
SetForegroundWindow(parent);
SetFocus(GetDlgItem(parent, 303));
disposeFont(w);
}
return DefWindowProcW(w, msg, wp, lp);
}
void showSnippetInsertion(HWND parent, App &app)
{
HWND w = makeWindow(app, L"KSwitcher.SnippetInsertion", L"Insert macro or marker", snippetInsertionProc,
426, 150, 0, false, parent);
label(w, L"&Action", 18, 14, 166);
auto list = control(w, L"COMBOBOX", L"", WS_TABSTOP | CBS_DROPDOWNLIST | WS_VSCROLL,
InsertionType, 18, 38, 166, 174);
for (const auto &option : snippetInsertions)
SendMessageW(list, CB_ADDSTRING, 0, reinterpret_cast<LPARAM>(option.name));
SendMessageW(list, CB_SETCURSEL, 0, 0);
control(w, L"STATIC", L"", 0, InsertionLabel, 198, 14, 210, 22);
auto parameter = edit(w, InsertionParameter, 198, 38, NumericParameterWidth);
auto *capture = new SnippetKeyCapture{w};
SetWindowLongPtrW(w, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(capture));
SetWindowSubclass(parameter, snippetParameterProc, 1, reinterpret_cast<DWORD_PTR>(capture));
label(w, L"Inserts", 18, 75, 59);
control(w, L"STATIC", L"", SS_ENDELLIPSIS, InsertionPreview, 78, 75, 330, 22);
button(w, L"&Insert", IDOK, 200, 105);
button(w, L"&Cancel", IDCANCEL, 308, 105);
updateSnippetInsertion(w, true);
EnableWindow(parent, FALSE);
showRequestedWindow(w);
SetFocus(list);
}
bool snippetCommand(HWND w, SnippetView *v, WPARAM wp)
{
if (v->filling) return true;
const int id = LOWORD(wp);
if (embedded && ((HIWORD(wp) == EN_CHANGE && (id == 302 || id == 303 || id == 315)) ||
id == 310 || id == 311 || id == 314))
value(w, 319, L"");
if (id == 314)
{
bool restricted = IsDlgButtonChecked(w, 314) == BST_CHECKED;
@@ -2278,38 +2489,9 @@ bool snippetCommand(HWND w, SnippetView *v, WPARAM wp, bool embedded)
}
return true;
}
if (id == 312 || id == 313)
{
if (v->selection < 0 || v->selection >= (int)v->snippets.size()) return true;
SendDlgItemMessageW(w, 303, EM_REPLACESEL, TRUE, (LPARAM)(id == 312 ? L"{cursor}" : L"{enter}"));
return true;
}
if (id == SnippetMacros)
{
MessageBoxW(w,
L"Macros between text segments run before the next segment.\n\n"
L"Tap a key: {{shift}}, {{altr}}, {{tab}}, {{F5}}\n"
L"Hold keys together: {{ctrl}}+{{shift}}+{{A}}\n"
L"Press Enter once per {enter} marker. Use {{return}} in a combination.\n"
L"Wait: {pause:50} for 50 milliseconds (0–10000)\n\n"
L"Use an extra pair of braces for literal markers, for example {{enter}} or {{{shift}}}.",
L"Snippet macros", MB_OK | MB_ICONINFORMATION);
return true;
}
if (id == (embedded ? SaveSnippets : IDOK))
{
saveSnippetDraft(w, v);
if (embedded)
{
fillSnippets(w, v);
value(w, 319, L"Snippets saved.");
}
else DestroyWindow(w);
return true;
}
if (!embedded && id == IDCANCEL)
{
DestroyWindow(w);
if (v->selection >= 0 && v->selection < (int)v->snippets.size()) showSnippetInsertion(w, *v->app);
return true;
}
return false;
@@ -2334,15 +2516,8 @@ void createSnippetEditor(HWND w, SnippetView *view, bool embedded)
label(w, L"Process names, comma-separated (for example chrome.exe)", 232, top + 144, 452);
label(w, L"&Replacement text", 232, top + 170, 452);
edit(w, 303, 232, top + 194, 452, textHeight, ES_MULTILINE | ES_WANTRETURN | ES_AUTOVSCROLL | WS_VSCROLL);
button(w, L"Cursor &marker", 312, 232, actions, 108);
button(w, L"&Enter marker", 313, 348, actions, 100);
button(w, L"Macro &help…", SnippetMacros, 456, actions, 96);
if (embedded)
{
button(w, L"Sa&ve snippets", SaveSnippets, 572, actions, 112);
control(w, L"STATIC", L"", 0, 319, 232, actions + 33, 452, 24);
}
else
button(w, L"Insert &macro or marker…", SnippetMacros, 232, actions, 202);
if (!embedded)
{
button(w, L"&Save", IDOK, 474, actions + 44, 100);
button(w, L"&Cancel", IDCANCEL, 584, actions + 44, 100);
@@ -2384,7 +2559,22 @@ LRESULT CALLBACK snippetProc(HWND w, UINT msg, WPARAM wp, LPARAM lp)
drawActionApplicationIcon(w, *reinterpret_cast<const DRAWITEMSTRUCT *>(lp));
return TRUE;
}
if (msg == WM_COMMAND && snippetCommand(w, v, wp, false)) return 0;
if (msg == WM_COMMAND)
{
if (LOWORD(wp) == IDOK)
{
auto settings = v->app->settings;
if (mergeSnippetDraft(w, v, settings))
{
if (v->app->busy) throw std::runtime_error("Wait for the current operation");
v->app->applySettings(settings);
}
DestroyWindow(w);
return 0;
}
if (LOWORD(wp) == IDCANCEL) { DestroyWindow(w); return 0; }
if (snippetCommand(w, v, wp)) return 0;
}
if (msg == WM_CLOSE)
{
DestroyWindow(w);