firemonkey中的GetKeyState

时间:2012-11-21 11:42:57

标签: delphi keyboard firemonkey delphi-xe3 firemonkey-fm2

在VCL(Delphi 2010)中,我使用此功能检查是否按下了控制键:

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

GetKeyState是Windows库中的函数,我不想将它包含在我的项目中。

如何在XE3中检查是否按下了控制键或Shift键以进行firemonkey应用程序?

4 个答案:

答案 0 :(得分:3)

如果对其他人有帮助,这是我的单位:

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.

答案 1 :(得分:2)

请务必在单位 uUtils 中添加以下内容才能正常使用:

System.UITypes  和Macapi.CoreGraphics

uses
System.UITypes,
{$IFDEF MSWINDOWS}
  Winapi.Windows;
{$ELSE}
  Macapi.AppKit, Macapi.CoreGraphics;
{$ENDIF}

答案 2 :(得分:0)

我根据上面的代码检查扩展了这个,如果有任何键是基于vkKeyCode(以及Mac的一些翻译)而关闭的话

function KeyIsDown(KeyInQuestion:Integer):Boolean;
begin
{$IFDEF MSWINDOWS}
   result:=GetAsyncKeyState(KeyInQuestion) AND $FF00 <> 0;
{$ENDIF MSWINDOWS}
{$IFDEF MACOS}
  {$INCLUDE WinVKeyToMacVKey.Inc}
  case KeyInQuestion of
  vkLButton: result:= Macapi.AppKit.TNSEvent.OCClass.pressedMouseButtons = 1;
  vkRButton: result:= Macapi.AppKit.TNSEvent.OCClass.pressedMouseButtons = (1 SHL 1);
  vkMButton: result:= Macapi.AppKit.TNSEvent.OCClass.pressedMouseButtons = (1 SHL 2);
 else
   result:=(CGEventSourceKeyState(0, KeyInQuestion) <> 0);
 end;
{$ENDIF MACOS}
//This is so it's not left/right-centric vkShift will return true
//if either vkShift or vkRShift is down
  if not result then
   case KeyInQuestion of
    vkShift:result:=KeyIsDown(vkRShift);
    vkControl:result:=KeyIsDown(vkRControl);
    vkMenu:result:=KeyIsDown(vkRMenu);
   end;
end;

答案 3 :(得分:0)

这是WinVKeyToMacVKey.Inc文件的文本

case KeyInQuestion of
   vkReturn: KeyInQuestion := $24;
   vkTab: KeyInQuestion := $30;
   vkSpace: KeyInQuestion := $31;
   vkBack: KeyInQuestion := $33;
   vkEscape: KeyInQuestion := $35;
   vkLWin: KeyInQuestion := $37;
   vkRWin: KeyInQuestion := $37;
   vkShift: KeyInQuestion := $38;
   vkMenu: KeyInQuestion := $3A;
   vkControl: KeyInQuestion := $3B;
   vkRShift: KeyInQuestion := $3C;
   vkRMenu: KeyInQuestion := $3D;
   vkRControl: KeyInQuestion := $3E;
   vkF17: KeyInQuestion := $40;
   vkVolumeUp: KeyInQuestion := $48;
   vkVolumeDown: KeyInQuestion := $49;
   vkF18: KeyInQuestion := $4F;
   vkF19: KeyInQuestion := $50;
   vkF20: KeyInQuestion := $5A;
   vkF5: KeyInQuestion := $60;
   vkF6: KeyInQuestion := $61;
   vkF7: KeyInQuestion := $62;
   vkF3: KeyInQuestion := $63;
   vkF8: KeyInQuestion := $64;
   vkF9: KeyInQuestion := $65;
   vkF11: KeyInQuestion := $67;
   vkF13: KeyInQuestion := $69;
   vkF16: KeyInQuestion := $6A;
   vkF14: KeyInQuestion := $6B;
   vkF10: KeyInQuestion := $6D;
   vkF12: KeyInQuestion := $6F;
   vkF15: KeyInQuestion := $71;
   vkHelp: KeyInQuestion := $72;
   vkHome: KeyInQuestion := $73;
   vkPrior: KeyInQuestion := $74;
   vkDelete: KeyInQuestion := $75;
   vkF4: KeyInQuestion := $76;
   vkEnd: KeyInQuestion := $77;
   vkF2: KeyInQuestion := $78;
   vkNext: KeyInQuestion := $79;
   vkF1: KeyInQuestion := $7A;
   vkLeft: KeyInQuestion := $7B;
   vkRight: KeyInQuestion := $7C;
   vkDown: KeyInQuestion := $7D;
   vkUp: KeyInQuestion := $7E;
end;