Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > Delphi: Для новичков > hook в dll


Автор: AlexeyShestchenko 19.2.2008, 17:59
можно ли каким-нибудь образом осуществить перехват клавиш непосредственно в самой dll, т.е. только когда dll плагин загружен и нажать на определенное сочетание клавиш то происходили бы нужные мне операции находящиеся в dll плагине?

Автор: dumb 19.2.2008, 19:05
никаких проблем: ставишь хук(WH_KEYBOARD_LL) в DLL_PROCESS_ATTACH, в DLL_PROCESS_DETACH - снимаешь.

Автор: AlexeyShestchenko 22.2.2008, 02:45
нашел тему где то на винграде и заделал под себя, но проблема в том что как только зажимаю ctrl+mouse1, то бесконечно много вылезает мессаг, мне надо чтобы одно нажатие, означало один вызов моего условия, помогите плз:

Код

library HookDLL;

uses
  SysUtils,
  Classes,
  Windows,
  Messages,
  dialogs;

const
  WM_SETFORE = WM_USER + 101;

var
  HKbdHook: HHook;
  MForm: HWND;

{$R *.res}

//------------------------------------------------------------------------------
// Hook
//------------------------------------------------------------------------------
function KeyboardProc(nCode: Integer; WP: wParam; LP: lParam): integer; stdcall;
begin
  Result := CallNextHookEx(HKbdHook, nCode, WP, LP);
  if nCode = HC_ACTION then
  begin
    //if (WP = 46) and (HI(GetKeyState(VK_CONTROL)) = 255) and (HI(GetKeyState(VK_MENU)) = 255) then
    if (HI(GetKeyState(VK_CONTROL)) = 255) and (HI(GetKeyState(VK_LBUTTON)) = 255) then
    begin
      ShowMessage('GGG');
     // SetforeGroundWindow(MFOrm);
      //SendMessage(MForm, WM_SETFORE, 0, 0);
    end;
  end;
end;

//------------------------------------------------------------------------------

procedure Hook(Flag: boolean; Handl: HWND); export; stdcall;
Begin
  MForm := Handl;
  if Flag then
    HKbdHook := SetWindowsHookEx(WH_KEYBOARD, @KeyboardProc, HInstance, 0)
  else begin
    UnhookWindowsHookEx(HKbdHook);
    HKbdHook := 0;
  end;
end;

exports
  Hook;

begin
end.


Код

interface

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

const
  WM_SETFORE = WM_USER + 101;

type
  TKeyboardProc = procedure (Flag: boolean; Handl: HWND); stdcall;
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
      protected
    procedure HotKeyReleased(var Message: TWmHotKey); message WM_HOTKEY;
    procedure WMSETFORE(var Message: TMsg); message WM_SETFORE;
  end;

var
  Form1: TForm1;
    HKey: integer;
  HKbdHook: integer;
  HookProc: TKeyboardProc;
  HookDLL: HModule;
  UniqKey: string;
  MsgId: integer;
implementation

{$R *.dfm}
//------------------------------------------------------------------------------
{ * * * * * * * * * * * * * * * * TfrmMain * * * * * * * * * * * * * * * * * * }
//------------------------------------------------------------------------------
// Îáðàáîò÷èê ãîðÿ÷åé êëàâèøè
//------------------------------------------------------------------------------
procedure Tform1.HotKeyReleased(var Message: TWmHotKey);
begin
  if Message.HotKey = HKey then
  begin
    SetForeGroundWindow(Handle);
    ShowMessage('Alt+Tab pressed!');
  end;
end;

//------------------------------------------------------------------------------

procedure Tform1.WMSETFORE(var Message: TMsg);
begin
  SetForeGroundWindow(Handle);
  ShowMessage('Ctrl+Alt+Del pressed!');
end;

//------------------------------------------------------------------------------

procedure Tform1.FormCreate(Sender: TObject);
begin
  HKey := GlobalAddAtom('MyHotKey');
  RegisterHotKey(Handle, HKey, MOD_ALT, 9);
  HookDLL := LoadLibrary('HookDLL.dll');
  @HookProc := getProcAddress(HookDLL, 'Hook');
  HookProc(True, Handle);
end;

//------------------------------------------------------------------------------




procedure TForm1.FormDestroy(Sender: TObject);
begin
  UnRegisterHotKey(Handle, HKey);
  HookProc(False, Handle);
  FreeLibrary(HOOKDLL);
end;


Автор: MetalFan 22.2.2008, 10:54
читай библию (aka MSDN).
зачем ты используешь GetKeyState???
Цитата(MSDN)
KeyboardProc Function
...
wParam
[in] Specifies the virtual-key code of the key that generated the keystroke message.
lParam
[in] Specifies the repeat count, scan code, extended-key flag, context code, previous key-state flag, and transition-state flag. For more information about the lParam parameter, see Keystroke Message Flags. This parameter can be one or more of the following values. 
0-15
Specifies the repeat count. The value is the number of times the keystroke is repeated as a result of the user's holding down the key.
16-23
Specifies the scan code. The value depends on the OEM.
24
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.
25-28
Reserved.
29
Specifies the context code. The value is 1 if the ALT key is down; otherwise, it is 0.
30
Specifies the previous key state. The value is 1 if the key is down before the message is sent; it is 0 if the key is up.
31
Specifies the transition state. The value is 0 if the key is being pressed and 1 if it is being released.


Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)