Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2010-11

HotKey with Carbon

6 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Eugen Bolz

Hey,

i tried to port this
http://dbachrach.com/blog/2005/11/program-global-hotkeys-in-cocoa-easily/ .

Nothing Happens If I press Option + 1.

Here's my code, I hope anybody can help me with this :) :

{ Based on
http://dbachrach.com/blog/2005/11/program-global-hotkeys-in-cocoa-easily/ }

unit Unit1;
{$mode objfpc}{$H+}
interface

uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,

   MacOSAll;
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;
implementation
uses
   LCLType, CarbonUtils, CarbonDef, CarbonProc;
{$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, 1,
     @eventType, nil, nil), 'Install'); // 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;
end.

Reply by email

Eugen Bolz
to dmitry boyarintsev
dmitry boyarintsev wrote 6 lines
I think you need to create UPP handler first, instead of passing the function pointer to the InstallApplicationEventHandler() thanks, dmitry

Isn't this function the handler?

function MAC_OS_Handler(ANextHandler: EventHandlerCallRef;
   AEvent: EventRef;
   inUserData: UnivPtr ): OSStatus; {$IFDEF darwin}mwpascal;{$ENDIF}

I'm really new to Mac and Carbon, so currently no know-how on this area :(

If this isn't the UPP Handler, could you show me one?

Best Regards
Eugen

Reply by email

dmitry boyarintsev
to Eugen Bolz

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

Reply by email

dmitry boyarintsev
to Eugen Bolz
Eugen Bolz wrote
Using -Sa and deleted the Assert-function and

This is FPC feature, rather than Lazarus or Carbon specific issue. With -Sa switch you can be sure that Assert() function code is compiled, otherwise the expression, given at Assert() function, might not be compiled. In your case, the program did not install the handler.

this UPP_Handler it finally works :D

Whenever you meet Carbon events, you should use UPP handlers, instead of direct function pointers.

thanks,
Dmitry

Reply by email