Hammerspoon: hs.dialog.textPromt keyboard focus

Created on 1 Oct 2017  路  16Comments  路  Source: Hammerspoon/hammerspoon

I really like the dialog.textPromt, but sadly the text input field doesn't have keyboard focus when it opens, and there doesn't seem to be anyway to get it without using the mouse. Is there some trick to do this?

If not it would be amazing if it could be implemented so it would open up with keyboard focus (possibly optional)

Most helpful comment

@blahgeek - If I type hs.dialog.textPrompt("Main message.", "Please enter something:") into the Hammerspoon Console it will popup the Text Prompt, and the text box will have focus, so you can start typing straight away.

However if I use hs.timer.doAfter(5, function() hs.dialog.textPrompt("Main message.", "Please enter something:") end), and then click away from the Hammerspoon Console before the 5 seconds is up, the script won't automatically give Hammerspoon focus. As soon as Hammerspoon gets focus though, you can start typing in the text box.

You could solve this by just adding hs.focus() before hs.dialog.textPrompt(), for example:

hs.timer.doAfter(5, function() hs.focus(); hs.dialog.textPrompt("Main message.", "Please enter something:") end)

All 16 comments

Great suggestion! Will take a look and try and submit a pull request tomorrow.

Awesome! Another suggestion while on it would be the possibility to cycle between the buttons and the input field using tab. But im already very happy :)

Sorry for the delay @rhoeberg!

1570 now gives the text input focus, and a few other bug/documentation fixes.

I'm not sure why you can't tab between the buttons and text input though. Any ideas @asmagill or @cmsj ?

Very fast response and implementation I'm thankful. Looking forward to trying it.

FYI - it seems you need to enable a macOS Keyboard Preference to tab between buttons on a dialog box - see: https://stackoverflow.com/a/32774468

However, even with this activated, it won't let you tab between the text input and the buttons (it just does the buttons in the original code, and stays on the text input on my latest commit). Any ideas @asmagill or @cmsj how I make it so it's tab-able between buttons and input if "All controls" is selected at the bottom of the Keyboard > Shortcuts preferences panel?

Ran into this with the textfield submodule of guitk... I don't have the code in front of me at the moment, but I think what I did to fix it was rebuild the keyViewLoop for the window object after attaching each new textfield element -- check out NSWindow's recalculateKeyViewLoop and/or autorecalculatesKeyViewLoop.

FYI - I've had a play with trying to make it tab between the input and the buttons, but couldn't get it to work using recalculateKeyViewLoop or autorecalculatesKeyViewLoop. However, given hs.dialog.textPrompt only supports two buttons anyway, I've just made it so ENTER will trigger buttonOne and ESC will trigger buttonTwo.

late response here. It sounds like a really good compromise, it would work for my use cases.

Unfortunately my experiences with Objective-C is at a minimum so not sure I can help that much, but if there is anything I could do I would gladly help. Also don't mind a challenge.

I'm using the latest release version of hammerspoon but I'm still having this issue, am I missing something?

I use a keyboard shortcut to trigger dialog.textPrompt, but after the dialog pops up, the keyboard focus is still on the (old) frontmost window, I have to click the dialog to input text.

@blahgeek - If I type hs.dialog.textPrompt("Main message.", "Please enter something:") into the Hammerspoon Console it will popup the Text Prompt, and the text box will have focus, so you can start typing straight away.

However if I use hs.timer.doAfter(5, function() hs.dialog.textPrompt("Main message.", "Please enter something:") end), and then click away from the Hammerspoon Console before the 5 seconds is up, the script won't automatically give Hammerspoon focus. As soon as Hammerspoon gets focus though, you can start typing in the text box.

You could solve this by just adding hs.focus() before hs.dialog.textPrompt(), for example:

hs.timer.doAfter(5, function() hs.focus(); hs.dialog.textPrompt("Main message.", "Please enter something:") end)

I am using the latest 0.9.66 of Hammerspoon, and this issue happens still, as mentioned before.

Essentially, when the HS main window is not focused, i.e. you are in an other application, the dialog does not get focus. Typing text, the escape or the enter keys do not work.

When querying for keyboard focus via Mac's VoiceOver, it says that nothing has keyboard focus.

Is there a way to fix this internally, without having to call hs.focus()?

Thoughts on this @asmagill & @cmsj ? If you trigger hs.dialog.textPrompt() should it automatically give Hammerspoon focus?

This is still an issue for me on v0.9.76

Regards,
iain

@latenitefilms thinking back to my experiences with other apps over the last year or so, I think this has become the default for most applications -- even if they throw up a modal dialog, it doesn't take focus unless the app is already focused. Certainly with installers I've occasionally found them stalled in the background while I was doing something else because it was waiting for me to answer a dialog box.

One idea that comes to mind is to add [[NSApplication sharedApplication] activateIgnoringOtherApps:YES]; to the dialog functions themselves (or maybe better, add an optional boolean argument to the dialog functions to optionally do this).

Still an issue for me but only when assiging to a v

fixed by focusing hammerspoon first.

Was this page helpful?
0 / 5 - 0 ratings