1. Home
  2. Computing & Technology
  3. Delphi Programming

Disable Mouse and Keyboard from Delphi Code

By , About.com Guide

The BlockInput API function blocks keyboard and mouse input events from reaching applications.

Place a Button (name: "Button1") on a form (name: "Form1") and use this code for the button's OnClick event handler. Once you click the button your mouse and keyboard will be blocked for 5 seconds.

Note: BlockInput is available only on Windows 98 and newer, this is why we have a "FuncAvaial" procedure - to test the existance of a function inside a DLL.

procedure TForm1.Button1Click(Sender: TObject) ;

  function FuncAvail(dllName, funcName: string; var p: pointer): boolean;
  var
    lib: THandle;
  begin
    result := false;
    p := nil;
    if LoadLibrary(PChar(dllName)) = 0 then exit;
    lib := GetModuleHandle(PChar(dllName)) ;
    if lib <> 0 then
    begin
     p := GetProcAddress(lib, PChar(funcName)) ;
     if p <> nil then Result := true;
    end;
  end;

  var
    BlockInput : function(Block: BOOL): BOOL; stdcall;

  begin
   if FuncAvail('USER32.DLL', 'BlockInput', @BlockInput) then
   begin
    ShowMessage('Your Mouse and Keyboard will be blocked for 5 seconds!') ;
    BlockInput(true) ;
    Sleep(5000) ;
    BlockInput(false) ;
   end;
  end;

end.
Delphi tips navigator:
» Get memory status
« Hide/Show System Tray
Explore Delphi Programming
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Using VCL Components
  5. TMouse
  6. Disable Mouse and Keyboard from Delphi Code

©2009 About.com, a part of The New York Times Company.

All rights reserved.