Skip to content

Commit 5aaa0eb

Browse files
lucasalberticlaude
authored andcommitted
fix: use Cmd instead of Ctrl for default keyboard shortcuts on macOS
On Cocoa, ssCtrl is the physical Control key, while the Command key arrives as ssMeta. All default shortcuts of actions and of the SynEdit keystrokes use ssCtrl, so Cmd+C/V/X/A/Z did nothing in the query editor and other SynEdits. Move default Ctrl+letter/digit shortcuts to Cmd on macOS, before customized shortcuts are applied. Q, H, M and W are left alone, as Cmd+Q/H/M/W are reserved for app and window handling. The shortcut editor in the preferences dialog has no checkbox for Cmd, so it now keeps that modifier instead of silently dropping it when leaving an item. Closes #2369 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 1363b16 commit 5aaa0eb

2 files changed

Lines changed: 51 additions & 2 deletions

File tree

‎source/main.pas‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1866,6 +1866,25 @@ procedure TMainForm.FormDestroy(Sender: TObject);
18661866
AppSettings.Free;
18671867
end;
18681868

1869+
{$IFDEF DARWIN}
1870+
function CtrlToCmdShortCut(ShortCut: TShortCut): TShortCut;
1871+
var
1872+
Key: Word;
1873+
Shift: TShiftState;
1874+
begin
1875+
// On Cocoa, ssCtrl is the physical Control key, while the Command key arrives as ssMeta.
1876+
// Move Ctrl+letter/digit shortcuts to Cmd, as macOS users expect Cmd+C/V/X/A/Z etc.
1877+
// Q, H, M and W are left alone, as Cmd+Q/H/M/W are reserved for app and window handling.
1878+
Result := ShortCut;
1879+
ShortCutToKey(ShortCut, Key, Shift);
1880+
if (ssCtrl in Shift)
1881+
and (not (ssMeta in Shift))
1882+
and (Key in [VK_0..VK_9, VK_A..VK_Z])
1883+
and (not (Key in [VK_Q, VK_H, VK_M, VK_W])) then
1884+
Result := KeyToShortCut(Key, Shift - [ssCtrl] + [ssMeta]);
1885+
end;
1886+
{$ENDIF}
1887+
18691888
procedure TMainForm.FormCreate(Sender: TObject);
18701889
var
18711890
i, j, MonitorIndex: Integer;
@@ -2097,6 +2116,22 @@ procedure TMainForm.FormCreate(Sender: TObject);
20972116
actExitApplication.Caption := 'E&xit';
20982117
actExitApplication.ShortCut := KeyToShortCut(VK_F4, [ssAlt]);
20992118
{$ENDIF}
2119+
{$IFDEF DARWIN}
2120+
// Default shortcuts use Cmd instead of Ctrl, before customized ones are applied
2121+
for i:=0 to ActionList1.ActionCount-1 do begin
2122+
Action := TAction(ActionList1.Actions[i]);
2123+
Action.ShortCut := CtrlToCmdShortCut(Action.ShortCut);
2124+
end;
2125+
for i:=0 to SynMemoQuery.Keystrokes.Count-1 do begin
2126+
try
2127+
SynMemoQuery.Keystrokes[i].ShortCut := CtrlToCmdShortCut(SynMemoQuery.Keystrokes[i].ShortCut);
2128+
SynMemoQuery.Keystrokes[i].ShortCut2 := CtrlToCmdShortCut(SynMemoQuery.Keystrokes[i].ShortCut2);
2129+
except
2130+
on E:ESynKeyError do
2131+
LogSQL(E.Message, lcDebug);
2132+
end;
2133+
end;
2134+
{$ENDIF}
21002135
// Customized keyboard shortcuts
21012136
for i:=0 to ActionList1.ActionCount-1 do begin
21022137
Action := TAction(ActionList1.Actions[i]);

‎source/preferences.pas‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1354,14 +1354,28 @@ procedure TfrmPreferences.TreeShortcutItemsFocusChanging(
13541354
Data: PShortcutItemData;
13551355
NewShortCut: TShortCut;
13561356
Shift: TShiftState;
1357+
1358+
function UncheckableModifiers(OldShortCut: TShortCut; NewKey: Word): TShiftState;
1359+
var
1360+
OldKey: Word;
1361+
begin
1362+
// Keep modifiers which have no checkbox here, e.g. Cmd (ssMeta) on macOS,
1363+
// unless the shortcut gets removed
1364+
ShortCutToKey(OldShortCut, OldKey, Result);
1365+
if NewKey = VK_UNKNOWN then
1366+
Result := []
1367+
else
1368+
Result := Result * [ssMeta];
1369+
end;
1370+
13571371
begin
13581372
// Check if shortcut 1 or 2 changed
13591373
if (not Assigned(OldNode)) or (TreeShortcutItems.GetNodeLevel(OldNode) = 0) then
13601374
Exit;
13611375
Data := TreeShortcutItems.GetNodeData(OldNode);
13621376
Allowed := True;
13631377

1364-
Shift := [];
1378+
Shift := UncheckableModifiers(Data.ShortCut1, VKcodes[comboShortcut1Key.ItemIndex].Code);
13651379
if chkShortcut1Shift.Checked then Include(Shift, ssShift);
13661380
if chkShortcut1Alt.Checked then Include(Shift, ssAlt);
13671381
if chkShortcut1Control.Checked then Include(Shift, ssCtrl);
@@ -1376,7 +1390,7 @@ procedure TfrmPreferences.TreeShortcutItemsFocusChanging(
13761390
end;
13771391

13781392
if chkShortcut2Shift.Enabled then begin
1379-
Shift := [];
1393+
Shift := UncheckableModifiers(Data.ShortCut2, VKcodes[comboShortcut2Key.ItemIndex].Code);
13801394
if chkShortcut2Shift.Checked then Include(Shift, ssShift);
13811395
if chkShortcut2Alt.Checked then Include(Shift, ssAlt);
13821396
if chkShortcut2Control.Checked then Include(Shift, ssCtrl);

0 commit comments

Comments
 (0)