Which app has the focus when a global shortcut is triggered

試著忘記壹切 提交于 2019-12-24 04:28:10

问题


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 :)


回答1:


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.




回答2:


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})))


来源:https://stackoverflow.com/questions/30446578/which-app-has-the-focus-when-a-global-shortcut-is-triggered

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!