Windows which window has focus

How can I tell which window has the keyboard focus, by looking?

How can I tell which window has the keyboard focus, by looking?

Used to be there was a difference in appearance between the window with the focus (activated) and other windows. With two large monitors, and generally 20 or so windows open, it is very hard to «guess» or «remember» which window has the focus.

The only thing I’ve seen so far, if I look really close, is the 1px border around the active window is a different color than other windows. But that is extremely easy to overlook.

When I lose the mouse, I can figure out where it is because I turned on the option to draw a circle around it when I hit the Control key.

Is there some similar way to figure out which window has the focus? Or better, some setting that changes the color of the active window’s title bar to be different than inactive windows, like it used to be?

Replies (5) 

Thank you for posting your query on Microsoft Community.

As per the description, I understand that you are facing some issues using Windows 10.

I would like to know some information.

What exactly is the issue that you are facing?

What do you mean by when you say the Keyboard focus?

Have you tried any solution for this issue?

If possible please provide us the screen shot of the issue, so that we can go ahead and help you in a better way.

Please provide us the information in brief. We will be glad to assist you further.

Was this reply helpful?

Sorry this didn’t help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

1. Two large monitors with maybe 20 windows open, and maybe 5 windows are fully visible on the monitors, with the other partially or fully hidden. When I walk away and come back, I look at the screen and wonder which window has the keyboard focus.

2. Ever since Windows was created, there has been one window that has the keyboard focus. That is the window to which keystrokes are sent, when you type on the keyboard. You know, the keyboard is the thing that gets in the way of moving your mouse around, especially to the left. Another description for the window that has the keyboard focus is the activated window, or the «top of the Z order».

3. In prior versions of Windows, the title bar of the window with the keyboard focus had a different background color than any of the other windows. In Windows 95, the window with the keyboard focus had a blue background color for the title bar, and all the others had grey. This was an easy thing to spot. Newer versions of Windows have used colors that are less and less different, but it was still fairly easy to distinguish the one window that was different than the others. With Windows 10, the background color for all windows seems to be white. This makes it very hard to distinguish which window has the keyboard focus. This is a new issue, encountered within the last few days when I upgraded to Windows 10. The purpose of my question is to find a solution (one or more) so that I can try them. If there is a solution, I haven’t yet found it by Googling. Most items I have found by Googling are just the complaints by other people about the same issue. If you have solution to offer, please suggest it.

Читайте также:  Pdo ��� ������� linux

How to know which window has focus and how to change it?

I would like to know how can I ask X11 which windows has focus. And if for any reason my own application (that may be visible or not) got the focus I want be able to let the former windows to get focus again.

For instance, my application is running with many others (e.g. firefox, gvim, nautilus. )

Suppose that at first firefox has focus and that the user clicked on my app which now has the focus. I want that my application put focus on firefox again.

Does anyone knows how to achieve this? Books recommendations would be very nice.

4 Answers 4

Use this XQueryTree to find the currently active, or top-most window.

Here is a function, when given a display, it will find the current window in focus:

I found the source:

Good Luck

Take a look at the _NET_ACTIVE_WINDOW value of the root window which is set by most modern window managers:

This value can, of course, be obtained using Xlib library calls.

You probably want the XGetInputFocus call.

In this snippet, focused will be the window with current input focus, getting keyboard events and mouse button presses.

This will work even if the window manager does not set the _NET_ACTIVE_WINDOW property on the root window, as specified by EWMH. A few window managers, such as dwm and my 9wm, don’t set this.

I recommend an application called XDoTool. It supports quite a lot of queries, controls, and even hooks.

Commands like behave accept a callback, which can be built-in like getmouselocation or external like exec notify-send ‘focused window’ , exec zsh myscript.zsh , etc., however you want to use it.

Edit — you can focus using xdotool windowfocus [options] [window] , as in xdotool search —class firefox windowfocus . In my case this causes errors because Firefox shows up as a couple dozen ‘windows’, but all have the same PID; it works given the right ID. Hopefully that’s a start.

How to find out which object currently has focus

I have a few TextField s in my Frame . I want to know which TextField currently has focus. How can I find this information?

9 Answers 9

JFrame.getFocusOwner() (inherited from Window.getFocusOwner() ) ought to return a reference to the component with focus. getMostRecentFocusOwner() might also be of interest.

getFocusOwner() will return the child component which is currently focused.

But you have to check to see if it is a JTextField. Other components like buttons might be focused if they exist in your frame as well.

You could also listen for the appropriate property change in keyboard focus manager:

This outputs focus owner as you interact with Swing components and is useful for debugging focus issues in general.

Every JComponent has a hasFocus method that you can use to check if it has focus. However, this has been changed, and now you should use isFocusOwner.

So run over all the text fields in your frame, and check on each of them if it is isFocusOwner by calling that method.

You could also get the focus owner through the frame.

KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() ;
wont work across threads. So if your app invokes a new thread and that thread has its own frame/window etc then it wont be able to gain the focus owner from that thread. Instead use: KeyboardFocusManager.getCurrentKeyboardFocusManager().getGlobalFocusOwner();

Читайте также:  Что такое драйвер для lenovo 3000 g530 windows

This tutorial should be pretty helpful to understand focus.

You can get currently focused Component like that:

After that You can check if focusOwner is instance of TextField

Not the answer you’re looking for? Browse other questions tagged java swing focus or ask your own question.

Linked

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.4.16.39093

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

How do I find out which DOM element has the focus?

I would like to find out, in JavaScript, which element currently has focus. I’ve been looking through the DOM and haven’t found what I need, yet. Is there a way to do this, and how?

The reason I was looking for this:

I’m trying to make keys like the arrows and enter navigate through a table of input elements. Tab works now, but enter, and arrows do not by default it seems. I’ve got the key handling part set up but now I need to figure out how to move the focus over in the event handling functions.

16 Answers 16

Use document.activeElement , it is supported in all major browsers.

Previously, if you were trying to find out what form field has focus, you could not. To emulate detection within older browsers, add a «focus» event handler to all fields and record the last-focused field in a variable. Add a «blur» handler to clear the variable upon a blur event for the last-focused field.

If you need to remove the activeElement you can use blur; document.activeElement.blur() . It will change the activeElement to body .

As said by JW, you can’t find the current focused element, at least in a browser-independent way. But if your app is IE only (some are. ), you can find it the following way :

EDIT: It looks like IE did not have everything wrong after all, this is part of HTML5 draft and seems to be supported by the latest version of Chrome, Safari and Firefox at least.

If you can use jQuery, it now supports :focus, just make sure you are using version 1.6+.

This statement will get you the currently focused element.

elem.tabIndex); – Harry Pehkonen Oct 13 ’17 at 3:16

document.activeElement is now part of the HTML5 working draft specification, but it might not yet be supported in some non-major/mobile/older browsers. You can fall back to querySelector (if that is supported). It’s also worth mentioning that document.activeElement will return document.body if no element is focused — even if the browser window doesn’t have focus.

The following code will work around this issue and fall back to querySelector giving a little better support.

An addition thing to note is the performance difference between these two methods. Querying the document with selectors will always be much slower than accessing the activeElement property. See this jsperf.com test.

By itself, document.activeElement can still return an element if the document isn’t focused (and thus nothing in the document is focused!)

You may want that behavior, or it may not matter (e.g. within a keydown event), but if you need to know something is actually focused, you can additionally check document.hasFocus() .

The following will give you the focused element if there is one, or else null .

Читайте также:  Драйвера для idt high definition audio codec windows 10 64 bit

To check whether a specific element has focus, it’s simpler:

To check whether anything is focused, it’s more complex again:

Robustness Note: In the code where it the checks against document.body and document.documentElement , this is because some browsers return one of these or null when nothing is focused.

It doesn’t account for if the (or maybe ) had a tabIndex attribute and thus could actually be focused. If you’re writing a library or something and want it to be robust, you should probably handle that somehow.

Here’s a (heavy airquotes) «one-liner» version of getting the focused element, which is conceptually more complicated because you have to know about short-circuiting, and y’know, it obviously doesn’t fit on one line, assuming you want it to be readable.
I’m not gonna recommend this one. But if you’re a 1337 hax0r, idk. it’s there.
You could also remove the || null part if you don’t mind getting false in some cases. (You could still get null if document.activeElement is null ):

For checking if a specific element is focused, alternatively you could use events, but this way requires setup (and potentially teardown), and importantly, assumes an initial state:

You could fix the initial state assumption by using the non-evented way, but then you might as well just use that instead.

How to detect which window is focus?

How can you in Javascript detect an active window?

Ok, I want to open up 3 windows. These 3 windows are menus and will be used to modify number of other windows.

Think of it like a paint program. You have 2 windows for tools and 1 for content. However, think about that 1 window for content but instead it’s not restricted to just one. You can have 12 windows for content to change or add stuff too. etc.

So, with JavaScript I want to know what window is focused? 3 windows will be like tools. Then there could be 1 to 12 or more windows opened that has content for edit. However, I need to know how can JavaScript focus on 1 window that the user has select or is currently monkeying around with?

I just want the user to be able to edit or work with one content page at a time while having all them open up as individual windows.

So for example: I have 3 windows that are tool menu windows. Lets say I open 12 individual windows that have images in them. I plan to modify and edit them.

How can I get javascript to focus on one window at a time. So, if I lets say change the background color of the image to red. I don’t want to change all 12 windows contents to a background with red. Instead lets say I want to change the background color of window 10 out of the 1 to 12 content windows.

How can I zero in on that one window to work with. However, I must be able to zero in on it based on what the user does. Like for example clicks that window which gets selected or focused on. So, the user just has to click on that window that they want to work on.

So, if I clicked on that content window 10. I will modify only that window. Then quickly I can click content window 5 and the focus will change to that window content 5. So, if I change that background to blue it will only change content window 5’s background color to blue and not content window 10.

From what I see I would need to check if the window is focus property is true or if blur is true. However, I need to trigger an event when the focus properties of any of the windows change.

Оцените статью