diff --git a/lcl/interfaces/cocoa/cocoamenus.pas b/lcl/interfaces/cocoa/cocoamenus.pas index 25f4d7e5b0..3353d66a3a 100644 --- a/lcl/interfaces/cocoa/cocoamenus.pas +++ b/lcl/interfaces/cocoa/cocoamenus.pas @@ -72,6 +72,7 @@ procedure MenuTrackCancelAll; // the returned "Key" should not be released, as it's not memory owned procedure ShortcutToKeyEquivalent(const AShortCut: TShortcut; out Key: NSString; out shiftKeyMask: NSUInteger); +function LCLMenuItemInit(item: NSMenuItem; const lclMenuItem: TMenuItem): id; function LCLMenuItemInit(item: NSMenuItem; const atitle: string; ashortCut: TShortCut): id; function LCLMenuItemInit(item: NSMenuItem; const atitle: string; VKKey: Word = 0; State: TShiftState = []): id; function ToggleAppMenu(ALogicalEnabled: Boolean): Boolean; @@ -194,6 +195,41 @@ begin ANSMenuItem.setState( menustate[Checked] ); end; +function getHotkeyFromTitle( aTitle:String ): Word; +var + i: Integer; + hotkeyChar: Char; +begin + Result:= 0; + i:= aTitle.IndexOf( cHotkeyPrefix ); + if (i<0) or (i>=aTitle.Length-1) then + Exit; + + hotkeyChar:= aTitle.Chars[i+1]; + if hotkeyChar <> cHotkeyPrefix then + Result:= Word( UpCase(hotkeyChar) ); +end; + +function LCLMenuItemInit(item: NSMenuItem; const lclMenuItem: TMenuItem): id; +var + aShortCut: TShortCut; + aTitle: String; + key: Word; +begin + aTitle := lclMenuItem.Caption; + aShortCut := lclMenuItem.ShortCut; + + if aShortCut=0 then begin + if not Assigned(lclMenuItem.Action) then begin + key:= getHotkeyFromTitle( aTitle ); + if key<>0 then + aShortCut:= ShortCut( key, [] ); + end; + end; + + Result:= LCLMenuItemInit(item, aTitle, aShortCut); +end; + function LCLMenuItemInit(item: NSMenuItem; const atitle: string; ashortCut: TShortCut): id; var key : NSString; diff --git a/lcl/interfaces/cocoa/cocoawsmenus.pas b/lcl/interfaces/cocoa/cocoawsmenus.pas index c22dce416b..d50150bb29 100644 --- a/lcl/interfaces/cocoa/cocoawsmenus.pas +++ b/lcl/interfaces/cocoa/cocoawsmenus.pas @@ -268,7 +268,7 @@ begin end else begin - item := LCLMenuItemInit(TCocoaMenuItem.alloc, AMenuItem.Caption, AMenuItem.ShortCut); + item := LCLMenuItemInit(TCocoaMenuItem.alloc, AMenuItem); TCocoaMenuItem(item).FMenuItemTarget := AMenuItem; if AMenuItem.IsInMenuBar then