2012-10-13 43 views
5

Với Delphi cho Windows, tôi thường sử dụng mã này:Làm cách nào để nhận trạng thái sửa đổi hiện tại bằng FireMonkey trên OSX?

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

Làm thế nào tôi có thể đạt được điều này với FireMonkey trên Mac OSX?

Tôi đã tìm thấy this, nhưng tôi không biết làm thế nào để quản lý nó với FireMonkey/Delphi (trong đó sử dụng, ...):

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; 
} 

tôi vẫn đang điều tra ... Đối bây giờ, tôi có tìm thấy đơn vị này với sự kiện chính thứ ... unit Macapi.AppKit;

Trả lời

2

Dựa trên answer này bạn có thể thử này:

function isCtrlDown : Boolean; 
begin 
    Result := NSControlKeyMask and TNSEvent.OCClass.modifierFlags = NSControlKeyMask; 
end; 
4

này trả về trạng thái thay đổi hiện tại:

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; 
+0

Cả hai giải pháp được đăng trong giấc ngủ của tôi đang làm việc. Xin lỗi, tôi đã chấp nhận cái khác vì nó đã được đăng vài phút trước đó ... rất khó để lựa chọn giữa hai người. BTW, bạn nhận được +1 – Whiler

+0

Cảm ơn Whiler, +1 từ tôi cũng là Giel –