如何测试窗口当前的焦点 [英] How to test if window is currently focused

查看:101
本文介绍了如何测试窗口当前的焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以测试一个窗口当前的焦点?我知道我可以通过在窗口中激活的addEventListener来实现这一点,并使用全局变量来跟踪它,但事情是我需要测试我的插件启动时,所以这是一个机会添加任何事件侦听器。

执行窗口== Services.wm.getMostRecentWindow(null)将不起作用,因为其他应用程序窗口可能已经结束它。所以我想我需要知道如果firefox是当前最顶层的窗口。

tldr:我需要测试当前窗口是否集中,意味着不只是任何其他firefox窗口,但在操作系统上的所有窗口。

解决方案

使用附加sdk:



  require(sdk / window / utils)。isFocused(window)



没有加载项sdk:

$ p $ function isFocused(window){
const FM = Cc [@ mozilla.org/focus-manager;1]。
getService(Ci.nsIFocusManager);

let childTargetWindow = {};
FM.getFocusedElementForWindow(window,true,childTargetWindow);
childTargetWindow = childTargetWindow.value;

let focusedChildWindow = {};
if(FM.activeWindow){
FM.getFocusedElementForWindow(FM.activeWindow,true,focusedChildWindow);
focusedChildWindow = focusedChildWindow.value;
}

return(focusedChildWindow === childTargetWindow);
}

isFocused(window);


Is there a way to test if a window is currently focused? I know I can do this with addEventListener of activated on the window, and use a global var to track it, but the thing is I need to test this on startup of my addon, so this is before a chance to add any event listeners.

Doing window == Services.wm.getMostRecentWindow(null) will not work because other application windows may be over it. So I guess I need to know if firefox is the currently top most window.

tldr: I need to test if the current window is focused, meaning not just of any other firefox windows, but of all windows on the OS.

解决方案

With the add-on sdk:

require("sdk/window/utils").isFocused(window)

Without the add-on sdk:

function isFocused(window) {
  const FM = Cc["@mozilla.org/focus-manager;1"].
                getService(Ci.nsIFocusManager);

  let childTargetWindow = {};
  FM.getFocusedElementForWindow(window, true, childTargetWindow);
  childTargetWindow = childTargetWindow.value;

  let focusedChildWindow = {};
  if (FM.activeWindow) {
    FM.getFocusedElementForWindow(FM.activeWindow, true, focusedChildWindow);
    focusedChildWindow = focusedChildWindow.value;
  }

  return (focusedChildWindow === childTargetWindow);
}

isFocused(window);

这篇关于如何测试窗口当前的焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆