[Delphi] キーボードとマウスを使えなくする

Windows2000 / WindowsXP で動作確認しています。
以下のサンプルコードを実行すると、15秒間キーボードとマウスを受け付けなくなります。
以下のサンプルは、USER32.DLL にある BlockInput というエントリポイントの機能を使用しています。
BlockInput は引数に true を与えるとロックを false を与えるとロック解除します。
function FuncAvail(Lib, Func: string; var hnd: THandle; var ptr: Pointer): boolean;
begin
  Result := false;
  hnd := 0;
  if LoadLibrary(PChar(Lib)) = 0 then
  begin
    Exit;
  end;
  hnd := GetModuleHandle(PChar(Lib));
  if (hnd <> 0) then
  begin
    ptr := GetProcAddress(hnd, PChar(Func));
    if (ptr <> nil) then
    begin
      Result := true;
    end;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  hnd: THandle;
  xBlockInput: function(Block: BOOL): BOOL; stdcall;
begin
  if FuncAvail('USER32.DLL', 'BlockInput', hnd, @xBlockInput) then
  begin 
    xBlockInput(true);
    Sleep(15000);         // 15秒 
    xBlockInput(false); 
    FreeLibrary(hnd);
  end;
end;

コメント (0件)


くろねこ研究所
https://www.blackcat.xyz/article.php/ProgramingFAQ_del0022