[Lazarus] HotKey with Carbon

dmitry boyarintsev skalogryz.lists at gmail.com
Sat Nov 6 04:10:30 CET 2010


Here you go!

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  MacOSAll, LCLType, CarbonUtils, CarbonDef, CarbonProc;

type

  { TForm1 }

  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;
  gMyHotKeyRef : EventHotKeyRef;
  gMyHotKeyID  : EventHotKeyID;
  Mac_OS_Handler_UPP : EventHandlerUPP = nil;

implementation

{$R *.lfm}

function MAC_OS_Handler(ANextHandler: EventHandlerCallRef;
  AEvent: EventRef;
  inUserData: UnivPtr ): OSStatus; {$IFDEF darwin}mwpascal;{$ENDIF}
begin
  ShowMessage('hot'); // No Message :(
  Result := CallNextEventHandler(ANextHandler, AEvent);
end;

{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);
var
 eventType    : EventTypeSpec;
begin
 eventType.eventClass:= kEventClassKeyboard;
 eventType.eventKind:= kEventHotKeyPressed;

 Assert(
 InstallApplicationEventHandler(Mac_OS_Handler_UPP, 1, @eventType,
nil, nil), 'error'); // No Error

 gMyHotKeyID.signature:=MakeFourCC('htk1');
 gMyHotKeyID.id:=1;

 OSError(
   RegisterEventHotKey(49, optionKey, gMyHotKeyID,
     GetApplicationEventTarget, 0, gMyHotKeyRef),
 'Register' ,''); // No Error
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
 UnregisterEventHotKey(gMyHotKeyRef);
end;

initialization
  Mac_OS_Handler_UPP:=NewEventHandlerUPP (@MAC_OS_Handler);

finalization
  DisposeEventHandlerUPP(Mac_OS_Handler_UPP);


copy paste the code. Compile.
If it doesn't work, go to Compiler Options and enable "Include
Asserting code (-Sa)" or get rid of the usage Assert() function.

regards,
Dmitry




More information about the Lazarus mailing list