Which app has the focus when a global shortcut is

2019-07-18 17:08发布

I use a global shortcut to popup a dialog. But I would like to fill the dialog depending on the application having the focus at the moment the shortcut is triggered.

But I can not find a way to do it. I read the extension code here but it's hard to know how to modify the code, and how to recompile electron.

If someone have a pointer, that will be greatly appreciated :)

2条回答
孤傲高冷的网名
2楼-- · 2019-07-18 17:40

I found an answer myself, it looks that the frontmost app (at least on OSX) it still the app that had the focus before the invokation.

Here is my ClojureScript code to find the app info

(defn- get-current-app-info-osx
  "Return info about the current frontmost application on OSX"
  []
  (let [remote  (js/require "remote")
        nodobjc (js/require "nodobjc")]
    (.framework nodobjc "AppKit")
    (let [workspace (.NSWorkspace nodobjc "sharedWorkspace")
      app       (workspace "frontmostApplication")
      app-name  (str (app "localizedName"))
      app-id    (str (app "bundleIdentifier"))]
  {:name app-name
   :id   app-id})))
查看更多
放荡不羁爱自由
3楼-- · 2019-07-18 17:58

I couldn't find a way to know which app had the focus before, but I was able to hide my app and return the focus to the previous one, simulating clicking the 'hide' option from the view menu:

const menu = require('electron').Menu;

menu.sendActionToFirstResponder('hide:');

I hope it helps.

查看更多
登录 后发表回答