BSOne.SFC/Tocsg.Module/KeyMon/EXE_KeyMon/KeyMonDLL.pas

168 lines
4.1 KiB
Plaintext

{*******************************************************}
{ }
{ CatchKeyDLL }
{ }
{ Copyright (C) 2022 sunk }
{ }
{*******************************************************}
unit KeyMonDLL;
interface
uses
System.Classes, System.SysUtils, Winapi.Windows, Tocsg.Obj,
Winapi.Messages, Tocsg.CommonData, Define, DefineKeyMon;
{$IFDEF _HE_}
{$DEFINE BLOCK_KEY}
{$ENDIF}
const
WM_CATCHKEY_NOTIFY = WM_USER + 3948;
WM_CATCHMOUSE_NOTIFY = WM_CATCHKEY_NOTIFY + 1;
type
THookState = (hsFree, hsHooking, hsReload, hsFinish);
PSharedData = ^TSharedData;
TSharedData = packed record
hRcvWnd: ULONGLONG; // 8 바이트 고정
dwLastInputTick: DWORD;
HookState: THookState;
{$IFDEF BLOCK_KEY}
DisESC,
DisWin,
DisAlt,
DisCtrl: Boolean;
DisEtcs: array [0..30] of Byte;
{$ENDIF}
end;
TInstallCatchKeyHook = function: Integer; stdcall;
TUninstallCatchKeyHook = function: Integer; stdcall;
TKeyMonDLL = class(TTgObject)
private
hDll_: THandle;
SharedData_: TTgFileMapping<TSharedData>;
fnInstallCatchKeyHook_: TInstallCatchKeyHook;
fnUnInstallCatchKeyHook_: TUninstallCatchKeyHook;
public
Constructor Create(hRcvWnd: HWND; const sShareMapFilename, sDllPath: String);
Destructor Destroy; override;
function InstallCatchKeyHook: Integer;
function UnInstallCatchKeyHook: Integer;
{$IFDEF BLOCK_KEY}
procedure SetDisableKey_ESC(const bVal: Boolean);
procedure SetDisableKey_Win(const bVal: Boolean);
procedure SetDisableKey_Alt(const bVal: Boolean);
procedure SetDisableKey_Ctrl(const bVal: Boolean);
procedure SetDisableKey_Etc(nIdx: Integer; ucKey: Byte);
{$ENDIF}
end;
implementation
uses
Tocsg.WinInfo, Tocsg.Path, Tocsg.Shell;
{ TKeyMonDLL }
Constructor TKeyMonDLL.Create(hRcvWnd: HWND; const sShareMapFilename, sDllPath: String);
var
sPath: String;
begin
SharedData_ := TTgFileMapping<TSharedData>.Create(sShareMapFilename, SizeOf(TSharedData));
ASSERT(SharedData_.IsAvailable);
ZeroMemory(SharedData_.Data, SizeOf(TSharedData));
SharedData_.Data.hRcvWnd := hRcvWnd;
SharedData_.Data.dwLastInputTick := 0;
SharedData_.Data.HookState := hsFree;
{$IFDEF BLOCK_KEY}
SharedData_.Data.DisESC := false;
SharedData_.Data.DisWin := false;
SharedData_.Data.DisAlt := false;
SharedData_.Data.DisCtrl := false;
{$ENDIF}
// _Trace('Wnd = %d', [hRcvWnd]);
Inherited Create;
hDll_ := LoadLibrary(PChar(sDllPath));
if hDll_ = 0 then
begin
_Trace('Fail .. LoadLibrary(), Path="%s"', [sDllPath]);
exit;
end;
@fnInstallCatchKeyHook_ := GetProcAddress(hDll_, 'InstallCatchKeyHook');
@fnUnInstallCatchKeyHook_ := GetProcAddress(hDll_, 'UnInstallCatchKeyHook');
ChangeWindowMessageFilter(WM_CATCHKEY_NOTIFY, MSGFLT_ADD);
nLastError_ := InstallCatchKeyHook;
end;
Destructor TKeyMonDLL.Destroy;
begin
if hDll_ <> 0 then
begin
UnInstallCatchKeyHook;
FreeLibrary(hDll_);
end;
Inherited;
FreeAndNil(SharedData_);
end;
function TKeyMonDLL.InstallCatchKeyHook: Integer;
begin
Result := -1;
if Assigned(fnInstallCatchKeyHook_) then
Result := fnInstallCatchKeyHook_;
end;
function TKeyMonDLL.UnInstallCatchKeyHook: Integer;
begin
Result := -1;
if Assigned(fnUnInstallCatchKeyHook_) then
Result := fnUnInstallCatchKeyHook_;
end;
{$IFDEF BLOCK_KEY}
procedure TKeyMonDLL.SetDisableKey_ESC(const bVal: Boolean);
begin
SharedData_.Data.DisESC := bVal;
end;
procedure TKeyMonDLL.SetDisableKey_Win(const bVal: Boolean);
begin
SharedData_.Data.DisWin := bVal;
end;
procedure TKeyMonDLL.SetDisableKey_Alt(const bVal: Boolean);
begin
SharedData_.Data.DisAlt := bVal;
end;
procedure TKeyMonDLL.SetDisableKey_Ctrl(const bVal: Boolean);
begin
SharedData_.Data.DisCtrl := bVal;
end;
procedure TKeyMonDLL.SetDisableKey_Etc(nIdx: Integer; ucKey: Byte);
begin
if (nIdx < 0) or (nIdx > 30) then
exit;
SharedData_.Data.DisEtcs[nIdx] := ucKey;
end;
{$ENDIF}
end.