【问题标题】:How to get current modifier states with FireMonkey on OSX?如何在 OSX 上使用 FireMonkey 获取当前修饰符状态?
【发布时间】:2012-10-04 07:44:14
【问题描述】:

对于 Windows 的 Delphi,我通常使用以下代码:

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

如何在 Mac OSX 上使用 FireMonkey 实现这一目标?

我找到了this,但我不知道如何使用 FireMonkey/Delphi(它使用...)来管理它:

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

我还在调查... 目前,我发现这个单元有 key events 的东西...... unit Macapi.AppKit;

【问题讨论】:

    标签: macos delphi delphi-xe2 firemonkey keyevent


    【解决方案1】:

    这将返回当前的班次状态:

    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;
    

    【讨论】:

    • 我在睡觉时发布的两种解决方案都有效。抱歉,我接受了另一个,因为它是在几分钟前发布的……在你们俩之间很难做出选择。顺便说一句,你会得到 +1
    • 感谢 Whiler,Giel 也向我 +1
    • 在 Delphi 11 中,该函数返回一个布尔值,因此新代码看起来像 CGEventSourceKeyState(0, kVK_Command) then Include(result, ssCommand);
    【解决方案2】:

    基于这个answer,你可以试试这个:

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

    【讨论】:

      猜你喜欢
      • 2016-06-23
      • 1970-01-01
      • 1970-01-01
      • 2019-02-18
      • 1970-01-01
      • 2016-12-02
      • 1970-01-01
      • 2020-08-15
      • 2023-03-17
      相关资源
      最近更新 更多