Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Delphi
  4. About Delphi KeyBoard Hook question,I need your help !

About Delphi KeyBoard Hook question,I need your help !

Scheduled Pinned Locked Moved Delphi
helpdelphigraphicsperformancequestion
5 Posts 5 Posters 12 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    true_jeamy
    wrote on last edited by
    #1

    I program on Delphi 7 . First I Create a DLL Code :

    library HookDLL;

    { Important note about DLL memory management: ShareMem must be the
    first unit in your library's USES clause AND your project's (select
    Project-View Source) USES clause if your DLL exports any procedures or
    functions that pass strings as parameters or function results. This
    applies to all strings passed to and from your DLL--even those that
    are nested in records and classes. ShareMem is the interface unit to
    the BORLNDMM.DLL shared memory manager, which must be deployed along
    with your DLL. To avoid using BORLNDMM.DLL, pass string information
    using PChar or ShortString parameters. }

    uses
    SysUtils,Classes,Dialogs,windows;

    {$R *.res}

    var
    hookHandle:HHOOK;
    hookCode:string;

    function KeyBoardHook(code:Integer;
    wparaw:Cardinal;
    lparaw:Cardinal):Cardinal;stdcall;
    begin
    if code >0 then
    begin
    // hookCode := hookCode + IntToStr(code);
    ShowMessage(IntToStr(code));
    CallNextHookEx(hookHandle,code,wparaw,lparaw);
    end;
    end;

    function StartHook(instance:Cardinal;SysType:Integer):Boolean;stdcall;
    begin
    hookHandle := SetWindowsHookEx(WH_KEYBOARD,@KeyBoardHook,instance,SysType);
    if hookHandle <> 0 then
    begin
    ShowMessage('install Success!');
    Result := True;
    end
    else
    begin
    ShowMessage('install Error!');
    Result := False;
    end;
    end;

    function UnSetHook:Boolean;stdcall;
    begin
    Result := UnhookWindowsHookEx(hookHandle);
    end;

    function GetPass:string;stdcall;
    begin
    Result := hookCode;
    end;

    exports
    StartHook,GetPass,UnSetHook;

    begin

    end.

    and use code

    unit SetHook;

    interface

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

    type
    TForm1 = class(TForm)
    btn1: TButton;
    btn2: TButton;
    btn3: TButton;
    tmr1: TTimer;
    procedure btn1Click(Sender: TObject);
    procedure btn2Click(Sender: TObject);
    procedure btn3Click(Sender: TObject);
    procedure tmr1Timer(Sender: TObject);
    private
    { Private declarations }
    public
    { Public declarations }

    end;

    var
    Form1: TForm1;

    function UnSetHook:Boolean; external 'HookDLL.dll';
    function GetPass:string; external 'HookDLL.dll';
    function StartHook(instance:Car

    D D N G 4 Replies Last reply
    0
    • T true_jeamy

      I program on Delphi 7 . First I Create a DLL Code :

      library HookDLL;

      { Important note about DLL memory management: ShareMem must be the
      first unit in your library's USES clause AND your project's (select
      Project-View Source) USES clause if your DLL exports any procedures or
      functions that pass strings as parameters or function results. This
      applies to all strings passed to and from your DLL--even those that
      are nested in records and classes. ShareMem is the interface unit to
      the BORLNDMM.DLL shared memory manager, which must be deployed along
      with your DLL. To avoid using BORLNDMM.DLL, pass string information
      using PChar or ShortString parameters. }

      uses
      SysUtils,Classes,Dialogs,windows;

      {$R *.res}

      var
      hookHandle:HHOOK;
      hookCode:string;

      function KeyBoardHook(code:Integer;
      wparaw:Cardinal;
      lparaw:Cardinal):Cardinal;stdcall;
      begin
      if code >0 then
      begin
      // hookCode := hookCode + IntToStr(code);
      ShowMessage(IntToStr(code));
      CallNextHookEx(hookHandle,code,wparaw,lparaw);
      end;
      end;

      function StartHook(instance:Cardinal;SysType:Integer):Boolean;stdcall;
      begin
      hookHandle := SetWindowsHookEx(WH_KEYBOARD,@KeyBoardHook,instance,SysType);
      if hookHandle <> 0 then
      begin
      ShowMessage('install Success!');
      Result := True;
      end
      else
      begin
      ShowMessage('install Error!');
      Result := False;
      end;
      end;

      function UnSetHook:Boolean;stdcall;
      begin
      Result := UnhookWindowsHookEx(hookHandle);
      end;

      function GetPass:string;stdcall;
      begin
      Result := hookCode;
      end;

      exports
      StartHook,GetPass,UnSetHook;

      begin

      end.

      and use code

      unit SetHook;

      interface

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

      type
      TForm1 = class(TForm)
      btn1: TButton;
      btn2: TButton;
      btn3: TButton;
      tmr1: TTimer;
      procedure btn1Click(Sender: TObject);
      procedure btn2Click(Sender: TObject);
      procedure btn3Click(Sender: TObject);
      procedure tmr1Timer(Sender: TObject);
      private
      { Private declarations }
      public
      { Public declarations }

      end;

      var
      Form1: TForm1;

      function UnSetHook:Boolean; external 'HookDLL.dll';
      function GetPass:string; external 'HookDLL.dll';
      function StartHook(instance:Car

      D Offline
      D Offline
      dan_fish
      wrote on last edited by
      #2

      I think your starting point should be learning how to use the debugger, rather than copying low-level code off the internet and then asking other people to explain it to you.

      1 Reply Last reply
      0
      • T true_jeamy

        I program on Delphi 7 . First I Create a DLL Code :

        library HookDLL;

        { Important note about DLL memory management: ShareMem must be the
        first unit in your library's USES clause AND your project's (select
        Project-View Source) USES clause if your DLL exports any procedures or
        functions that pass strings as parameters or function results. This
        applies to all strings passed to and from your DLL--even those that
        are nested in records and classes. ShareMem is the interface unit to
        the BORLNDMM.DLL shared memory manager, which must be deployed along
        with your DLL. To avoid using BORLNDMM.DLL, pass string information
        using PChar or ShortString parameters. }

        uses
        SysUtils,Classes,Dialogs,windows;

        {$R *.res}

        var
        hookHandle:HHOOK;
        hookCode:string;

        function KeyBoardHook(code:Integer;
        wparaw:Cardinal;
        lparaw:Cardinal):Cardinal;stdcall;
        begin
        if code >0 then
        begin
        // hookCode := hookCode + IntToStr(code);
        ShowMessage(IntToStr(code));
        CallNextHookEx(hookHandle,code,wparaw,lparaw);
        end;
        end;

        function StartHook(instance:Cardinal;SysType:Integer):Boolean;stdcall;
        begin
        hookHandle := SetWindowsHookEx(WH_KEYBOARD,@KeyBoardHook,instance,SysType);
        if hookHandle <> 0 then
        begin
        ShowMessage('install Success!');
        Result := True;
        end
        else
        begin
        ShowMessage('install Error!');
        Result := False;
        end;
        end;

        function UnSetHook:Boolean;stdcall;
        begin
        Result := UnhookWindowsHookEx(hookHandle);
        end;

        function GetPass:string;stdcall;
        begin
        Result := hookCode;
        end;

        exports
        StartHook,GetPass,UnSetHook;

        begin

        end.

        and use code

        unit SetHook;

        interface

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

        type
        TForm1 = class(TForm)
        btn1: TButton;
        btn2: TButton;
        btn3: TButton;
        tmr1: TTimer;
        procedure btn1Click(Sender: TObject);
        procedure btn2Click(Sender: TObject);
        procedure btn3Click(Sender: TObject);
        procedure tmr1Timer(Sender: TObject);
        private
        { Private declarations }
        public
        { Public declarations }

        end;

        var
        Form1: TForm1;

        function UnSetHook:Boolean; external 'HookDLL.dll';
        function GetPass:string; external 'HookDLL.dll';
        function StartHook(instance:Car

        D Offline
        D Offline
        Dr Walt Fair PE
        wrote on last edited by
        #3

        It's recommended that you not put your email address in the message. No one is going to emil you an answer - you'll have to read the forum to see if there is any help. As suggested, use the debugger and find out where the error is occurring. Once you know that, then you see how to fix it.

        CQ de W5ALT

        Walt Fair, Jr., P. E. Comport Computing Specializing in Technical Engineering Software

        1 Reply Last reply
        0
        • T true_jeamy

          I program on Delphi 7 . First I Create a DLL Code :

          library HookDLL;

          { Important note about DLL memory management: ShareMem must be the
          first unit in your library's USES clause AND your project's (select
          Project-View Source) USES clause if your DLL exports any procedures or
          functions that pass strings as parameters or function results. This
          applies to all strings passed to and from your DLL--even those that
          are nested in records and classes. ShareMem is the interface unit to
          the BORLNDMM.DLL shared memory manager, which must be deployed along
          with your DLL. To avoid using BORLNDMM.DLL, pass string information
          using PChar or ShortString parameters. }

          uses
          SysUtils,Classes,Dialogs,windows;

          {$R *.res}

          var
          hookHandle:HHOOK;
          hookCode:string;

          function KeyBoardHook(code:Integer;
          wparaw:Cardinal;
          lparaw:Cardinal):Cardinal;stdcall;
          begin
          if code >0 then
          begin
          // hookCode := hookCode + IntToStr(code);
          ShowMessage(IntToStr(code));
          CallNextHookEx(hookHandle,code,wparaw,lparaw);
          end;
          end;

          function StartHook(instance:Cardinal;SysType:Integer):Boolean;stdcall;
          begin
          hookHandle := SetWindowsHookEx(WH_KEYBOARD,@KeyBoardHook,instance,SysType);
          if hookHandle <> 0 then
          begin
          ShowMessage('install Success!');
          Result := True;
          end
          else
          begin
          ShowMessage('install Error!');
          Result := False;
          end;
          end;

          function UnSetHook:Boolean;stdcall;
          begin
          Result := UnhookWindowsHookEx(hookHandle);
          end;

          function GetPass:string;stdcall;
          begin
          Result := hookCode;
          end;

          exports
          StartHook,GetPass,UnSetHook;

          begin

          end.

          and use code

          unit SetHook;

          interface

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

          type
          TForm1 = class(TForm)
          btn1: TButton;
          btn2: TButton;
          btn3: TButton;
          tmr1: TTimer;
          procedure btn1Click(Sender: TObject);
          procedure btn2Click(Sender: TObject);
          procedure btn3Click(Sender: TObject);
          procedure tmr1Timer(Sender: TObject);
          private
          { Private declarations }
          public
          { Public declarations }

          end;

          var
          Form1: TForm1;

          function UnSetHook:Boolean; external 'HookDLL.dll';
          function GetPass:string; external 'HookDLL.dll';
          function StartHook(instance:Car

          N Offline
          N Offline
          nortee
          wrote on last edited by
          #4

          Yeah... what the other two people said... Just out of curiosity though, why don't you just use a combination of the OnKeyPress, OnKeyUp and OnKeyDown events to handle what you need to do rather than the schlep of hooking into the keyboard? Also, remember to propogate meaningful error messages rather than just a one liner that won't tell you what the actual problem is. Use the GetLastError function (in the Windows.pas unit if I'm not mistaken) at the very least to see exactly what went wrong. Hope this helps you a little :)

          Cheers, Glen Vlotman QFTD: "You cannot code around stupidity..."

          1 Reply Last reply
          0
          • T true_jeamy

            I program on Delphi 7 . First I Create a DLL Code :

            library HookDLL;

            { Important note about DLL memory management: ShareMem must be the
            first unit in your library's USES clause AND your project's (select
            Project-View Source) USES clause if your DLL exports any procedures or
            functions that pass strings as parameters or function results. This
            applies to all strings passed to and from your DLL--even those that
            are nested in records and classes. ShareMem is the interface unit to
            the BORLNDMM.DLL shared memory manager, which must be deployed along
            with your DLL. To avoid using BORLNDMM.DLL, pass string information
            using PChar or ShortString parameters. }

            uses
            SysUtils,Classes,Dialogs,windows;

            {$R *.res}

            var
            hookHandle:HHOOK;
            hookCode:string;

            function KeyBoardHook(code:Integer;
            wparaw:Cardinal;
            lparaw:Cardinal):Cardinal;stdcall;
            begin
            if code >0 then
            begin
            // hookCode := hookCode + IntToStr(code);
            ShowMessage(IntToStr(code));
            CallNextHookEx(hookHandle,code,wparaw,lparaw);
            end;
            end;

            function StartHook(instance:Cardinal;SysType:Integer):Boolean;stdcall;
            begin
            hookHandle := SetWindowsHookEx(WH_KEYBOARD,@KeyBoardHook,instance,SysType);
            if hookHandle <> 0 then
            begin
            ShowMessage('install Success!');
            Result := True;
            end
            else
            begin
            ShowMessage('install Error!');
            Result := False;
            end;
            end;

            function UnSetHook:Boolean;stdcall;
            begin
            Result := UnhookWindowsHookEx(hookHandle);
            end;

            function GetPass:string;stdcall;
            begin
            Result := hookCode;
            end;

            exports
            StartHook,GetPass,UnSetHook;

            begin

            end.

            and use code

            unit SetHook;

            interface

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

            type
            TForm1 = class(TForm)
            btn1: TButton;
            btn2: TButton;
            btn3: TButton;
            tmr1: TTimer;
            procedure btn1Click(Sender: TObject);
            procedure btn2Click(Sender: TObject);
            procedure btn3Click(Sender: TObject);
            procedure tmr1Timer(Sender: TObject);
            private
            { Private declarations }
            public
            { Public declarations }

            end;

            var
            Form1: TForm1;

            function UnSetHook:Boolean; external 'HookDLL.dll';
            function GetPass:string; external 'HookDLL.dll';
            function StartHook(instance:Car

            G Offline
            G Offline
            GAMerritt
            wrote on last edited by
            #5

            To: 1102043058@qq.com To: Mr. #6352956 at CodeProject: I was wondering if you're still doing anything with that .DLL code you had up on the Delphi forum at CodeProject. I looked at it, and was trying to figure out whether there might be a thread-access problem (even a security block) that kept the hook from being set. I would have called GetLastError to find out why SetWindowsHookEx returned null. Did you ever do that? I'm curious to know what you found out if you did. In case you didn't get around to it, you can look up the parameter info on MSDN, which will tell you the same thing Delphi's documentation should have. (I haven't looked at it yet to see what it says about SetWindowsHookEx.) Here are the links: GetLastError function: http://msdn.microsoft.com/en-us/library/ms679360 SetWindowsHookEx function: http://msdn.microsoft.com/en-us/library/ms644990 GAMerritt 01-18-11

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups