{
  This library is free software; you can redistribute it and/or modify it
  under the terms of the GNU Library General Public License as published by
  the Free Software Foundation; either version 2 of the License, or (at your
  option) any later version with the following modification:

  As a special exception, the copyright holders of this library give you
  permission to link this library with independent modules to produce an
  executable, regardless of the license terms of these independent modules,and
  to copy and distribute the resulting executable under terms of your choice,
  provided that you also meet, for each linked independent module, the terms
  and conditions of the license of that module. An independent module is a
  module which is not derived from or based on this library. If you modify
  this library, you may extend this exception to your version of the library,
  but you are not obligated to do so. If you do not wish to do so, delete this
  exception statement from your version.

  This program is distributed in the hope that it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
  for more details.

  You should have received a copy of the GNU Library General Public License
  along with this library; if not, write to the Free Software Foundation,
  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
}

unit HotKey;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, LCLType, fgl, Dialogs,
  {$IFDEF Unix}
    {$IFDEF Darwin}
  // Mac
  MacOSAll,  CarbonUtils, CarbonDef, CarbonProc
    {$ELSE}
  // Unix

    {$ENDIF}
  {$ELSE}
  // Windows
  Windows
  {$ENDIF};

type
  THotKeyEvent = procedure(Key, Option: Word) of object;

  { TNotifyClass }

  TNotifyClass = class
  public
    Notify      : THotKeyEvent;
    Key, Option : Word;
    {$IFDEF Darwin}
    HotKeyRef : EventHotKeyRef;
    {$ENDIF}

    procedure DoNotify;
  end;

  { TNotifyList }

  TNotifyList = specialize TFPGObjectList<TNotifyClass>;

  { THotKey }

  THotKey = class
  private
  {$IFDEF Unix}
    {$IFDEF Darwin}
    gMyHotKeyID        : EventHotKeyID;
    {$ELSE}

    {$ENDIF}
  {$ELSE}
    FHandle: HWND;
  {$ENDIF}
  public
    constructor Create{$IFDEF MSWINDOWS} (AHandle: HWND){$ENDIF};
    function RegisterHotKey(Key, Options: Word; AEvent: THotKeyEvent) : Integer;
    procedure UnRegisterHotKey(AIndex: Integer);
  end;



implementation

uses
  Forms;

var
  TheList: TNotifyList;
  {$IFDEF MSWINDOWS} PrevWndProc : Long_ptr; {$ENDIF}

{ TNotifyClass }

procedure TNotifyClass.DoNotify;
begin
  Notify(Key, Option);
end;

{ THotKey }

function NotifyExists(Key, Option: Word): Boolean;
var
  i       : Integer;
  tempRec : TNotifyClass;
begin
  Result := false;
  for i := 0 to TheList.Count-1 do
  begin
    tempRec := TNotifyClass(TheList.Items[i]);
    if (tempRec.Key = Key) AND (tempRec.Option = Option) then
      Result := true;
  end;
end;

{$IFDEF UNIX}
{$IFDEF Darwin}
var
  Mac_OS_Handler_UPP : EventHandlerUPP = nil;

function MAC_OS_Handler(ANextHandler: EventHandlerCallRef;
  AEvent: EventRef;
  inUserData: UnivPtr ): OSStatus; mwpascal;
var
  hkCom: EventHotKeyID;
  l : Integer;
begin
  GetEventParameter(AEvent, kEventParamDirectObject, typeEventHotKeyID, nil,
    SizeOf(hkCom), nil, @hkCom);
  l := hkCom.id;

  if l <= TheList.Count then
  begin
    TNotifyClass(TheList.Items[l]).DoNotify;

    Application.ProcessMessages;
  end;

  Result := noErr;
end;

function THotKey.RegisterHotKey(Key, Options: Word; AEvent: THotKeyEvent): Integer;
var
  eventType : EventTypeSpec;
  tempKey   : Word;
  Notify : TNotifyClass;
begin
  if NotifyExists(Key, Options) then Exit;

  Notify := TNotifyClass.Create;
  Notify.Key:= Key;
  Notify.Option:= Options;
  Notify.Notify:=AEvent;

  eventType.eventClass:= kEventClassKeyboard;
  eventType.eventKind:= kEventHotKeyPressed;

  InstallApplicationEventHandler(Mac_OS_Handler_UPP, 1, @eventType,
    nil, nil);

  gMyHotKeyID.id := TheList.Add(Notify);
  gMyHotKeyID.signature := MakeFourCC(LeftStr(IntToStr(gMyHotKeyID.id)+'htk', 4));

  tempKey := VirtualKeyCodeToMac(Key);

  if (Key <> 0) and (tempKey = 0) then
  begin
    // VK_A isn't the same on Mac
    case Key of
      VK_A: tempKey := 0;
      VK_B: tempKey := 11;
      VK_C: tempKey := 8;
      VK_D: tempKey := 2;
      VK_E: tempKey := 14;
      VK_F: tempKey := 3;
      VK_G: tempKey := 5;
      VK_H: tempKey := 4;
      VK_I: tempKey := 34;
      VK_J: tempKey := 38;
      VK_K: tempKey := 40;
      VK_L: tempKey := 37;
      VK_M: tempKey := 46;
      VK_N: tempKey := 45;
      VK_O: tempKey := 31;
      VK_P: tempKey := 35;
      VK_Q: tempKey := 12;
      VK_R: tempKey := 15;
      VK_S: tempKey := 1;
      VK_T: tempKey := 17;
      VK_U: tempKey := 32;
      VK_V: tempKey := 9;
      VK_W: tempKey := 13;
      VK_X: tempKey := 7;
      VK_Y: tempKey := 6;
      VK_Z: tempKey := 16;
      VK_0: tempKey := 29;
      VK_1: tempKey := 18;
      VK_2: tempKey := 19;
      VK_3: tempKey := 20;
      VK_4: tempKey := 21;
      VK_5: tempKey := 23;
      VK_6: tempKey := 22;
      VK_7: tempKey := 26;
      VK_8: tempKey := 28;
      VK_9: tempKey := 29;
      else tempKey := Key;
    end;
  end;

  OSError(RegisterEventHotKey(tempKey, Options, gMyHotKeyID,
    GetApplicationEventTarget, 0, Notify.HotKeyRef),
  'Register' ,''); // No Error
end;

procedure THotKey.UnRegisterHotKey(AIndex: Integer);
begin
  UnregisterEventHotKey(TNotifyClass(TheList.Items[AIndex]).HotKeyRef);

  TheList.Items[AIndex] := nil;
end;
{$ELSE}
// Unix

{$ENDIF}
{$ELSE}
// Windows
function MsgProc(Handle: HWnd; Msg: UInt; WParam: Windows.WParam; LParam: Windows.LParam): LResult; stdcall;
var
  i : Integer;
begin
if Msg = WM_HOTKEY then
begin
  i := uint(WParam);

  if i <= TheList.Count then
  begin
    TNotifyClass(TheList.Items[i]).DoNotify;

    Application.ProcessMessages;
  end;
end;
Result := Windows.CallWindowProc(WNDPROC(PrevWndProc), Handle, Msg, WParam, LParam);
end;

function THotKey.RegisterHotKey(Key, Options: Word; AEvent: THotKeyEvent): Integer;
var
  Notify: TNotifyClass;
begin
  if NotifyExists(Key, Options) then Exit;

  Notify := TNotifyClass.Create;
  Notify.Key := Key;
  Notify.Option := Options;
  Notify.Notify := AEvent;

  Windows.RegisterHotKey(FHandle, TheList.Add(Notify), Options, Key);
end;

procedure THotKey.UnRegisterHotKey(AIndex: Integer);
begin
  Windows.UnregisterHotKey(FHandle, AIndex);

  TheList.Items[AIndex] := nil;
end;
{$ENDIF}

constructor THotKey.Create{$IFDEF MSWINDOWS} (AHandle: HWND){$ENDIF};
begin
  {$IFDEF MSWINDOWS}
    FHandle := AHandle;

    PrevWndProc := Windows.GetWindowLongPtr(FHandle, GWL_WNDPROC);
    SetWindowLongPtr(FHandle,Windows.GWL_WNDPROC,Long_ptr(@MsgProc));
  {$ENDIF}
end;

initialization
  TheList := TNotifyList.Create(true);
  {$IFDEF Darwin} Mac_OS_Handler_UPP:=NewEventHandlerUPP (@MAC_OS_Handler); {$ENDIF}

finalization
  TheList.Clear;
  TheList.Free;
  {$IFDEF Darwin} DisposeEventHandlerUPP(Mac_OS_Handler_UPP);  {$ENDIF}

end.

