Главная страница
    Top.Mail.Ru    Яндекс.Метрика
Форум: "WinAPI";
Текущий архив: 2007.06.10;
Скачать: [xml.tar.bz2];

Вниз

Хук без формы   Найти похожие ветки 

 
Dmitry_177   (2006-12-31 02:35) [0]

Если нужно сделать хук без формы, нужно всеравно создавать цикл сообщений т.е. примерно так?

 hKeyHook := SetWindowsHookEx(WH_KEYBOARD_LL, @LowLevelKeyboardProc, hInstance, 0);
 if hKeyHook = 0 then PostQuitMessage(GetLastError);

 while GetMessage(AMessage, 0, 0, 0) do
 begin
   TranslateMessage(AMessage);
   DispatchMessage(AMessage);
 end;

 UnhookWindowsHookEx(hKeyHook);

Или надо еще какнибудь?


 
Anatoly Podgoretsky ©   (2006-12-31 12:41) [1]

> Dmitry_177  (31.12.2006 2:35:00)  [0]

А где здесь форма?
ВинАПИ ничего про формы не знает, не нужна ему форма.


 
Piroxyline ©   (2006-12-31 13:03) [2]

Не понял вопроса, но вот пример из DRKB:


library HookDll;

uses
 SysUtils,
 Classes,windows;

var CurrentHook: HHook;
   KeyArray: array[0..19] of byte;
   KeyArrayPtr: integer;
   CurFile: file of byte;
{
GlobalKeyboardHook
------------
This is the hook procedure to be loaded from hooks.exe when you
try and create a global hook. It is similar in structure to that defined
in hook.dpr for creating a local hook, but this time it does not beep!
Instead it stores each key pressed in an array of bytes (20 long). Whenever
this array gets full, it writes it to a file, log.txt and starts again.
}
function GlobalKeyBoardHook(code: integer; wParam: word; lParam: longword): longword; stdcall;
begin
   if code<0 then begin  //if code is <0 your keyboard hook should always run CallNextHookEx instantly and
      GlobalKeyBoardHook:=CallNextHookEx(CurrentHook,code,wParam,lparam); //then return the value from it.
      Exit;
   end;
   //firstly, is the key being pressed, and is it between A and Z
   //note that wParam contains the scan code of the key (which for a-z is the same as the ascii value)
   if ((lParam and KF_UP)=0) and (wParam>=65) and (wParam<=90) then begin
        //store the keycode in the list of keys pressed and increase the pointer
        KeyArray[KeyArrayPtr]:=wParam;
        KeyArrayPtr:=KeyArrayPtr+1;
        //if 20 keys have been recorded, save them to log.txt and start again
        if KeyArrayPtr>19 then begin
           assignfile(CurFile,"log.txt");
           if fileexists("log.txt")=false then rewrite(CurFile) else reset(CurFile); //if log.txt exists, add to it, otherwise recreate it
           blockwrite(CurFile,KeyArray[0],20);
           closefile(CurFile);
           KeyArrayPtr:=0;
        end;
   end;
   CallNextHookEx(CurrentHook,code,wParam,lparam);  //call the next hook proc if there is one
   GlobalKeyBoardHook:=0; //if GlobalKeyBoardHook returns a non-zero value, the window that should get
                    //the keyboard message doesnt get it.
   Exit;
end;

{
SetHookHandle
-------------
This procedure is called by hooks.exe simply to "inform" the dll of
the handle generated when creating the hook. This is required
if the hook procedure is to call CallNextHookEx. It also resets the
position in the key list to 0.

}
procedure SetHookHandle(HookHandle: HHook); stdcall;
begin
   CurrentHook:=HookHandle;
   KeyArrayPtr:=0;
end;

exports GlobalKeyBoardHook index 1,
       SetHookHandle index 2;
begin

end.

unit MainFormUnit;

interface

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

type
 TMainForm = class(TForm)
   Button1: TButton;
   Button2: TButton;
   procedure Button1Click(Sender: TObject);
   procedure FormCreate(Sender: TObject);
   procedure FormDestroy(Sender: TObject);
   procedure Button2Click(Sender: TObject);
 private
   { Private declarations }
 public
   { Public declarations }
 end;

var
 MainForm: TMainForm;

implementation

{$R *.DFM}

procedure TMainForm.Button1Click(Sender: TObject);
begin
   if GHookInstalled=true then exit; //if a global hook is installed, exit routine
    //if a local hook not installed, then attempt to install one, else attempt to remove one
    if HookInstalled=false then HookInstalled:=SetupLocalHook else HookInstalled:=not(RemoveLocalHook);
end;

procedure TMainForm.FormCreate(Sender: TObject);
begin
    HookInstalled:=false;
    GHookInstalled:=false;
    LibLoaded:=false;
end;

procedure TMainForm.FormDestroy(Sender: TObject);
begin
    if HookInstalled=true then RemoveLocalHook;
end;

procedure TMainForm.Button2Click(Sender: TObject);
begin
    if HookInstalled=true then exit; //if a local hook is installed, exit routine
    //if a local hook not installed, then attempt to install one, else attempt to remove one
    //note that removelocalhook can still be used no matter whether the hook is global or local
    if GHookInstalled=false then GHookInstalled:=SetupGlobalHook else GHookInstalled:=not(RemoveLocalHook);
end;

end.


 
kaZaNoVa ©   (2007-01-04 13:25) [3]

более того, хук мона и без длл делать)

unit Unit1;

interface

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

type
 TForm1 = class(TForm)
   Button1: TButton;
   Button2: TButton;
   procedure Button1Click(Sender: TObject);
   procedure Button2Click(Sender: TObject);
 private
   { Private declarations }
 public
   { Public declarations }
 end;

var
 Form1: TForm1;

implementation

{$R *.dfm}

const
 WH_KEYBOARD_LL = 13;
 WH_MOUSE_LL = 14;

 LLKHF_EXTENDED = KF_EXTENDED shr 8;
 LLKHF_INJECTED = $00000010;
 LLKHF_ALTDOWN = KF_ALTDOWN shr 8;
 LLKHF_UP = KF_UP shr 8;

 LLMHF_INJECTED = $00000001;

{ Structure used by WH_KEYBOARD_LL }
type
 tagKBDLLHOOKSTRUCT  = packed record
   vkCode : DWord;
   scanCode : DWord;
   flags : DWord;
   time : DWord;
   dwExtraInfo : PDWord;
 end;
 TKBDLLHOOKSTRUCT = tagKBDLLHOOKSTRUCT;
 PKBDLLHOOKSTRUCT = ^TKBDLLHOOKSTRUCT;

{ Structure used by WH_MOUSE_LL }
 tagMSLLHOOKSTRUCT = packed record
   pt : TPoint;
   mouseData : DWord;
   flags : DWord;
   time : DWord;
   dwExtraInfo : PDWord;
 end;
 tMSLLHOOKSTRUCT = tagMSLLHOOKSTRUCT;
 PMSLLHOOKSTRUCT = ^TMSLLHOOKSTRUCT;

Var hhk: HHOOK;

function  LowLevelKeyboardProc(nCode :Longint; awParam: WParam; alParam:
LParam): DWord; stdcall;
var
 fEatKeystroke: Boolean;
 p: PKBDLLHOOKSTRUCT;
 buf: array [0..255] of Char;
 sc: UINT;
begin
  fEatKeystroke := False;

  if (nCode = HC_ACTION) then
     case (awParam) of
      WM_KEYDOWN, WM_SYSKEYDOWN,
      WM_KEYUP, WM_SYSKEYUP:
        begin
         p := PKBDLLHOOKSTRUCT(alParam);
         fEatKeystroke :=
           ((p.vkCode = VK_TAB) and (p.flags and LLKHF_ALTDOWN <> 0)) or
           ((p.vkCode = VK_ESCAPE) and ((p.flags and LLKHF_ALTDOWN) <> 0)) or
           ((p.vkCode = VK_ESCAPE) and ((GetKeyState(VK_CONTROL) and $8000)
<>0));
        end;
      end;

  if fEatKeystroke then
    Result := 1
  else
    Result := CallNextHookEx(0, nCode, awParam, alParam);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
hhk:= SetWindowsHookEx(WH_KEYBOARD_LL,@LowLevelKeyboardProc, Hinstance, 0);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
UnHookWindowsHookEx(hhk);
end;

end.



Страницы: 1 вся ветка

Форум: "WinAPI";
Текущий архив: 2007.06.10;
Скачать: [xml.tar.bz2];

Наверх




Память: 0.47 MB
Время: 0.034 c
15-1178977274
parus
2007-05-12 17:41
2007.06.10
TV Tuner


15-1178787861
Vlad Oshin
2007-05-10 13:04
2007.06.10
Почему то сбилось нахождение кода по щелчку в ObjectInspector


2-1179458410
delphino
2007-05-18 07:20
2007.06.10
Как подставить в ComboBox значения из таблицы БД?


2-1179643152
А.Брей
2007-05-20 10:39
2007.06.10
Переменная


15-1179073175
Василий Кузнецов
2007-05-13 20:19
2007.06.10
.GIF





Afrikaans Albanian Arabic Armenian Azerbaijani Basque Belarusian Bulgarian Catalan Chinese (Simplified) Chinese (Traditional) Croatian Czech Danish Dutch English Estonian Filipino Finnish French
Galician Georgian German Greek Haitian Creole Hebrew Hindi Hungarian Icelandic Indonesian Irish Italian Japanese Korean Latvian Lithuanian Macedonian Malay Maltese Norwegian
Persian Polish Portuguese Romanian Russian Serbian Slovak Slovenian Spanish Swahili Swedish Thai Turkish Ukrainian Urdu Vietnamese Welsh Yiddish Bengali Bosnian
Cebuano Esperanto Gujarati Hausa Hmong Igbo Javanese Kannada Khmer Lao Latin Maori Marathi Mongolian Nepali Punjabi Somali Tamil Telugu Yoruba
Zulu
Английский Французский Немецкий Итальянский Португальский Русский Испанский