MadCodeHook global mouse/keyboard hook demo code, please

Posted on 2006-05-30
Last Modified: 2010-04-05
Hi, a newbie question here.

Does anyone have any MadCodeHook global keyboard/mouse hook demos laying around?

Basically, I need a demo of how to hook the mouse to get the mouse pointer position and hook the keyboard to get the current key that is being pressed (I need to hook all the keys, not just a-z).

The demos that Madshi has are way over my head.  


Question by:devilduck
    LVL 7

    Expert Comment

    Here is a simple mousehook dll and test application that I downloaded from somewhere.  Not sure where.\Downloads\ExpertExchange\

    This is a system wide global hook.  Is that what you are looking for?

    Author Comment

    Thanks for the reply, kfoster11.  

    I am looking for demo code using MadCodeHook specifically.  


    LVL 33

    Accepted Solution

    hello devilduck, , I have done a little with the MadCodeHook, , but the MadCodeHook is a special and complex (compared to the usuall API), and if you are without deep API and code knowlage you can crash the whole system. . . I would not even think about using it unless I had a special hard to do problem. .

    I would ask you, why don't you use the API  GetCursorPos( ) function to get the cursor position (mouse pointer is not exactly a programming term). . . also you can get the key being pressed with the standard API Key Hook, but I do not think there may be any API functions you can "Hook" with MadCodeHook to get the cursor pos or the current key that is being pressed. .  which API functions would you hook for that sort of thing, I do not think that the term "Hook" in the  MadCodeHook, is the same sort of meaning as the "Hook" of the API  SetWindowsHookEx( ), , , but I could be wrong about how I understand your question
    LVL 5

    Expert Comment

    Few things that youll need to do first though

    1. first go to " " and download the (madcollection.exe)
    2. Create a dll using this code :

    library TPHook;

    uses Windows, madRemote, madCodeHook, madStrings;

    var TerminateProcessNext : function (processHandle, exitCode: dword) : bool; stdcall;

    function ThisIsOurProcess(processHandle: dword) : boolean;
    var pid   : dword;
        arrCh : array [0..MAX_PATH] of char;
      pid := ProcessHandleToId(processHandle);
      result := (pid <> 0) and ProcessIdToFileName(pid, arrCh) and
                (PosText('OurApplication.exe', arrCh) > 0);         <----*** very important use you own    
                                                                                                     application name

    function TerminateProcessCallback(processHandle, exitCode: dword) : bool; stdcall;
      if ThisIsOurProcess(processHandle) then begin
        result := false;
      end else
        result := TerminateProcessNext(processHandle, exitCode);

      HookAPI('kernel32.dll', 'TerminateProcess', @TerminateProcessCallback, @TerminateProcessNext);

    rename that dll to "TPHook.dll"

    3. in your program under uses "madcodehook"
    4. in your project should look something like this :

    program ***; // this is your program name that the dll will hook

      Forms,windows, madcodehook,

    {$R *.RES}


      InjectLibrary(ALL_SESSIONS or SYSTEM_PROCESSES, 'TPHook.dll');
      Application.Title := '';
      Application.CreateForm(TForm1, Form1);

    5. Last with the madcodehook in your unit1 uses (or whatever) you have to Uninject the dll something like :

    procedure TForm1.Exit2Click(Sender: TObject);
    UninjectLibrary(ALL_SESSIONS or SYSTEM_PROCESSES, 'TPHook.dll');

    So that if you call close from the program it self then only can you exit the program, otherwise if someone tries to end task it it wont allow it so long ass the DLL is hooked into the program...

    Peace Scay7
    LVL 5

    Expert Comment

    Oh dear me in my haste i pasted the wrong thing... that hook is to trap the exe from being end tasked... sowwie
    please ignore that post...

    Pease scay7


    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
    Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

    732 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

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now