Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get current modifier states with FireMonkey on OSX?

With Delphi for Windows, I usually use this code:

function isCtrlDown : Boolean;
var
  ksCurrent : TKeyboardState;
begin
  GetKeyboardState(ksCurrent);
  Result := ((ksCurrent[VK_CONTROL] and 128) <> 0);
end;

How can I achieve this with FireMonkey on Mac OSX?

I have found this, but I don't know how to manage it with FireMonkey/Delphi (which uses, ...):

void PlatformKeyboardEvent::getCurrentModifierState(bool& shiftKey, bool& ctrlKey, bool& altKey, bool& metaKey)
{
    UInt32 currentModifiers = GetCurrentKeyModifiers();
    shiftKey = currentModifiers & ::shiftKey;
    ctrlKey = currentModifiers & ::controlKey;
    altKey = currentModifiers & ::optionKey;
    metaKey = currentModifiers & ::cmdKey;
}

I'm still investigating... For now, I have find this unit with key events stuff... unit Macapi.AppKit;

like image 910
Whiler Avatar asked Oct 13 '12 20:10

Whiler


2 Answers

This returns the current shift state:

uses
  Macapi.CoreGraphics;

function KeyboardModifiers: TShiftState;
const
  kVK_Shift                     = $38;
  kVK_RightShift                = $3C;
  kVK_Control                   = $3B;
  kVK_Command                   = $37;
  kVK_Option                    = $3A;
begin
  result := [];
  if (CGEventSourceKeyState(0, kVK_Shift) <> 0) or (CGEventSourceKeyState(0, kVK_RightShift) <> 0) then Include(result, ssShift);
  if CGEventSourceKeyState(0, kVK_Command) <> 0 then Include(result, ssCommand);
  if CGEventSourceKeyState(0, kVK_Option) <> 0 then Include(result, ssAlt);
  if CGEventSourceKeyState(0, kVK_Control) <> 0 then Include(result, ssCtrl);
end;
like image 156
Giel Avatar answered Oct 25 '22 10:10

Giel


Based on this answer you could try this:

function isCtrlDown : Boolean; 
begin
    Result := NSControlKeyMask and TNSEvent.OCClass.modifierFlags = NSControlKeyMask;
end;
like image 22
Arjen van der Spek Avatar answered Oct 25 '22 09:10

Arjen van der Spek