From 9bb4b7f0f2f654d6bb33e8d124426971a7a9fe17 Mon Sep 17 00:00:00 2001 From: fxliang Date: Mon, 1 Jul 2024 16:38:34 +0800 Subject: [PATCH] feat: Caps_Lock binding available --- WeaselTSF/Composition.cpp | 1 + WeaselTSF/EditSession.cpp | 3 +++ WeaselTSF/KeyEventSink.cpp | 30 +++++++++++++++++++++++++++++- WeaselTSF/WeaselTSF.h | 1 + 4 files changed, 34 insertions(+), 1 deletion(-) diff --git a/WeaselTSF/Composition.cpp b/WeaselTSF/Composition.cpp index 728a05d18..e94cea0a5 100644 --- a/WeaselTSF/Composition.cpp +++ b/WeaselTSF/Composition.cpp @@ -404,6 +404,7 @@ void WeaselTSF::_AbortComposition(bool clear) { if (_IsComposing()) { _EndComposition(_pEditSessionContext, clear); } + _committed = TRUE; _cand->Destroy(); } diff --git a/WeaselTSF/EditSession.cpp b/WeaselTSF/EditSession.cpp index 99b0fb583..8f4d50367 100644 --- a/WeaselTSF/EditSession.cpp +++ b/WeaselTSF/EditSession.cpp @@ -25,6 +25,9 @@ STDAPI WeaselTSF::DoEditSession(TfEditCookie ec) { } _InsertText(_pEditSessionContext, commit); _EndComposition(_pEditSessionContext, false); + _committed = TRUE; + } else { + _committed = FALSE; } if (_status.composing && !_IsComposing()) { _StartComposition(_pEditSessionContext, diff --git a/WeaselTSF/KeyEventSink.cpp b/WeaselTSF/KeyEventSink.cpp index 0f89f6db5..55dde4f98 100644 --- a/WeaselTSF/KeyEventSink.cpp +++ b/WeaselTSF/KeyEventSink.cpp @@ -4,6 +4,10 @@ #include "KeyEvent.h" #include "CandidateList.h" +static weasel::KeyEvent prevKeyEvent; +static BOOL prevfEaten = FALSE; +static int keyCountToSimulate = 0; + void WeaselTSF::_ProcessKeyEvent(WPARAM wParam, LPARAM lParam, BOOL* pfEaten) { if (!_IsKeyboardOpen() || _IsKeyboardDisabled()) { *pfEaten = FALSE; @@ -28,7 +32,31 @@ void WeaselTSF::_ProcessKeyEvent(WPARAM wParam, LPARAM lParam, BOOL* pfEaten) { else if (ke.keycode == ibus::Down) ke.keycode = ibus::Up; } - *pfEaten = (BOOL)m_client.ProcessKeyEvent(ke); + if (!keyCountToSimulate) + *pfEaten = (BOOL)m_client.ProcessKeyEvent(ke); + + if (ke.keycode == ibus::Caps_Lock) { + if (prevKeyEvent.keycode == ibus::Caps_Lock && prevfEaten == TRUE && + (ke.mask & ibus::RELEASE_MASK) && (!keyCountToSimulate)) { + if ((GetKeyState(VK_CAPITAL) & 0x01)) { + if (_committed || (!*pfEaten && _status.composing)) { + keyCountToSimulate = 2; + INPUT inputs[2]; + inputs[0].type = INPUT_KEYBOARD; + inputs[0].ki = {VK_CAPITAL, 0, 0, 0, 0}; + inputs[1].type = INPUT_KEYBOARD; + inputs[1].ki = {VK_CAPITAL, 0, KEYEVENTF_KEYUP, 0, 0}; + ::SendInput(sizeof(inputs) / sizeof(INPUT), inputs, sizeof(INPUT)); + } + } + *pfEaten = TRUE; + } + if (keyCountToSimulate) + keyCountToSimulate--; + } + + prevfEaten = *pfEaten; + prevKeyEvent = ke; } } diff --git a/WeaselTSF/WeaselTSF.h b/WeaselTSF/WeaselTSF.h index 69c8baad0..12ae30d17 100644 --- a/WeaselTSF/WeaselTSF.h +++ b/WeaselTSF/WeaselTSF.h @@ -231,4 +231,5 @@ class WeaselTSF : public ITfTextInputProcessorEx, // guidatom for the display attibute. TfGuidAtom _gaDisplayAttributeInput; BOOL _async_edit = false; + BOOL _committed = false; };