Change Shortcut on MacOS for ‘Insert’ key

  

EDIT
When converting a program with 100+ forms. Some of them have popupmenus with the ‘Insert’ key as shortcut. This key does not excists on a Mac Keyboard, or even the Mac OS.

So we need to change all these shortcuts when deploying for mac.

Solution

Suggested by David Hefferman : When Streaming a Form, loop through all actions and shortcuts and then change the shortcut.
Change key handling of new shortcut to map to the old one (in the form). And change the shortcut when the popupmenu is showing, to show the new shortcut.

code

TMenuItem = class(FMX.Menus.TMenuItem)
protected
procedure ApplyStyle; override;
procedure DialogKey(var Key: Word; Shift: TShiftState); override;
end;

TForm = class(FMX.Forms.TForm)
public
procedure KeyDown(var Key: Word; var KeyChar: System.WideChar; Shift: TShiftState); override;
end;

procedure TMenuItem.ApplyStyle;
begin
inherited;
//When the popupmenu is shown, make shure that the shortcut is showing the new one
if self.ShortCut=45 then
self.ShortCut := 16462; //new shortcut (Ctrl+N)
end;

procedure TMenuItem.DialogKey(var Key: Word; Shift: TShiftState);
begin
if (key = 45) AND (ShortCut = 16462) then begin //form has passed old shortcut so change it back
ShortCut:= 45;
inherited;
ShortCut := 16462; //revert to new
end else begin
inherited;
end;
if self.ShortCut=45 then //if the shortcut was still mapped to old shortcut, map to new
self.ShortCut := 16462;
end;

procedure TForm.KeyDown(var Key: Word; var KeyChar: System.WideChar; Shift: TShiftState);
begin
//new Shortcut is ctrl+N
if (key=vkN) AND (ssCtrl in Shift) then begin
key := 45; //change to the code for insert
Shift := shift – [ssCTRL]; //remove Ctrl
end;
inherited;
end;

Comments are closed.