如何将键关闭事件传输到以下窗口

  • 本文关键字:传输 窗口 事件 swift cocoa
  • 更新时间 :
  • 英文 :


我禁用了应用程序的菜单栏,因此当窗口打开时,左上方没有菜单。现在我想执行以下操作:

例如:

当我的窗口在"Chrome"窗口的前面时,当我专注于我的窗口时,菜单栏仍然显示"Chrome"的菜单。现在我想在窗口激活时将快捷方式事件转移到镶边,我尝试输入"命令 + Q",但它没有任何操作。那么我怎样才能务实地做到这一点呢?

monitor = NSEvent.addGlobalMonitorForEvents(matching: mask, handler: handler)

我认为这对使用您的应用程序的用户来说是一种安全风险。您可以在不让用户知道的情况下监视任何密钥输入(密码(。使用沙盒应用程序,这将很难存档,但我知道有效的一种方法是使用 Apple 脚本转发事件,如以下示例所示。这也可以与ScriptingBridge或可访问性框架一起使用,但我自己没有使用它。当然,您必须先询问窗口服务器当前在应用程序(窗口索引,框架(下方的屏幕上可以看到哪个应用程序(窗口(。

使用 Apple Script 发送击键:

tell application "Firefox" 
activate 
end tell 
delay 1 
tell application "System Events" 
keystroke "l" using {command down} 
set the clipboard to ("[@URL]") 
keystroke "v" using {command down} 
keystroke return 
end tell 

获取窗口列表:

let options = CGWindowListOption(arrayLiteral: CGWindowListOption.excludeDesktopElements, CGWindowListOption.optionOnScreenOnly)
let windowListInfo = CGWindowListCopyWindowInfo(options, CGWindowID(0))
let infoList = windowListInfo as NSArray? as? [[String: AnyObject]]

最新更新