Recent

Author Topic: How do I test for MacOS COMMAND key down?  (Read 347 times)

Dan3468298

  • Full Member
  • ***
  • Posts: 125
How do I test for MacOS COMMAND key down?
« on: August 04, 2020, 10:02:51 pm »
In KeyDown is the MacOS COMMAND key defined in a TShiftState?   I can't figure it out.  Any ideas?
MacOS 10.15.5/Lazarus 2.0.10 Build 2020-07-07

jamie

  • Hero Member
  • *****
  • Posts: 3518
Re: How do I test for MacOS COMMAND key down?
« Reply #1 on: August 04, 2020, 10:34:38 pm »
Not sure on a mac but maybe the GetKeyState in the LCLIntf unit will do it ?

If you know the Virtual key code of the MAC key you can do this during a Keydown event..

Code: Pascal  [Select][+][-]
  1. If GetKeyState(VK_MACKEY?) < 0 THen its pressed..
  2.  

Like I said, I don't know what the key code would be...
The only true wisdom is knowing you know nothing

dsiders

  • Sr. Member
  • ****
  • Posts: 372
Re: How do I test for MacOS COMMAND key down?
« Reply #2 on: August 04, 2020, 10:59:34 pm »
In KeyDown is the MacOS COMMAND key defined in a TShiftState?   I can't figure it out.  Any ideas?

I'm not a Mac user, so I don't know whether your version is based on Carbon or Cocoa.

For Carbon, the command key MK_COMMAND (sames as MK_APPLE) is mapped to VK_CONTROL. See lcl/interfaces/carbon/mackeycodes.inc and carbonproc.pp.

I don't see anything in the widgetset class for Cocoa.
Lazarus 2.1 (SVN) / FPC 3.0.4 / FPC 3.2.0 / x86-win64 / Windows 8.1

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2514
    • havefunsoft.com
Re: How do I test for MacOS COMMAND key down?
« Reply #3 on: August 04, 2020, 11:01:57 pm »
In KeyDown is the MacOS COMMAND key defined in a TShiftState?   I can't figure it out.  Any ideas?
it's ssMeta
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
  2. begin
  3.   if (ssMeta in Shift) then Caption:='Command is held'
  4.   else Caption :='not a command';
  5. end;
Patron Cocoa Widgetset development https://www.patreon.com/skalogryz

Dan3468298

  • Full Member
  • ***
  • Posts: 125
Re: How do I test for MacOS COMMAND key down?
« Reply #4 on: August 05, 2020, 12:42:17 am »
Not sure on a mac but maybe the GetKeyState in the LCLIntf unit will do it ?

If you know the Virtual key code of the MAC key you can do this during a Keydown event..

Code: Pascal  [Select][+][-]
  1. If GetKeyState(VK_MACKEY?) < 0 THen its pressed..
  2.  

Like I said, I don't know what the key code would be...

KeyCode is 91 for the Command key.
MacOS 10.15.5/Lazarus 2.0.10 Build 2020-07-07

Dan3468298

  • Full Member
  • ***
  • Posts: 125
Re: How do I test for MacOS COMMAND key down?
« Reply #5 on: August 05, 2020, 12:45:33 am »
In KeyDown is the MacOS COMMAND key defined in a TShiftState?   I can't figure it out.  Any ideas?
it's ssMeta
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
  2. begin
  3.   if (ssMeta in Shift) then Caption:='Command is held'
  4.   else Caption :='not a command';
  5. end;

That worked.  Thanks!
MacOS 10.15.5/Lazarus 2.0.10 Build 2020-07-07

VTwin

  • Hero Member
  • *****
  • Posts: 927
  • Former Turbo Pascal 3 user
Re: How do I test for MacOS COMMAND key down?
« Reply #6 on: August 05, 2020, 03:01:18 am »
I use this for cross-platform:

Code: Pascal  [Select][+][-]
  1.  // define standard menu command key as ssCmd
  2.   {$IFDEF DARWIN}
  3.   ssCmd = ssMeta; // Apple
  4.   {$ELSE}
  5.   ssCmd = ssCtrl; // Windows, Linux
  6.   {$ENDIF}
  7.  
“Talk is cheap. Show me the code.” -Linus Torvalds

macOS 10.13.6: Lazarus 2.0.8 (64 bit Cocoa, also fixes and trunk)
Ubuntu 18.04.3: Lazarus 2.0.8 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.0.8 (64 bit on VBox)

Dan3468298

  • Full Member
  • ***
  • Posts: 125
Re: How do I test for MacOS COMMAND key down?
« Reply #7 on: August 05, 2020, 03:26:09 am »
I use this for cross-platform:

Code: Pascal  [Select][+][-]
  1.  // define standard menu command key as ssCmd
  2.   {$IFDEF DARWIN}
  3.   ssCmd = ssMeta; // Apple
  4.   {$ELSE}
  5.   ssCmd = ssCtrl; // Windows, Linux
  6.   {$ENDIF}
  7.  

Thumbs up!  Thx
MacOS 10.15.5/Lazarus 2.0.10 Build 2020-07-07

 

TinyPortal © 2005-2018