delphikeyboardfiremonkeydelphi-xe3firemonkey-fm2

GetKeyState in firemonkey


In VCL (Delphi 2010) I used this function to check whether control key is pressed:

function IsControlKeyPressed: Boolean;
begin
  Result := GetKeyState(VK_CONTROL) < 0;
end;

GetKeyState is function in windows library that I do not want to include it into my project.

How can I check if control or shift key is pressed in XE3 for firemonkey application?


Solution

  • If it helps for anyone else, this is my unit:

    unit uUtils;
    
    interface
    
    uses
    {$IFDEF MSWINDOWS}
      Winapi.Windows;
    {$ELSE}
      Macapi.AppKit;
    {$ENDIF}
    function IsControlKeyPressed: Boolean;
    function IsShiftKeyPressed: Boolean;
    
    implementation
    
    function IsControlKeyPressed: Boolean;
    begin
    {$IFDEF MSWINDOWS}
      Result := GetKeyState(VK_CONTROL) < 0;
    {$ELSE}
      Result := NSControlKeyMask and TNSEvent.OCClass.modifierFlags = NSControlKeyMask;
    {$ENDIF}
    end;
    
    function IsShiftKeyPressed: Boolean;
    begin
    {$IFDEF MSWINDOWS}
      Result := GetKeyState(VK_SHIFT) < 0;
    {$ELSE}
      Result := NSShiftKeyMask and TNSEvent.OCClass.modifierFlags = NSShiftKeyMask;
    {$ENDIF}
    end;
    
    end.