如何测试窗口当前的焦点 [英] How to test if window is currently focused
问题描述
执行窗口== 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);
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屋!