[ borovac @ 05.07.2005. 18:01 ] @
Code:

library HookLib;

uses
  Windows,
  Messages,
  SysUtils;

type
  PHookRec = ^THookRec;
  THookRec = record
    AppHnd: Integer;
    MemoHnd: Integer;
  end;

var
  Hooked: Boolean;
  hKeyHook, hMemFile: HWND;
  PHookRec1: PHookRec;

function KeyHookFunc(Code, VirtualKey, KeyStroke: Integer): LRESULT; stdcall;
var
  KeyState1: TKeyBoardState;
  AryChar: array[0..1] of Char;
  Count: Integer;
begin
  Result := 0;
  if Code = HC_NOREMOVE then Exit;
  Result := CallNextHookEx(hKeyHook, Code, VirtualKey, KeyStroke);
  if Code < 0 then
    Exit;

  if Code = HC_ACTION then
  begin
    if ((KeyStroke and (1 shl 30)) <> 0) then
    begin
       if VirtualKey=VK_TAB then ....
    end;
  end;
end;


function StartHook(MemoHandle, AppHandle: HWND): Byte; export;
begin
  Result := 0;
  if Hooked then
  begin
    Result := 1;
    Exit;
  end;
  hKeyHook := SetWindowsHookEx(WH_KEYBOARD, KeyHookFunc, hInstance, 0);
  if hKeyHook > 0 then
  begin
{you need to use a mapped file because this DLL attatches to every app
that gets windows messages when it's hooked, and you can't get info except
through a Globally avaiable Mapped file}
    hMemFile := CreateFileMapping($FFFFFFFF, // $FFFFFFFF gets a page memory file
      nil,                // no security attributes
      PAGE_READWRITE,      // read/write access
      0,                   // size: high 32-bits
      SizeOf(THookRec),           // size: low 32-bits
      //SizeOf(Integer),
      'Global7v9k');    // name of map object
    PHookRec1 := MapViewOfFile(hMemFile, FILE_MAP_WRITE, 0, 0, 0);
    {set the Memo and App handles to the mapped file}
    Hooked := True;
  end 
  else
    Result := 2;
end;

function StopHook: Boolean; export;
begin
  if PHookRec1 <> nil then
  begin
    UnmapViewOfFile(PHookRec1);
    CloseHandle(hMemFile);
    PHookRec1 := nil;
  end;
  if Hooked then
    Result := UnhookWindowsHookEx(hKeyHook) 
  else
    Result := True;
  Hooked := False;
end;

procedure EntryProc(dwReason: DWORD);
begin
  if (dwReason = Dll_Process_Detach) then
  begin
    if PHookRec1 <> nil then
    begin
      UnmapViewOfFile(PHookRec1);
      CloseHandle(hMemFile);
    end;
    UnhookWindowsHookEx(hKeyHook);
  end;
end;

exports
  StartHook,
  StopHook;

begin
  PHookRec1 := nil;
  Hooked := False;
  hKeyHook := 0;
  hMemo := 0;
  DLLProc := @EntryProc;
  EntryProc(Dll_Process_Attach);
end.


kao što vidite ovaj dll obrađuje sve što dolazi sa tastature.
Kako sada to što dolazi sa tastature ne proslijediti dalje, tamo gdje treba da ide?
Znači kako da onemogućim neki pritisnut taster?
Gore kod linije
Code:
 if VirtualKey=VK_TAB then

šta treba uraditi pa da onemogućimo Tab??


[Ovu poruku je menjao borovac dana 05.07.2005. u 19:01 GMT+1]
[ Srki_82 @ 05.07.2005. 18:46 ] @
Ako ti je Result <> 0 taj key event nece doci do prozora, a ako je Result = 0 onda hoce.
[ borovac @ 05.07.2005. 22:30 ] @
znači da umjesto
Code:
Result := CallNextHookEx(hKeyHook, Code, VirtualKey, KeyStroke);

trebam staviti
Code:
Result := 1
[ borovac @ 06.07.2005. 11:28 ] @
Ovo sam uradio. I recimo otvorim notepad i pišem nešto bezveze. Moj program pamti to, dok se u notepad-u ne pojavljuje ništa. Znači te tipke su spriječene. Međutim kada pritisnem Alt+Tab ili Ctrl+Esc ili Ctrl+Alt+Del odgovarajuće stvari se izvrše. Znači ove windows kombinacije ne može spriječiti samo tako.
Kako njih da spriječim???
Please help
[ Srki_82 @ 06.07.2005. 13:43 ] @
Za to ti treba hook na WH_SYSMSGFILTER, a ne na WH_KEYBOARD.
[ borovac @ 06.07.2005. 14:29 ] @
Našao sam bio neki primjer koji tako onemogućava tipke, i koristi WH_GETMESSAGE.
Probat ću nešto skontat.
hvala svima.
[ obucina @ 06.07.2005. 14:54 ] @
Citat:
borovac: Našao sam bio neki primjer koji tako onemogućava tipke, i koristi WH_GETMESSAGE.

Hoces li podeliti taj primer sa nama?
[ Srki_82 @ 06.07.2005. 16:08 ] @
Pardon... ipak treba da se koristi WH_KEYBOARD_LL, ali koliko znam to ne postoji u Delphi 7 (mislim... nije deklarisana konstanta za to... mozda bih mogao malo da pronjuskam po C++ kodu i probam da je nadjem kao i prototip funkcije za LowLevel Keyboard Hook). Ovaj hook hvata svaki taster pre sistema tako da efikasno mozes da blokiras totalno tastaturu.
Videcu da prevedem sa C++ na Delphi sve sto je potrebno da bi ovo radilo... ako se razumes u C onda ce ti ovo pomoci
Code:
LRESULT CALLBACK LowLevelKeyboardProc (INT nCode, WPARAM wParam, LPARAM lParam)
{
    // By returning a non-zero value from the hook procedure, the
    // message does not get passed to the target window
    KBDLLHOOKSTRUCT *pkbhs = (KBDLLHOOKSTRUCT *) lParam;
    BOOL bControlKeyDown = 0;

    switch (nCode)
    {
        case HC_ACTION:
        {
            // Check to see if the CTRL key is pressed
            bControlKeyDown = GetAsyncKeyState (VK_CONTROL) >> ((sizeof(SHORT) * 8) - 1);

            // Disable CTRL+ESC
            if (pkbhs->vkCode == VK_ESCAPE && bControlKeyDown)
                return 1;

            // Disable ALT+TAB
            if (pkbhs->vkCode == VK_TAB && pkbhs->flags & LLKHF_ALTDOWN)
                return 1;

            // Disable ALT+ESC
            if (pkbhs->vkCode == VK_ESCAPE && pkbhs->flags & LLKHF_ALTDOWN)
                return 1;

            break;
        }

        default:
            break;
    }
    return CallNextHookEx (hHook, nCode, wParam, lParam);
}


Ovo je skinuto sa Microsoft Support sajta.
[ Nemanja Avramović @ 06.07.2005. 16:41 ] @
prevedi ti to u Delphi :)
[ Srki_82 @ 06.07.2005. 16:56 ] @
A tako me mrzi da instaliram MS Visual Studio
[ borovac @ 06.07.2005. 17:23 ] @
Svaka čast majstore, baš kad sam izgubio nadu.
Razumijem se ja u C++. Ali ipak ti to prevedi. U delphi-u nema LowLevelKeyboardProc niti WH_KEYBOARD_LL.
To je samo kod Microsoftovih alata, majka im stara!

Evo onog primjera!
DLL file:
Code:

unit WHookInt;

interface

uses
  Windows, Messages, SysUtils;

function SetHook(WinHandle: HWND; MsgToSend: Integer): Boolean; stdcall; export;
function FreeHook: Boolean; stdcall; export;
function MsgFilterFunc(Code: Integer; wParam, lParam: Longint): Longint stdcall; export;

implementation


// Memory map file stuff

{
  The CreateFileMapping function creates unnamed file-mapping object
  for the specified file.
}

function CreateMMF(Name: string; Size: Integer): THandle;
begin
  Result := CreateFileMapping($FFFFFFFF, nil, PAGE_READWRITE, 0, Size, PChar(Name));
  if Result <> 0 then
  begin
    if GetLastError = ERROR_ALREADY_EXISTS then
    begin
      CloseHandle(Result);
      Result := 0;
    end;
  end;
end;

{ The OpenFileMapping function opens a named file-mapping object. }

function OpenMMF(Name: string): THandle;
begin
  Result := OpenFileMapping(FILE_MAP_ALL_ACCESS, False, PChar(Name));
  // The return value is an open handle to the specified file-mapping object.
end;

{
 The MapViewOfFile function maps a view of a file into
 the address space of the calling process.
}

function MapMMF(MMFHandle: THandle): Pointer;
begin
  Result := MapViewOfFile(MMFHandle, FILE_MAP_ALL_ACCESS, 0, 0, 0);
end;

{
  The UnmapViewOfFile function unmaps a mapped view of a file
  from the calling process's address space.
}

function UnMapMMF(P: Pointer): Boolean;
begin
  Result := UnmapViewOfFile(P);
end;

function CloseMMF(MMFHandle: THandle): Boolean;
begin
  Result := CloseHandle(MMFHandle);
end;


// Actual hook stuff

type
  TPMsg = ^TMsg;

const
  VK_D = $44;
  VK_E = $45;
  VK_F = $46;
  VK_M = $4D;
  VK_R = $52;

  MMFName = 'MsgFilterHookDemo';

type
  PMMFData = ^TMMFData;
  TMMFData = record
    NextHook: HHOOK;
    WinHandle: HWND;
    MsgToSend: Integer;
  end;

  // global variables, only valid in the process which installs the hook.
var
  MMFHandle: THandle;
  MMFData: PMMFData;

function UnMapAndCloseMMF: Boolean;
begin
  Result := False;
  if UnMapMMF(MMFData) then
  begin
    MMFData := nil;
    if CloseMMF(MMFHandle) then
    begin
      MMFHandle := 0;
      Result := True;
    end;
  end;
end;

{
  The SetWindowsHookEx function installs an application-defined
  hook procedure into a hook chain.

  WH_GETMESSAGE Installs a hook procedure that monitors messages
  posted to a message queue.
  For more information, see the GetMsgProc hook procedure.
}

function SetHook(WinHandle: HWND; MsgToSend: Integer): Boolean; stdcall;
begin
  Result := False;
  if (MMFData = nil) and (MMFHandle = 0) then
  begin
    MMFHandle := CreateMMF(MMFName, SizeOf(TMMFData));
    if MMFHandle <> 0 then
    begin
      MMFData := MapMMF(MMFHandle);
      if MMFData <> nil then
      begin
        MMFData.WinHandle := WinHandle;
        MMFData.MsgToSend := MsgToSend;
        MMFData.NextHook := SetWindowsHookEx(WH_GETMESSAGE, MsgFilterFunc, HInstance, 0);

        if MMFData.NextHook = 0 then
          UnMapAndCloseMMF
        else
          Result := True;
      end
      else
      begin
        CloseMMF(MMFHandle);
        MMFHandle := 0;
      end;
    end;
  end;
end;


{
  The UnhookWindowsHookEx function removes the hook procedure installed
  in a hook chain by the SetWindowsHookEx function.
}

function FreeHook: Boolean; stdcall;
begin
  Result := False;
  if (MMFData <> nil) and (MMFHandle <> 0) then
    if UnHookWindowsHookEx(MMFData^.NextHook) then
      Result := UnMapAndCloseMMF;
end;



(*
    GetMsgProc(
    nCode: Integer;  {the hook code}
    wParam: WPARAM;  {message removal flag}
    lParam: LPARAM  {a pointer to a TMsg structure}
    ): LRESULT;  {this function should always return zero}

    { See help on ==> GetMsgProc}
*)

function MsgFilterFunc(Code: Integer; wParam, lParam: Longint): Longint;
var
  MMFHandle: THandle;
  MMFData: PMMFData;
  Kill: boolean;
begin
  Result := 0;
  MMFHandle := OpenMMF(MMFName);
  if MMFHandle <> 0 then
  begin
    MMFData := MapMMF(MMFHandle);
    if MMFData <> nil then
    begin
      if (Code < 0) or (wParam = PM_NOREMOVE) then
        {
          The CallNextHookEx function passes the hook information to the
          next hook procedure in the current hook chain.
        }
        Result := CallNextHookEx(MMFData.NextHook, Code, wParam, lParam)
      else
      begin
        Kill := False;

        { Examples }
        with TMsg(Pointer(lParam)^) do
        begin
          // Kill Numbers
          if (wParam >= 48) and (wParam <= 57) then Kill := True;
          // Kill Tabulator
          if (wParam = VK_TAB) then Kill := True;
        end;

        { Example to disable all the start-Key combinations }
        case TPMsg(lParam)^.message of
          WM_SYSCOMMAND: // The Win Start Key (or Ctrl+ESC)
            if TPMsg(lParam)^.wParam = SC_TASKLIST then Kill := True;

          WM_HOTKEY:
            case ((TPMsg(lParam)^.lParam and $00FF0000) shr 16) of
              VK_D,      // Win+D        ==> Desktop
              VK_E,      // Win+E        ==> Explorer
              VK_F,      // Win+F+(Ctrl) ==> Find:All (and Find: Computer)
              VK_M,      // Win+M        ==> Minimize all
              VK_R,      // Win+R        ==> Run program.
              VK_F1,     // Win+F1       ==> Windows Help
              VK_PAUSE:  // Win+Pause    ==> Windows system properties
                Kill := True;
            end;
        end;
        if Kill then TPMsg(lParam)^.message := WM_NULL;
        Result := CallNextHookEx(MMFData.NextHook, Code, wParam, lParam)
      end;
      UnMapMMF(MMFData);
    end;
    CloseMMF(MMFHandle);
  end;
end;


initialization
  begin
    MMFHandle := 0;
    MMFData   := nil;
  end;

finalization
  FreeHook;
end.


glavni program glasi ovako

Code:

unit FrmMainU;

interface

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

const
  HookDemo = 'WINHOOK.dll';

const
  WM_HOOKCREATE = WM_USER + 300;

type
  TFrmMain = class(TForm)
    Panel1: TPanel;
    BtnSetHook: TButton;
    BtnClearHook: TButton;
    procedure BtnSetHookClick(Sender: TObject);
    procedure BtnClearHookClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    FHookSet: Boolean;
    procedure EnableButtons;
  public

  end;

var
  FrmMain: TFrmMain;

function SetHook(WinHandle: HWND; MsgToSend: Integer): Boolean; stdcall;
  external HookDemo;

function FreeHook: Boolean; stdcall; external HookDemo;

implementation

{$R *.DFM}

procedure TFrmMain.EnableButtons;
begin
  BtnSetHook.Enabled   := not FHookSet;
  BtnClearHook.Enabled := FHookSet;
end;

// Start the Hook
procedure TFrmMain.BtnSetHookClick(Sender: TObject);
begin
  FHookSet := LongBool(SetHook(Handle, WM_HOOKCREATE));
  EnableButtons;
end;

// Stop the Hook
procedure TFrmMain.BtnClearHookClick(Sender: TObject);
begin
  FHookSet := FreeHook;
  EnableButtons;
  BtnClearHook.Enabled := False;
end;

procedure TFrmMain.FormCreate(Sender: TObject);
begin
  EnableButtons;
end;

procedure TFrmMain.FormDestroy(Sender: TObject);
begin
  BtnClearHook.Click;
end;

end.


Ali bih višeo volio Keyboard Hook!
Hvala
[ borovac @ 06.07.2005. 17:52 ] @
Našo sam odličan paket komponenti za različite hookove. Svi koriste jedan dll file. Za source tog dll-a treba platiti, a meni se baš i ne plaća!
Više bih volio source. Nadam se da će Srki_82 uspjeti u svojoj misiji.

Možda bi mogao i ovaj moj primjer proći, ali ne nešto ne radi kakda pokušam obraditi poruku, tj. pretvoriti poruku u tipke!
[ Nemanja Avramović @ 06.07.2005. 18:56 ] @
Citat:
borovac: Našo sam odličan paket komponenti za različite hookove. Svi koriste jedan dll file. Za source tog dll-a treba platiti, a meni se baš i ne plaća!


daj napisi koji je to DLL i gde ga mozemo skinuti?
[ Srki_82 @ 06.07.2005. 21:34 ] @
Smilovao sam se i evo instaliram Visual Studio. Za par dana cu verovatno pronaci sve sto mi treba da dodal u Delphi WH_KEYBOARD_LL... pozelite mi srecu
[ Passwd @ 06.07.2005. 21:46 ] @
Citat:
Srki_82: ........pozelite mi srecu


Sretno

[Ovu poruku je menjao Passwd dana 06.07.2005. u 22:46 GMT+1]
[ Srki_82 @ 07.07.2005. 21:07 ] @
Evo unita koji ce vam omoguciti da koristite LowLevel Hooks u Delphiu.
Ako me ne bude mrzelo prevescu i onu hook funkciju sa C na Delphi, a mogao bi to i neko drugi da uradi
[ Nemanja Avramović @ 08.07.2005. 01:50 ] @
objasni kako koristiti to?
[ Srki_82 @ 08.07.2005. 06:41 ] @
Pa isto kao i bilo koji drugi hook :)

Kad pozivas SetWindowsHookEx postavis tip hook-a na WH_KEYBOARD_LL, LParam (poslednji parametar u hook funkciji) je pointer na strukturu tipa KBDLLHOOKSTRUCT pa radi sa tim podacima sta zelis :)

Tako isto je i sa WH_MOUSE_LL samo sto je LParam pointer na strukturu tipa MSLLHOOKSTRUCT.
[ Nemanja Avramović @ 08.07.2005. 12:05 ] @
e pa ja nikad nisam hookovao do sad tako da nem' pojma sta ti pricas ovde :D
ali snaci cu se (kao i uvek :P)
[ borovac @ 09.07.2005. 12:56 ] @
Evo ba onaj primjer za deaktiviranje nekih windows ključeva.
A evo link za onaj HookPack
[url]http://www.bitlogic.co.uk/downloads/hookpack.zip[/url]
[ Nemanja Avramović @ 09.07.2005. 23:19 ] @
hvala, nadam se da cu skontati neshto iz toga :)
[ borovac @ 10.07.2005. 15:58 ] @
Code:

{ Keyboard LowLevel structure }
  PKbdLLHookStruct = ^TKbdLLHookStruct;
  tagKBDLLHOOKSTRUCT = packed record
    vkCode: DWORD;
    scanCode: DWORD;
    flags: DWORD;
    time: DWORD;
    dwExtraInfo: ULONG_PTR;
  end;


Code:
function KeyHookFunc(Code, VirtualKey, KeyStroke: integer): LRESULT; stdcall;

gore u onoj strukturi vkCode je vjerovatno isto što i VirtualKey
Koje do promjenjivih iz gornje strukture odgovaraju promjenljivim Code i KeyStroke??

Ako sam dobro skonto ovako to trreba da ide:
Code:
function KeyHookFunc(Code, wParam:integer; LParam: KBDLLHOOKSTRUCT): LRESULT; stdcall;

[ Srki_82 @ 10.07.2005. 16:36 ] @
KBDLLHOOKSTRUCT
The KBDLLHOOKSTRUCT structure contains information about a low-level keyboard input event.

typedef struct tagKBDLLHOOKSTRUCT {
DWORD vkCode;
DWORD scanCode;
DWORD flags;
DWORD time;
ULONG_PTR dwExtraInfo;
} KBDLLHOOKSTRUCT, *PKBDLLHOOKSTRUCT;

Members

vkCode
Specifies a virtual-key code. The code must be a value in the range 1 to 254.

scanCode
Specifies a hardware scan code for the key.

flags
Specifies the extended-key flag, event-injected flag, context code, and transition-state flag. This member is specified as follows.
0 - Specifies whether the key is an extended key, such as a function key or a key on the numeric keypad. The value is 1 if the key is an extended key; otherwise, it is 0.
1-3 - Reserved.
4 - Specifies whether the event was injected. The value is 1 if the event was injected; otherwise, it is 0.
5 - Specifies the context code. The value is 1 if the ALT key is pressed; otherwise, it is 0.
6 - Reserved.
7 - Specifies the transition state. The value is 0 if the key is pressed and 1 if it is being released.

An application can use the following values to test the keystroke flags. Value Purpose
LLKHF_EXTENDED Test the extended-key flag.
LLKHF_INJECTED Test the event-injected flag.
LLKHF_ALTDOWN Test the context code.
LLKHF_UP Test the transition-state flag.

time
Specifies the time stamp for this message.

dwExtraInfo
Specifies extra information associated with the message.

Moze ovako da ide
Code:
function KeyHookFunc(Code, wParam:integer; LParam: PKbdLLHookStruct): LRESULT; stdcall;