Skip to content

Commit

Permalink
changed operation logic
Browse files Browse the repository at this point in the history
  • Loading branch information
setsumi committed May 25, 2022
1 parent 6a67141 commit feb86f3
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 98 deletions.
167 changes: 83 additions & 84 deletions Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,8 @@
TForm1 *Form1;

HINSTANCE hInstance = NULL;
HHOOK hMouseHook, hKbdHook;
HHOOK hMouseHook = NULL, hKbdHook = NULL;
HCURSOR hCurBlank = NULL;
bool CurIsVisible = true; // assuming current state of cursor (set in MyShowCursor())
int Emergency = 0; // force show cursor after some amount of mouse movement
int EmergencyMax = 200;
bool KeybTriggered = false; // flag when keyboard activated re-hide
bool MouseTriggered = false; // flag when mouse button activated hide
bool AppEnabled = true; // application function is active

String GetWindowClassPlus(HWND hwnd)
{
Expand Down Expand Up @@ -88,8 +82,8 @@ String TForm1::ProgramVer()
}*lpTranslate;

// Read first language and code page
if (VerQueryValue(data, L"\\VarFileInfo\\Translation", (LPVOID*)&lpTranslate,
&ilen))
if (VerQueryValue(data, L"\\VarFileInfo\\Translation",
(LPVOID*)&lpTranslate, &ilen))
{
String str;
str.printf(L"\\StringFileInfo\\%04x%04x\\FileVersion",
Expand Down Expand Up @@ -126,7 +120,6 @@ void TForm1::Save()
ini->WriteInteger(L"MAIN", L"Global", radioBtnGlobal->Checked ? 1 : 0);
ini->WriteInteger(L"MAIN", L"Timeout", udTimeout->Position);
ini->WriteInteger(L"MAIN", L"StartToTray", chkStartToTray->Checked ? 1 : 0);
ini->WriteInteger(L"MAIN", L"EmergencyMax", EmergencyMax);
ini->WriteInteger(L"MAIN", L"WinPageIndex", pageControl1->TabIndex);
delete ini;
}
Expand All @@ -149,7 +142,6 @@ void TForm1::Load()
udTimeout->Position = ini->ReadInteger(L"MAIN", L"Timeout", 3);
TimerReset();
chkStartToTray->Checked = ini->ReadInteger(L"MAIN", L"StartToTray", 0) == 1;
EmergencyMax = ini->ReadInteger(L"MAIN", L"EmergencyMax", 200);
pageControl1->TabIndex = ini->ReadInteger(L"MAIN", L"WinPageIndex", 0);
delete ini;
}
Expand Down Expand Up @@ -206,101 +198,109 @@ bool TForm1::TargetProgram()
return rv;
}
// ---------------------------------------------------------------------------
DWORD cursorID[] =
const DWORD cursorID[] =
{OCR_APPSTARTING, OCR_NORMAL, OCR_CROSS, OCR_HAND, OCR_IBEAM, OCR_NO, OCR_SIZEALL,
OCR_SIZENESW, OCR_SIZENS, OCR_SIZENWSE, OCR_SIZEWE, OCR_UP, OCR_WAIT,
32651 /* OCR_HELP */ };

void MyShowCursor(bool show)
void MyShowCursor(bool show, bool force = false)
{
// debug
if (Form1->chkDebug->Checked)
{
MessageBeep(show ? 0 : MB_ICONASTERISK);
}
static bool visible = true;

CurIsVisible = show;
if (show)
if (show != visible || force)
{
SystemParametersInfo(SPI_SETCURSORS, 0, NULL, 0);
}
else
{
for (size_t i = 0; i < (sizeof(cursorID) / sizeof(*cursorID)); i++)
visible = show;

// debug sound
if (Form1->chkDebug->Checked)
{
MessageBeep(show ? 0 : MB_ICONASTERISK);
}

if (show)
{
SetSystemCursor(CopyCursor(hCurBlank), cursorID[i]);
SystemParametersInfo(SPI_SETCURSORS, 0, NULL, 0);
}
else
{
for (size_t i = 0; i < (sizeof(cursorID) / sizeof(*cursorID)); i++)
{
SetSystemCursor(CopyCursor(hCurBlank), cursorID[i]);
}
}
}
}

// ---------------------------------------------------------------------------
LRESULT CALLBACK LLHookMouseProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if (AppEnabled && nCode == HC_ACTION)
{ // allowed to process message
if (wParam == WM_MOUSEMOVE)
{ // show cursor on mouse move
Form1->timerPuff->Enabled = false;
if (!CurIsVisible || KeybTriggered || MouseTriggered)
{
KeybTriggered = MouseTriggered = false;
MyShowCursor(true);
}
else if (++Emergency > EmergencyMax)
{
Emergency = 0;
MyShowCursor(true);
}
Form1->TimerReset();
} // start hide sequence on mouse button (for switching apps by only clicking without moving)
else if (wParam == WM_LBUTTONUP || wParam == WM_RBUTTONUP)
{
MouseTriggered = true;
CurIsVisible = true;
Form1->TimerReset();
}
if (nCode == HC_ACTION) // allowed to process message
{
Form1->TimerReset();
MyShowCursor(true);
}
return CallNextHookEx(hMouseHook, nCode, wParam, lParam);
}

// ---------------------------------------------------------------------------
LRESULT CALLBACK LLHookKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if (AppEnabled && !(nCode < 0))
{ // allowed to process message now
if (nCode == HC_ACTION)
if (nCode == HC_ACTION) // allowed to process message now
{
if (!Form1->radioBtnGlobal->Checked) // ignore keyboard in global mode
{
// show cursor on alt-tabbing to another app
if (!Form1->radioBtnGlobal->Checked && !Form1->TargetProgram())
{
if (!CurIsVisible)
{
MyShowCursor(true);
}
}
// start re-hide sequence on keyboard (in case some operation make cursor reappear)
KeybTriggered = true;
CurIsVisible = true;
Form1->TimerReset();
if (!Form1->timerKb->Enabled) // suppress fast keyboard processind
Form1->timerKb->Enabled = true;
}
}
return CallNextHookEx(hKbdHook, nCode, wParam, lParam);
}

// ---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
void MyUnhook()
{
hInstance = (HINSTANCE)GetWindowLong(Handle, GWL_HINSTANCE);
// release hooks
if (hMouseHook)
{
UnhookWindowsHookEx(hMouseHook);
hMouseHook = NULL;
}
if (hKbdHook)
{
UnhookWindowsHookEx(hKbdHook);
hKbdHook = NULL;
}
}

// ---------------------------------------------------------------------------
void MyHook()
{
MyUnhook();
// set up hooks
hMouseHook = SetWindowsHookEx(WH_MOUSE_LL, (HOOKPROC)LLHookMouseProc,
GetModuleHandle(NULL), NULL);
if (!hMouseHook)
throw Exception(L"INIT: SetWindowsHookEx(WH_MOUSE_LL) failed");
throw Exception(L"SetWindowsHookEx(WH_MOUSE_LL) failed");
hKbdHook = SetWindowsHookEx(WH_KEYBOARD_LL, (HOOKPROC)LLHookKeyboardProc,
GetModuleHandle(NULL), NULL);
if (!hKbdHook)
throw Exception(L"INIT: SetWindowsHookEx(WH_KEYBOARD_LL) failed");
throw Exception(L"SetWindowsHookEx(WH_KEYBOARD_LL) failed");
}

// ---------------------------------------------------------------------------
void __fastcall TForm1::timerKbTimer(TObject *Sender)
{
timerKb->Enabled = false;
TimerReset();
MyShowCursor(!TargetProgram());
}

// ---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
hInstance = (HINSTANCE)GetWindowLong(Handle, GWL_HINSTANCE);
// set up hooks
MyHook();
// load config
Load();
// load blank cursor fromk resource
Expand All @@ -312,12 +312,9 @@ void __fastcall TForm1::FormCreate(TObject *Sender)
// ---------------------------------------------------------------------------
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
// release hooks
UnhookWindowsHookEx(hMouseHook);
UnhookWindowsHookEx(hKbdHook);

MyUnhook();
Save();
MyShowCursor(true);
MyShowCursor(true, true);
DestroyCursor(hCurBlank);
}

Expand All @@ -332,24 +329,21 @@ void __fastcall TForm1::timerPuffTimer(TObject *Sender)
timerPuff->Enabled = false;
if (radioBtnGlobal->Checked || TargetProgram())
{
if (CurIsVisible)
{
MyShowCursor(false);
}
MyShowCursor(false);
}
}

// ---------------------------------------------------------------------------
void __fastcall TForm1::trayIconClick(TObject *Sender)
{
if (Visible)
if (this->Visible)
{
Hide();
this->Hide();
}
else
{
Show();
SetForegroundWindow(Handle);
this->Show();
::SetForegroundWindow(Handle);
}
}

Expand Down Expand Up @@ -401,10 +395,15 @@ void __fastcall TForm1::FormKeyDown(TObject *Sender, WORD &Key, TShiftState Shif
void __fastcall TForm1::chkEnabledClick(TObject *Sender)
{
timerPuff->Enabled = false;
AppEnabled = chkEnabled->Checked;
if (!AppEnabled)
if (!chkEnabled->Checked)
{
MyShowCursor(true);
MyUnhook();
MyShowCursor(true, true);
}
else
{
MyHook();
TimerReset();
}
}

Expand Down
19 changes: 7 additions & 12 deletions Main.dfm
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,6 @@ object Form1: TForm1
TabOrder = 0
object tabsheetTitle: TTabSheet
Caption = 'Window Title'
ExplicitLeft = 0
ExplicitTop = 0
ExplicitWidth = 0
ExplicitHeight = 0
object memoTitle: TMemo
Left = 0
Top = 0
Expand All @@ -168,10 +164,6 @@ object Form1: TForm1
object tabsheetClass: TTabSheet
Caption = 'Window Class'
ImageIndex = 1
ExplicitLeft = 0
ExplicitTop = 0
ExplicitWidth = 0
ExplicitHeight = 0
object memoClass: TMemo
Left = 0
Top = 0
Expand All @@ -186,10 +178,6 @@ object Form1: TForm1
object tabsheetExe: TTabSheet
Caption = 'Executable'
ImageIndex = 2
ExplicitLeft = 0
ExplicitTop = 0
ExplicitWidth = 0
ExplicitHeight = 0
object memoExe: TMemo
Left = 0
Top = 0
Expand Down Expand Up @@ -240,4 +228,11 @@ object Form1: TForm1
Left = 92
Top = 124
end
object timerKb: TTimer
Enabled = False
Interval = 300
OnTimer = timerKbTimer
Left = 182
Top = 123
end
end
2 changes: 2 additions & 0 deletions Main.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class TForm1 : public TForm
TMemo *memoClass;
TTabSheet *tabsheetExe;
TMemo *memoExe;
TTimer *timerKb;

void __fastcall FormCreate(TObject *Sender);
void __fastcall FormDestroy(TObject *Sender);
Expand All @@ -47,6 +48,7 @@ class TForm1 : public TForm
void __fastcall btnExitClick(TObject *Sender);
void __fastcall FormKeyDown(TObject *Sender, WORD &Key, TShiftState Shift);
void __fastcall chkEnabledClick(TObject *Sender);
void __fastcall timerKbTimer(TObject *Sender);

private: // User declarations
void Save();
Expand Down
4 changes: 2 additions & 2 deletions MousePuff.cbproj
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@
<AppEnableRuntimeThemes>true</AppEnableRuntimeThemes>
<AppEnableHighDPI>true</AppEnableHighDPI>
<Defines>NDEBUG;$(Defines)</Defines>
<VerInfo_Keys>CompanyName=;FileDescription=MousePuff by furniture;FileVersion=1.5.0.0;InternalName=;LegalCopyright=;LegalTrademarks=;OriginalFilename=;ProductName=;ProductVersion=1.5.0.0;Comments=;ProgramID=</VerInfo_Keys>
<VerInfo_MinorVer>5</VerInfo_MinorVer>
<VerInfo_Keys>CompanyName=;FileDescription=MousePuff by furniture;FileVersion=1.6.0.0;InternalName=;LegalCopyright=;LegalTrademarks=;OriginalFilename=;ProductName=;ProductVersion=1.6.0.0;Comments=;ProgramID=</VerInfo_Keys>
<VerInfo_MinorVer>6</VerInfo_MinorVer>
<AppExecutionLevel>requireAdministrator</AppExecutionLevel>
</PropertyGroup>
<PropertyGroup Condition="'$(Cfg_2_Win64)'!=''">
Expand Down

0 comments on commit feb86f3

Please sign in to comment.