Accessibility API - Setting keyboard focus to a specific element - cocoa

Using the accessibility API, I am drilling down through a series of elements and finding a specific AXUIElementRef I am interested in (all in an external application). How do I check if this element currently has keyboard focus? And if not, how do I set it?
Something similar to using AXUIElementSetAttributeValue to set the kAXMainAttribute to true (this works for a window - thanks Peter!).
PS - I have tried kAXFocusedAttribute, doesn't seem to do the job. Maybe it's read only?

PS - I have tried kAXFocusedAttribute, doesn't seem to do the job. Maybe it's read only?
Quite the opposite. Read the header:
Writable? Yes, for any focusable element. No in all other cases.
Make sure you've activated the application (by setting its kAXFrontmostAttribute to true) and made the window key (by setting its kAXFocusedAttribute to true) before you give a control in the window focus (by setting its kAXFocusedAttribute to true).

Related

Is there a way to find out whether the Metro UI panels are visible in Windows 10?

In Windows 10 there are several menus/panels that can be opened from the task bar, such as "action center" and "volume panel" etc. Those windows are not enumerated if I try to use EnumWindows(), I found a workaround -- instead of using EnumWindows(), I used FinWindowEx(), and now I'm able to enumerate those panels and other Metro UI specific menus.
The only problem is that I cannot identify whether they are visible or not. If I try to call IsWindowVisible() and pass "action center" HWND as an argument, the function always returns TRUE. The same result I've got for other Metro UI panels (battery usage panel, volume panel, search panel and so on).
I've just found a solution. As there has been no answers so far, I would consider it as a best solution (at the moment I don't know other ways to solve the issue).
So basically in order to identify whether the window is visible, I call DwmGetWindowAttribute() function (in addition to IsWindowVisible()) passing DWMA_CLOAKED as the 2nd argument. This attribute is set to 0 when those Windows 10 specific panels are visible on the screen.

SDL2 Raising a window without giving it focus

I need to display a tooltip over a window. I'm creating a second window with the tool tip and using SDL_RaiseWindow() to bring it to the top. However, doing that causes the tooltip to steal focus which is not what I want. Is there a way to bring a window to the top without changing focus?
Also, is there a way to set focus (mouse and/or keyboard) without changing the Z order of the windows?
The answer offered by Neil will only work under X11 as SDL_SetWindowInputFocus() is only implemented for that environment. In essence, the desired behaviour is otherwise not achievable. I have seen that there is a feature request in the SDL forums for an overload of the SDL_RaiseWindow() function to include an optional bool parameter to indicate if the raised window should also receive the input focus, or not. I hope they do implement that.
In any case, the support for multiple windows under SDL 2.x is a little weak. There is no built in support for the Z-order of different windows, and trying to build one based on the "painter's method" works, but leaves one no control over the input focus.
Old question, but this came up during my own search. You could try SDL_RaiseWindow() to bring your tooltip to the top, then use SDL_SetWindowInputFocus() on the main window to switch focus back to it.
I got this working sufficiently for my tooltips on mac by using SDL_WINDOW_ALWAYS_ON_TOP flag with SDL2:
SDL_CreateWindow(tooltip_window->name, x, y, w, h,
SDL_WINDOW_OPENGL | SDL_WINDOW_BORDERLESS |
SDL_WINDOW_ALWAYS_ON_TOP);
SDL_RaiseWindow(windowThatShouldHaveFocus);
// ...render what you want on that tooltip (SDL_RenderClear, SDL_RenderCopy, SDL_RenderPresent) & hide it with SDL_HideWindow
And when showing the tooltip:
SDL_ShowWindow(tooltipWindow);
SDL_RaiseWindow(windowThatShouldHaveFocus);

How to add a NSColorPicker to the application's main window?

I'm building an application to generate an array of colors based on a color chosen by the user.
The default on Mac OS X for color selection is to open a NSColorPanel containing multiple NSColorPickers. But, as the color selection process is the main interaction the user will have with the app, it'd be better to avoid the extra clicks and panel-popping in favor of a more straightforward way.
So, is there any way to add a NSColorPicker object to a window?
I know this is an older question, but check out NSColorWell. From the docs:
NSColorWell is an NSControl for selecting and displaying a single color value.
Interresting Question.
I strongly doubt it (but would love to be proven wrong). NSColorPickers are not NSControls (nor NSCells) so there's no clean wrapper to insert into a window.
Even if you were to instanciate an NSColorPanel and get a reference to its contentView and copy it (with all that defines the color picking controls) to your own window... there's no obvious way of obtaining the color value. NSColorPickers are plug-ins so you can't forsee the controls of a colorPicker.
The only other way I can see (and that's a stretch) would be to manually load the NSColorPickers plug-ins directly. I don't know how successfull this would be.
File a bug report and request the feature?

Tab Skips Select Form Fields in Mac Firefox. Can I stop this behavior by changing my program?

Tab skips select form fields in Mac Firefox.
http://www.tonyspencer.com/2006/05/02/tab-skips-select-form-fields-in-mac-browsers/
talks about the issue, and shows how to stop it by changing the system preference.
However, I am running some online psych experiment and would like to avoid asking users to change their system preference.
So I need some way to stop skipping-behavior in my code.
Is it possible?
Any ideas appreciated.
Thanks in advance.
=====edit========
Please note that I cannot use JQuery in my code, but I can use JavaScript.
I solved the issue by myself.
What I did was not very optimal, but here is what I did.
assign tabindex for each element which will have focus.
when enter key is pressed (tab caused another issue, so I didn't use it), search the element whose tabindex = 1 + currentelement.tabindex, and make it as next element.
set focus to the next element.
Thanks.

Setting value of AXTextField programmatically (OS X Cocoa Accessibility API)

I'm using the Cocoa Accessibility API to try and modify the value of a text field (AXTextField) in another application, but I've run into a problem: my code correctly identifies and modifies the contents of the text field in question, and the text of the field visibly changes, but the changes aren't registered by the program I'm trying to control. Is there a way to do this in with the API without having to generate keyboard events?
Sample code:
AXUIElementCopyElementAtPosition(appRef,
clickPoint.x,
clickPoint.y,
&boxRef);
NSString *valueToSet = [NSString stringWithFormat:#"%f",amount];
AXUIElementSetAttributeValue(boxRef,kAXValueAttribute,valueToSet);
And the text field changes to the value specified in "amount" but the other program doesn't recognize the change - I have to go type the number in myself to get it to pick up the change (I can tell the difference, because the program responds when a new value is typed in the box). Can anyone point me in the right direction?
For posterity: Informed sources tell me that this is actually a bug in the application I'm trying to control. You can tell the difference by using UI Browser (http://prefabsoftware.com/uibrowser/) to try and set the value of the textfield; if UI Browser can't make the change stick, then the matter is out of your control.
Try telling the text field:
perform action "AXConfirm"
This is Applescript, but whatever the Cocoa equivalent is, it may make the change stick even if UI Browser can not (I've used it before).

Resources