Recent

Author Topic: KeyDown and value of Shift on Mac  (Read 8301 times)

Bart

  • Hero Member
  • *****
  • Posts: 5743
    • Bart en Mariska's Webstek
KeyDown and value of Shift on Mac
« on: September 01, 2011, 07:06:14 pm »
Hi,

A question on KeyDown on Mac:

Code: [Select]
procedure TSomeEditControl.KeyDown(var Key: Word; Shift: TShiftState);
begin
 if (Key = VK_SomeKey) and (Shift = [ssCtrl]) then
 begin// Ctrl+SomeKey
   DoSomething;
   Key := 0;
 end;
end;

This will do fine on PC's and I thought it would work with Cmd (Meta?, don't know how it's called)+SomeKey on Mac, but is this really the case?
(Actually SomeKey is VK_C, VK_V or VK_X in this case.)

If it is not, should I write widgetsetimplementations for all controls
that look for Ctrl-key in KeyDown()?

I do not have access to a Mac, so I cannot test this myself.

Bart

jwdietrich

  • Hero Member
  • *****
  • Posts: 1278
    • formatio reticularis
Re: KeyDown and value of Shift on Mac
« Reply #1 on: September 01, 2011, 10:13:29 pm »
On the Mac simply write Shift = [ssMeta].

In order to make this cross-platform you could first define the procedure

Code: [Select]
procedure AdaptModifiers;
var
  modifierKey: TShiftState;
begin
  {$IFDEF LCLcarbon}
    modifierKey := [ssMeta];
  {$ELSE}
    modifierKey := [ssCtrl];
  {$ENDIF}
end;

and call it early in the application. Then your KeyDown procedure may be changed to:

Code: [Select]
procedure TSomeEditControl.KeyDown(var Key: Word; Shift: TShiftState);
begin
 if (Key = VK_SomeKey) and (Shift = modifierKey) then
 begin// Ctrl+SomeKey
   DoSomething;
   Key := 0;
 end;
end;

Certainly, you may also use the variable modifierKey for other tasks, e. g. adapting your menu shortcuts depending on the platform.
function GetRandomNumber: integer; // xkcd.com
begin
  GetRandomNumber := 4; // chosen by fair dice roll. Guaranteed to be random.
end;

http://www.formatio-reticularis.de

Lazarus 4.2.0 | FPC 3.2.2 | PPC, Intel, ARM | macOS, Windows, Linux

Bart

  • Hero Member
  • *****
  • Posts: 5743
    • Bart en Mariska's Webstek
Re: KeyDown and value of Shift on Mac
« Reply #2 on: September 02, 2011, 06:58:25 pm »
This seems doable.
What conditional define should I use to cover all Mac's (Classic and OS X)?

{$ifdef darwin}, {$ifdef lclcarbon}, {$if somethingelse}, and if possible as a oneliner?

Bart

jwdietrich

  • Hero Member
  • *****
  • Posts: 1278
    • formatio reticularis
Re: KeyDown and value of Shift on Mac
« Reply #3 on: September 02, 2011, 09:35:42 pm »
{$ifdef darwin} should work, too. However, I have no experience with the Cocoa Widgetset. Classic is not supported by Lazarus (old Free Pascal versions can be used to build Classic applications).
function GetRandomNumber: integer; // xkcd.com
begin
  GetRandomNumber := 4; // chosen by fair dice roll. Guaranteed to be random.
end;

http://www.formatio-reticularis.de

Lazarus 4.2.0 | FPC 3.2.2 | PPC, Intel, ARM | macOS, Windows, Linux

Bart

  • Hero Member
  • *****
  • Posts: 5743
    • Bart en Mariska's Webstek
Re: KeyDown and value of Shift on Mac
« Reply #4 on: September 02, 2011, 11:02:43 pm »
Thanks for explaining that.
I was under the impression that Lazarus could build for Mac OS Classic as well.

Bart

 

TinyPortal © 2005-2018