-1

I disable the menu bar for my application, so when the window is opened, there is no menu on the left top. Right now i want to do the below action:

for example:

when my window is in front of the window of "Chrome", then when i focus on my window, the menu bar still show the "Chrome"'s menu. Now i want to transfer the short cut event to the chrome when my window is activate, i tried to input "command + Q" but it has no action. So how can i do this pragmatically?

 monitor = NSEvent.addGlobalMonitorForEvents(matching: mask, handler: handler)
rmaddy
  • 314,917
  • 42
  • 532
  • 579
jimwan
  • 1,086
  • 2
  • 24
  • 39

1 Answers1

0

I see this as a security risk for user using your app. You could monitor any key input (passwords) without letting the user know. With a sandboxed app this will be difficult to archive but one way I know what would work is to forward events with Apple Script as shown in the sample below. This may also work with ScriptingBridge or Accessibility framework but I did not use this by myself. Of course you would have first to ask the window server which app (window) is currently visible on the screen below your app (window index, frame).

Send keystrokes with 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 

Getting the window list:

let options = CGWindowListOption(arrayLiteral: CGWindowListOption.excludeDesktopElements, CGWindowListOption.optionOnScreenOnly)
    let windowListInfo = CGWindowListCopyWindowInfo(options, CGWindowID(0))
    let infoList = windowListInfo as NSArray? as? [[String: AnyObject]]
Marc T.
  • 5,090
  • 1
  • 23
  • 40
  • 2
    About 1st solution: Can i use scriptingBridge to interact with "System Events", what's the location for this app; 2nd solution: how can i do with the window information then? @Marc T. – jimwan Jul 06 '18 at 06:56
  • @jimwan: I did not try it with ScriptingBridge by myself but this may help you https://stackoverflow.com/questions/3886214/in-scripting-bridge-how-can-i-send-shortcut-with-2-modifiers. For the window information, you would loop through the array to find the window/application you are looking for. Since the array contains dictionaries to hold the information it should not be too difficult to find the right values. KCGWindowOwnerName would be the name of the app. – Marc T. Jul 09 '18 at 04:30