Тема: компонент
Показать сообщение отдельно
  #3  
Старый 17.12.2012, 03:19
Аватар для poli-smen
poli-smen poli-smen вне форума
Профессионал
 
Регистрация: 06.08.2012
Адрес: Кривой Рог
Сообщения: 1,791
Версия Delphi: Delphi 7, XE2
Репутация: 4415
По умолчанию

Gudzik11, извини не заметил, что тема в разделе "Код на шару!".
Вот тебе полностью рабочий компонент:
Код:
unit HotKeyComponent;

interface

uses
  Windows, Messages, SysUtils, Classes, Dialogs;

type
  THotKeyComponent = class(TComponent)
  private
    id1, id2, id3, id4: Integer;
    FWnd: HWND;
    procedure WndProc(var Msg: TMessage);
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('Samples', [THotKeyComponent]);
end;

{ THotKeyComponent }

constructor THotKeyComponent.Create(AOwner: TComponent);
// Different (различные) Constants (константы) from (с модуля) Windows.pas
const
  MOD_ALT = 1;
  MOD_CONTROL = 2;
  MOD_SHIFT = 4;
  MOD_WIN = 8;
  VK_A = $41;
  VK_R = $52;
  VK_F4 = $73;
begin
  inherited;

  if not (csDesigning in ComponentState) then
  begin
    FWnd := AllocateHWnd(WndProc);

    // Register Hotkey Ctrl + A
    id1 := GlobalAddAtom('Hotkey1');
    RegisterHotKey(FWnd, id1, MOD_CONTROL, VK_A);

    // Register Hotkey Ctrl + Alt + R
    id2 := GlobalAddAtom('Hotkey2');
    RegisterHotKey(FWnd, id2, MOD_CONTROL + MOD_Alt, VK_R);

    // Register Hotkey Win + F4
    id3 := GlobalAddAtom('Hotkey3');
    RegisterHotKey(FWnd, id3, MOD_WIN, VK_F4);

    // Globally trap the Windows system key "PrintScreen"
    id4 := GlobalAddAtom('Hotkey4');
    RegisterHotKey(FWnd, id4, 0, VK_SNAPSHOT);
  end;
end;

destructor THotKeyComponent.Destroy;
begin
  if not (csDesigning in ComponentState) then
  begin
    UnRegisterHotKey(FWnd, id1);
    GlobalDeleteAtom(id1);
    UnRegisterHotKey(FWnd, id2);
    GlobalDeleteAtom(id2);
    UnRegisterHotKey(FWnd, id3);
    GlobalDeleteAtom(id3);
    UnRegisterHotKey(FWnd, id4);
    GlobalDeleteAtom(id4);

    DeallocateHWnd(FWnd);
  end;

  inherited;
end;

procedure THotKeyComponent.WndProc(var Msg: TMessage);
var
  WMHotKey: TWMHotKey absolute Msg;
begin
  case Msg.Msg of
    WM_HOTKEY:
      begin
        if WMHotKey.HotKey = id1 then
          ShowMessage('Ctrl + A pressed !');
        if WMHotKey.HotKey = id2 then
          ShowMessage('Ctrl + Alt + R pressed !');
        if WMHotKey.HotKey = id3 then
          ShowMessage('Win + F4 pressed !');
        if WMHotKey.HotKey = id4 then
          ShowMessage('Print Screen pressed !');
      end;
  else
    DefWindowProc(FWnd, Msg.Msg, Msg.WParam, Msg.LParam);
  end;
end;

end.
Ответить с цитированием