default dialog menu item

171 views
Skip to first unread message

Andrew Purvis

unread,
Jan 26, 2019, 6:57:19 PM1/26/19
to autokey-users
I've got a script that gives several options in a menu list, with the most commonly selected one specified as the default.  The menu dialog comes up with the correct item preselected, but the mouse focus is on the menu item.  The user needs to tab to "OK" and then hit Enter or use the mouse to click "OK".  Is there any way to have the focus already on the "OK" button so after the menu pops up, hitting Enter will select the default choice?

Thanks.

jos...@main.nc.us

unread,
Jan 27, 2019, 8:52:38 AM1/27/19
to autoke...@googlegroups.com
TL;DR: I don't know the answer, but this is how I would go about trying to
solve it.

Before getting into the fairly gruesome details below, a couple of quick
questions:

What happens now if you just press Enter?

What happens if you press Ctrl+Enter instead of a plain Enter?

That solves a lot of problems I have using yad (a fork of Zenity. AutoKey
uses Zenity) in other non-AutoKey use cases.

If it works, it takes getting used to, but it's a lot simpler than the
alternatives - at least the ones I can think of.

I glanced at the API code and it's just a setup and call to Zenity. That
means that you could also write a tiny standalone test script in bash that
just calls Zenity the same as AutoKey would and experiment on that to see
if there's any way to get it to behave as desired.

Without AutoKey on top of it, there are a great many more people in places
like stackexchange who might be able to assist you.

If you can get it to work right the first time, then you won't need the
rest of the suggested tomfoolery below.

----

I'd have to study the script because this sounds tricky. The main problem
is that what you want sounds like the way it ought to work to start with.

I don't generally use the AutoKey API to display dialogs. I prefer to call
out to yad (mainly because I know it because I use it for a lot for other
things, but also because it has a lot more options.)

Just off the top of my head, I might try something crazy like calling the
first dialog in the background (which I don't think the API can do) and
then calling the API to do tabbing/cursor positioning onto the button in
the dialog.

Since yad (or your favorite GUI dialog utility) is called externally, you
can tell Python not to wait for it to complete before returning from the
call.

With an approach like this, I would try to get it to work in straight
Python (outside of AutoKey) first because it's a lot easier to try things
out without the extra complexity of AutoKey on top of them and because you
can see error messages and add debugging output that would be much harder
to see from within AutoKey.

I did a quick search and found that calling things in the background in
Python is possible, but is definitely beyond my (very limited) Python Fu
level. The issue is that if you don't do "something" special, you lose the
output of the first command - which was the whole point of the exercise.
(I can think of a way around that too, but it gets even more convoluted.)

So, if you don't have that "Fu" level or don't get a better answer than
this one in a short while here, I would re-ask this question on Gitter
where the experts hang out.

https://gitter.im/autokey/autokey

Joe
> --
> You received this message because you are subscribed to the Google Groups
> "autokey-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to autokey-user...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>


Little Girl

unread,
Jan 27, 2019, 9:36:28 AM1/27/19
to autokey-users
If it's a Python script, you could add this to your script in the appropriate place one or more times (depending on how many tabs are needed to get to the OK button) to insert the tab(s) automatically:

keyboard.send_keys("<tab>")

Andrew Purvis

unread,
Jan 27, 2019, 12:15:13 PM1/27/19
to autokey-users
Enter, ctrl+Enter (and alt+ and super+) don't do anything, unfortunately.  Tab cycles between the menu items area, the Cancel button and the OK button.  Within the menu area, up and down arrow keys change the "focus" on the specific menu items (without any visual feedback) and pressing Enter changes the selection.

First time I've used Python, assuming Autokey scripts are Python.  Have to take a quick look into yad/zenity to see if anything looks like an obvious solutions.

Thanks for the suggestions.

Andrew Purvis

unread,
Jan 27, 2019, 12:36:52 PM1/27/19
to autokey-users
I tried putting this between the call to the dialog and processing the response.  The tabs get inserted into the window that triggered the script.  I suspect I need another script, triggered by the dialog window, that does the tabbing you suggest. 

This is the script:
-------------------
import time

choices = ["HTV beam 52", "KA2 Beam 5 (west)", "KA2 Beam 6 (east)", "ViaSat 2"]
retCode, choice = dialog.list_menu(choices,
    default = "HTV beam 52")
if retCode == 0:
    if choice == "HTV beam 52":
        keyboard.send_keys("R3N6<tab>H7VJ<tab>RTS2<tab>47BH<tab><tab><enter>")
        time.sleep(0.25)
        keyboard.send_keys("<tab><tab><tab><tab><tab>")
    if choice == "KA2 Beam 5 (west)":
        time.sleep(0.25)
        keyboard.send_keys("QJ8H<tab>G2KR<tab>9D6D<tab>PNK6<tab><tab><enter>")
        time.sleep(0.25)
        keyboard.send_keys("<tab><tab><tab><tab><tab>")
    if choice == "KA2 Beam 6 (east)":
        time.sleep(0.25)
        keyboard.send_keys("CN69<tab>X5GW<tab>5MDC<tab>RKG4<tab><tab><enter>")
        time.sleep(0.25)
        keyboard.send_keys("<tab><tab><tab><tab><tab>")
    if choice == "ViaSat 2":
        keyboard.send_keys("HBSK-WXMM-87L9-HQP7-767Q-HGCJ")
        time.sleep(0.25)
----------------------------

jos...@main.nc.us

unread,
Jan 27, 2019, 12:37:05 PM1/27/19
to autoke...@googlegroups.com
Hi.

I assumed that the OP is displaying the choice dialog using the AutoKey
API and the dialog doesn't return control to the script until it has been
completed/dismissed. That's why my reply was a bit convoluted.

If that's not the case, then we have to see how the dialog is being
displayed to see if/how tabbing, etc. may be used.

Joe

> If it's a Python script, you could add this to your script in the
> appropriate place one or more times (depending on how many tabs are needed
> to get to the OK button) to insert the tab(s) automatically:
>
> keyboard.send_keys("<tab>")
>

Little Girl

unread,
Jan 27, 2019, 12:50:20 PM1/27/19
to autokey-users
What would happen if you were to put the tabs outside of the if statement entirely, at the end of the script? Or, if that's not a solution, exactly which keystrokes are required to get the focus from where it is to where you want it to be? The https://code.google.com/archive/p/autokey/wikis/SpecialKeys.wiki page shows all the key strokes you can send and maybe you can put those into the appropriate place to get it to go there automatically.

jos...@main.nc.us

unread,
Jan 27, 2019, 1:14:04 PM1/27/19
to autoke...@googlegroups.com
Little Girl's reply exposed my assumption that your choice dialog was
generated using the AutoKey API. If that's the case, then my prior
comments stand.

If you generated the dialog some other way, we need to know how you did it
to analyze how it can be modified to get your desired behavior.

I just checked out one of my bash scripts (not used in AutoKey) which uses
a choice dialog similar to what you have described. When it popped up,
pressing Enter (and nothing else) worked fine. Below is the call that
generated it. It has a bunch of extra options in in it, so it's not an
ideal example.

If you want to actually pursue using yad, then we can go over all the
options, etc. in detail to get the arguments you need.

NEWPR="$(yad --center --on-top --list --always-print-result --title
"Configure Default Printer" --width 300 \
--text "New Default Printer" --column "Pick":RD --column "Printer":TEXT
${CHOICES})"

On my system, with two printers turned on, CHOICES (a string built by the
rest of the script) contains (inside the brackets):
CHOICES [TRUE HP1006 FALSE M2825DW ]

where the printer name with TRUE in front of it is the default choice.

A screenshot is attached to show what is displayed.

Translating that from bash into syntax Python will be happy with takes a
little doing, but is straightforward.
We can get into the details if you want to pursue it.

Joe
>> > email to autokey-user...@googlegroups.com <javascript:>.
klpset.png

jos...@main.nc.us

unread,
Jan 27, 2019, 1:23:35 PM1/27/19
to autoke...@googlegroups.com
Yes. That's the problem I was anticipating.

Unfortunately, even yad doesn't have a way to get inside one dialog from
another one that will do what you need.

Joe

jos...@main.nc.us

unread,
Jan 27, 2019, 1:35:45 PM1/27/19
to autoke...@googlegroups.com
https://github.com/autokey/autokey/wiki/Special-Keys
is the new location for those definitions, but the new page is identical
to the old one.

There's no way I know of to do what the OP wants using the dialog from the
AutoKey API because the dialog doesn't release control back to the script
while it is active, so any other keystrokes will either be too early -
before the dialog is displayed or too late - after the dialog has closed.

The same would be true using any other dialog manager I know of - unless
there's a way in the script to run it asynchronously so the call to it
returns to the script while the dialog is still displayed.

I'm quite sure that's possible to do, but it's beyond my current
understanding of Python.

Joe

Andrew Purvis

unread,
Jan 27, 2019, 1:57:16 PM1/27/19
to autokey-users
The dialog is created by Autokey, and once it's up, control doesn't return to the script until a choice has already been made - which does seem like reasonable behaviour.  That seems to rule out any manipulation of the dialog itself with Autokey.

Andrew Purvis

unread,
Jan 27, 2019, 2:02:42 PM1/27/19
to autokey-users
I am using Autokey to generate the dialog.  I'm the end user, and just created the script, so will try it for a bit and see how annoying I find it having to tab to the ok button.  If that doesn't work for my, I'll look into adding yad to the mix.

jack

unread,
Jan 28, 2019, 4:15:21 AM1/28/19
to autoke...@googlegroups.com, Andrew Purvis

you could move the default option to the end of the dialog... which would mean less tabbing to get to the OK button.

or, clunky, but would work... what about having 2 scripts, one that just applies your default option, the other that gives the remaining options? that way, if you know you want the default, then you just run the script to do that and don't need to bother with a dialog at all.

you could do what you originally wanted with a python tkinter dialog...i've never used such a thing, been having a play... i've nearly there but life takes over right now... will have another look later.


jack
hopefully the scenario of the doors not opening will disappear
Reply all
Reply to author
Forward
0 new messages