Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to disable keyboard/mouse?

Posted on 1999-07-10
15
Medium Priority
?
987 Views
Last Modified: 2010-04-04
I am creating a teachers tool for a computerized classroom situation.. and I need to know what API's or code to use to disable/re-enable the keyboard and mouse.  thanks.. Delphi version 4.
0
Comment
Question by:fosco
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 4
  • +1
15 Comments
 
LVL 17

Accepted Solution

by:
inthe earned 400 total points
ID: 1387521
for the keyboard you'll need to make a small hook ( in dll) to capture the keyboard input and swallows it.i can send you a demo of this if you wish.just leave mail address.
as for the mouse you can also use a dll and catch the  wh_mouse or can do like below

procedure TForm1.FormCreate(Sender: TObject);
begin
showcursor(false); //hides/disables the mouse
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
showcursor(true);  // shows/re-enables the mouse
end;

this would stop the cursor appearing on your form altogether but cursor is still available to the system.
another way is to hold down shift key while selecting all the objects on the form with mousemove event with the left mouse button and then in the object inspector go to events tab and on the mousemove event double click and type:
setcursorpos(100,100);
now when the cursor moves over any of the objects it will jump to the position you set eg 100,100 or 0,0 for topleft corner,this would probebly work if just put on the forms mousemove event alone but i would prefer to use the showcursor() api instead.

there are example code on dejanews or i can send you a example when i get home just let me know.
Regards Barry
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1387522
Yep, Barry is right. Here comes some source:

// app

var dll, keybHook : cardinal;

procedure DisableKeyboard;
begin
  dll:=LoadLibrary('c:\yourAppPath\hook.dll');
  keybHook:=SetWindowsHookEx(WH_KEYBOARD,GetProcAddress(dll,'hookProc'),dll,0);
end;

procedure EnableKeyboard;
begin
  UnhookWindowsHookEx(keybHook);
  FreeLibrary(dll);
end;

// dll

library hook;

function hookProc(code, wParam, lParam: integer) : integer; stdcall;
begin
  result:=1;
end;

exports hookProc index 1 name 'hookProc';

end.

// Same method for the mouse, just change WH_KEYBOARD to WH_MOUSE...

Regards, Madshi.
0
 

Author Comment

by:fosco
ID: 1387523
I found code for the keyboard on the net in Inline Asm... it works fantastic and system wide.. anyone have inline asm code for the mouse??

if not.. are the solutions listed above SYSTEM WIDE ??

thanks,

fosco
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 20

Expert Comment

by:Madshi
ID: 1387524
Yep, the solution is system wide. Would you mind to show us the inline assembler code?

Regards, Madshi.
0
 
LVL 17

Expert Comment

by:inthe
ID: 1387525
hi all,
yes as madshi said these codes are system wide ,i too would be interested in seeing how its done the asm way ;-)
was it a good site where you found it,i am always interested in good delphi sites
here is a page about hooks if you want to know more about them and trapping keyboard/mouse messages:
http://www.gajits.com/p_delphihooks.html

Regards Barry
0
 

Author Comment

by:fosco
ID: 1387526
The inline assembler code follows: (it was taken from a post on dejanews)

Disable:

Asm
    in  al,21h
    or al,00000010b
 out 21h,al
End;

Enable:

Asm
    in  al,21h
    mov al,0
 out 21h,al
End;


I love how short this is =)... and I'm not very experienced with hooks... If someone has a quick snippet of code for disable mouse and enable mouse.. ill cut/paste it and use it.. hopefully someone does.. thanks guys,

fosco
0
 
LVL 17

Expert Comment

by:inthe
ID: 1387527
Hi
this looks positively stupid after seeing that 3 lines to disable the keys ;-( anyway here goes:
 
after not finding a way using asm here is some code for the mouse ;-)
i pulled this code from a keylogging app from a while ago,so some of the code may not be needed,there is a lot of memory mapping stuff going on ,i took out most of the irrelevent stuff and tested to make sure it still works.although dont let this code confuse you as this code was from a keyboard logging app ,i only needed to change WH_KEYBOARD to WH_MOUSE for it to work on the mouse,everything is the same for keyboard or mouse.

you just call startkeyboardhook or stopkeyboardhook etc to disable/reenable the mouse.
you should be able to paste these straight into a test project and a dll to see it work.
anyway here is a testapp :

unit TestTheHook;
interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls;

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

{Functions prototypes for the hook dll}
type TGetHookRecPointer = function : pointer stdcall;

type TStartKeyBoardHook = procedure stdcall;

type TStopKeyBoardHook = procedure stdcall;

{The record type filled in by the hook dll}
type THookRec = packed record
  TheHookHandle : HHOOK;
  TheAppWinHandle : HWND;
  TheCtrlWinHandle : HWND;
 
end;

{A pointer type to the hook record}
type PHookRec = ^THookRec;

var
  hHookLib : THANDLE; {A handle to the hook dll}
  GetHookRecPointer : TGetHookRecPointer; {Function pointer}
  StartKeyBoardHook : TStartKeyBoardHook; {Function pointer}
  StopKeyBoardHook : TStopKeyBoardHook; {Function pointer}
  LibLoadSuccess : bool; {If the hook lib was successfully loaded}
  lpHookRec : PHookRec; {A pointer to the hook record}
  EnterKeyCount : DWORD; {An internal count of the Enter Key}

  procedure TForm1.FormCreate(Sender: TObject);
begin
 {Set our initial variables}
  EnterKeyCount := 0;
  lpHookRec := NIL;
  LibLoadSuccess := FALSE;
  @GetHookRecPointer := NIL;
  @StartKeyBoardHook := NIL;
  @StopKeyBoardHook := NIL;
 {Try to load the hook dll}
  hHookLib := LoadLibrary('THEHOOK.DLL');
 {If the hook dll was loaded successfully}
  if hHookLib <> 0 then begin
   {Get the function addresses}
    @GetHookRecPointer :=
      GetProcAddress(hHookLib, 'GETHOOKRECPOINTER');
    @StartKeyBoardHook :=
      GetProcAddress(hHookLib, 'STARTKEYBOARDHOOK');
    @StopKeyBoardHook :=
      GetProcAddress(hHookLib, 'STOPKEYBOARDHOOK');
   {Did we find all the functions we need?}
    if ((@GetHookRecPointer <> NIL) AND
        (@StartKeyBoardHook <> NIL) AND
        (@StopKeyBoardHook <> NIL)) then begin
       LibLoadSuccess := TRUE;
      {Get a pointer to the hook record}
       lpHookRec := GetHookRecPointer;
      {Were we successfull in getting a ponter to the hook record}
       if (lpHookRec <> nil) then begin
        {Fill in our portion of the hook record}
         lpHookRec^.TheHookHandle := 0;
         
        {Start the keyboard hook}
         StartKeyBoardHook;
        {Start the timer if the hook was successfully set}
         SHOWCURSOR(FALSE);
         if (lpHookRec^.TheHookHandle <> 0) then begin
       
         end;
       end;
    end else begin
     {We failed to find all the functions we need}
      FreeLibrary(hHookLib);
      hHookLib := 0;
      @GetHookRecPointer := NIL;
      @StartKeyBoardHook := NIL;
      @StopKeyBoardHook := NIL;
    end;
  end;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
 {Did we load the dll successfully?}
  if (LibLoadSuccess = TRUE) then begin
   {Did we sucessfully get a pointer to the hook record?}
    if (lpHookRec <> nil) then begin
     {Did the hook get set?}
      if (lpHookRec^.TheHookHandle <> 0) then begin
        StopKeyBoardHook;
        SHOWCURSOR(TRUE);
      end;
    end;
   {Free the hook dll}
    FreeLibrary(hHookLib);
  end;
end;

end.






and here is the code for the dll:





library TheHook;

uses
  Windows,
  Messages,
  Dialogs,
  SysUtils;

{Define a record for recording and passing information process wide}  
type
  PHookRec = ^THookRec;
  THookRec = packed record
    TheHookHandle : HHOOK;
    TheAppWinHandle : HWND;
    TheCtrlWinHandle : HWND;
    TheKeyCount : DWORD;
  end;

var
  hObjHandle : THandle; {Variable for the file mapping object}
  lpHookRec : PHookRec; {Pointer to our hook record}

procedure MapFileMemory(dwAllocSize : DWORD);
begin
 {Create a process wide memory mapped variable}
  hObjHandle := CreateFileMapping($FFFFFFFF,
                                  NIL,
                                  PAGE_READWRITE,
                                  0,
                                  dwAllocSize,
                                  'HookRecMemBlock');
   if (hObjHandle = 0) then begin
     MessageBox(0,
                'Hook DLL',
                'Could not create file map object',
                MB_OK);
     exit;
   end;
 {Get a pointer to our process wide memory mapped variable}
  lpHookRec := MapViewOfFile(hObjHandle,
                             FILE_MAP_WRITE,
                             0,
                             0,
                             dwAllocSize);
  if (lpHookRec = NIL) then begin
    CloseHandle(hObjHandle);
    MessageBox(0,
               'Hook DLL',
               'Could not map file',
               MB_OK);
    exit;
  end;
end;


procedure UnMapFileMemory;
begin
 {Delete our process wide memory mapped variable}
  if (lpHookRec <> NIL) then begin
    UnMapViewOfFile(lpHookRec);
    lpHookRec := NIL;
  end;
  if (hObjHandle > 0) then begin
    CloseHandle(hObjHandle);
    hObjHandle := 0;
  end;
end;


function GetHookRecPointer : pointer stdcall;
begin
 {Return a pointer to our process wide memory mapped variable}
  result := lpHookRec;
end;


{The function that actually processes the keystrokes for our hook}
function KeyBoardProc(Code : integer;
                      wParam : integer;
                      lParam : integer): integer; stdcall;
begin
   {if (Code < 0) then
   Call the next hook in the hook chain
    result :=
      CallNextHookEx(lpHookRec^.TheHookHandle,
                     Code,
                     wParam,
                     lParam); }
     result:=1;      {<<----most important line}
{this is what swallows the mouse or keys if you change WH_MOUSE to WH_KEYBOARD}
end;


procedure StartKeyBoardHook; stdcall;
begin
 {If we have a process wide memory variable}
 {and the hook has not already been set...}
  if ((lpHookRec <> NIL) AND
      (lpHookRec^.TheHookHandle = 0)) then begin
   {Set the hook and remember our hook handle}
    lpHookRec^.TheHookHandle := SetWindowsHookEx(WH_MOUSE,                                                 @KeyBoardProc,                                                 hInstance,                                                 0);
  end;
end;


procedure StopKeyBoardHook; stdcall;
begin
 {If we have a process wide memory variable}
 {and the hook has already been set...}
  if ((lpHookRec <> NIL) AND
      (lpHookRec^.TheHookHandle <> 0)) then begin
   {Remove our hook and clear our hook handle}
    if (UnHookWindowsHookEx(lpHookRec^.TheHookHandle) <> FALSE) then
begin
      lpHookRec^.TheHookHandle := 0;
    end;
  end;
end;


procedure DllEntryPoint(dwReason : DWORD);
begin
  case dwReason of
    Dll_Process_Attach : begin
     {If we are getting mapped into a process, then get}
     {a pointer to our process wide memory mapped variable}
      hObjHandle := 0;
      lpHookRec := NIL;
      MapFileMemory(sizeof(lpHookRec^));
    end;
    Dll_Process_Detach : begin
     {If we are getting unmapped from a process then, remove}
     {the pointer to our process wide memory mapped variable}
      UnMapFileMemory;
    end;
  end;
end;


exports
  KeyBoardProc name 'KEYBOARDPROC',
  GetHookRecPointer name 'GETHOOKRECPOINTER',
  StartKeyBoardHook name 'STARTKEYBOARDHOOK',
  StopKeyBoardHook name 'STOPKEYBOARDHOOK';



begin
 {Set our Dll's main entry point}
  DLLProc := @DllEntryPoint;
 {Call our Dll's main entry point}
  DllEntryPoint(Dll_Process_Attach);
end.


Regards Barry
0
 
LVL 12

Expert Comment

by:rwilson032697
ID: 1387528
Eaves-dropping!
0
 

Author Comment

by:fosco
ID: 1387529
oh wow, inthe.... that is absolutely amazing.. heh... yuck....... shucks...

fosco
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1387530
Hi guys, Barry's code looks impressive, but why memory mapped files and all that stuff??? We don't want to transport something, we only want to DISABLE something. Have you tried my code from my first comment. It's much shorter...    :O)

Regards, Madshi.
0
 

Author Comment

by:fosco
ID: 1387531
madshi, is it really necessary to have a DLL?? can't this just be done in the program???  
fosco
0
 
LVL 17

Expert Comment

by:inthe
ID: 1387532
if you put the code in the program is is not system wide,in a dll it is system wide.

and yes madshi i did mention about alot of code this was because i pulled it out of a kelogger program i have and as the ciode the same for mouse i just changed WH_MOUSE tested and poasted it here for all to have a laugh at. :-)
basically it is just a big version of madshi's code..

my mate is a bit of a asm wizard and he said if you make cx = 2000h in asm it will disable the mouse but we couldnt get it  working in delphi only in masm on a dos based prog :-(
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1387533
Well, this assembler hack is new to me. If there is something like that for the mouse, too: Nice!
However, I think this stuff will work only for win9x anyway. So I think you should better go the safe way and use a system wide hook - which can only be installed by using a DLL.   :-(
Hmm. Can anyone explain me what this assembler code *REALLY* does?    :-))

Regards, Madshi.
0
 

Author Comment

by:fosco
ID: 1387534
now that im thinking about it.. the way my program is going to work.. is when the students program gets told by the teachers program via the network, to lock the student up.. the program is going Modal... so the quick snippet of locking it in the program should work fine.. thanks ALOT guys and I am grading inthe's answer with an A =)...
0
 
LVL 17

Expert Comment

by:inthe
ID: 1387535
thanks fosco

madshi
00000010b (or 10b to make the code smaller) is the input buffer which is emtpied by using the "or"( or xor will do same) command  .
actually i cant remember if thats right way round it may be fulling the buffer..its one of those though ;-)
as you mentioned im not sure if that'll work on nt ..
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses

662 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question