delphi Firemonkey里触发home按键被按下的事件


本文整理自网络,侵删。

 
吾八哥我最近在使用Delphi里的Firemonkey平台写一个叫“由由密码管家”的APP工具,是跨多平台的,如ios/android/windows/macOs。由于是用于密码管理的,那么在手机里操作会很频繁的被按下home键而切换到后台的,所以希望程序被按下home键的时候隐藏到后台就自动锁定程序,再激活APP的时候要求重新输入密码才可以操作。那么问题来了,在Firemonkey里面如何捕获按下home键的事件呢?网上搜索各种资料,终于找到了答案,这里分享出来具体的解决方法:

定义一个事件方法:

Pascal
function TMaster.HandleAppEvent(AAppEvent: TApplicationEvent; AContext: TObject): Boolean;
begin
  case AAppEvent of
    TApplicationEvent.FinishedLaunching:
      ;
    TApplicationEvent.BecameActive:
      ;
    TApplicationEvent.WillBecomeInactive:
      begin
        if not Assigned(LoginFrame) then
          ShowFrame(TLoginFrame, Master, False);
        LoginFrame.BringToFront;
      end;
    TApplicationEvent.EnteredBackground:
      ;
    TApplicationEvent.WillBecomeForeground:
      ;
    TApplicationEvent.WillTerminate:
      ;
    TApplicationEvent.LowMemory:
      ;
    TApplicationEvent.TimeChange:
      ;
    TApplicationEvent.OpenURL:
      ;
  end;
  Result := True;
end;
使用IFMXApplicationEventService接口里的SetApplicationEventHandler方法将上述事件方法作为参数进行调用即可实现,HandleAppEvent方法里会触发各种程序相关的事件通知。代码如下:

Pascal
var
  SvcEvents: IFMXApplicationEventService;
begin
  if TPlatformServices.Current.SupportsPlatformService(IFMXApplicationEventService, IInterface(SvcEvents)) then
  begin
    SvcEvents.SetApplicationEventHandler(HandleAppEvent);
  end;
end;
注:开发环境Delphi10.2

来源:http://www.5bug.wang/post/39.html

相关阅读 >>

Delphi监控指定进程防止被关闭

Delphi idhttp 断开连接/超时读取

Delphi x 的 y 次方

Delphi twebbrowser 响应回车键(ewb响应正常,无需额外代码)

Delphi根据进程id获取进程路径

Delphi得到系统目录的几个方法

Delphi 用idhttp得到本机外网ip

Delphi fdconnection1 获取数据库总记录数

Delphi 2009 之 tstringbuilder 类[5]: chars[] 属性与 copyto 方法

Delphi通过wmi获取系统信息

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...