Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Complex Keybinds #499

Closed
robertmlinden opened this issue Jul 30, 2018 · 9 comments
Closed

Complex Keybinds #499

robertmlinden opened this issue Jul 30, 2018 · 9 comments
Labels
Milestone

Comments

@robertmlinden
Copy link

Menu keybinds such as 'Control-Shift-R' are unresponsive.

IBM Developer Thinkpad
0.93.0

@robertmlinden
Copy link
Author

Python 3.6.6

@jarvisteach
Copy link
Owner

Just had a play with this, with the following code:

from appJar import gui 

def keyPress(key): print(key, 'pressed')

with gui() as app:
    app.label('hello world')
    app.addMenuItem('a', 'a', shortcut='Control-Key-1', func=keyPress)
    app.addMenuItem('a', 'b', shortcut='Control-Key-2', func=keyPress)
    app.addMenuItem('a', 'c', shortcut='Control-Shift-T', func=keyPress)

When I run the code in trace mode (-t flag), I get the following output:

2018-07-31 07:03:21,132 appJar:TRACE [Line 13->10570/addMenuItem]: Adding accelerator: Ctrl-1
2018-07-31 07:03:21,133 appJar:TRACE [Line 13->10574/addMenuItem]: Binding: <Control-Key-1> to <function gui.MAKE_FUNC.<locals>.<lambda> at 0x1028a1840>
2018-07-31 07:03:21,134 appJar:TRACE [Line 14->10570/addMenuItem]: Adding accelerator: Ctrl-2
2018-07-31 07:03:21,135 appJar:TRACE [Line 14->10574/addMenuItem]: Binding: <Control-Key-2> to <function gui.MAKE_FUNC.<locals>.<lambda> at 0x1028a17b8>
2018-07-31 07:03:21,136 appJar:TRACE [Line 15->10570/addMenuItem]: Adding accelerator: Ctrl-Shift-T
2018-07-31 07:03:21,137 appJar:TRACE [Line 15->10574/addMenuItem]: Binding: <Control-Shift-t> to <function gui.MAKE_FUNC.<locals>.<lambda> at 0x1028a18c8>

And, when I test it, it seems to work:

keys

@jarvisteach
Copy link
Owner

A couple of things to point out here though:

  • The menu name is passed as a parameter to the function, not the shortcut that was pressed.
  • The syntax for the shortcut is different than the syntax for key bindings (putting angle brackets on menu shortcuts breaks them)

@robertmlinden
Copy link
Author

Thanks for confirmation, I'll have to run using the -t flag and see what I come up with!

@robertmlinden
Copy link
Author

Sorry for the delay. I've come back to this issue and it doesn't seem to be resolved. 'Control-Shift-H' is not responding. Luckily, 'Control-H' isn't activated when I try the former keystroke, but it would be nice if this complex keybind worked. I'll be back with a -t-flag stacktrace.

jarvisteach pushed a commit that referenced this issue Aug 13, 2018
jarvisteach added a commit that referenced this issue Aug 13, 2018
Stripped out most of the code trying to format shortcuts/bindings for menus
@jarvisteach
Copy link
Owner

@robertmlinden - I've done a fair bit of digging into this today, and safe to say, it's all a bit of a mess.

The default behaviour in tkinter is that keyboard bindings are case-sensitive, so the binding Control-Shift-h won't fire as holding shift makes h uppercase (unless caps lock is on). Equally, a binding of Control-H will also fire when Control-Shift-h is pressed (or Control-h with caps-lock on).

This is different to how menus work in other applications. They seem to be case-insensitive, and will NOT match in the second example, where holding shift should generate a different event.

This is consistent across both Mac & Windows when creating bindings.

I think the case-insensitivity can be dealt with: by registering two bindings each time.
However, the issue with modifier keys looks more tricky and will probably take much more effort to get it right.

There is another level of complexity with menu bindings, as the shortcut text also needs to be generated and added to the menu. The chevrons need to be removed, words like Control need to be shortened to Ctrl and special words like Key need to be removed.

Furthermore, it seems that adding shortcuts to Mac menus, creates the bindings - and case insensitive ones at that! Which is not what the docs say should happen!

For now, I'm going to strip out all the extra code I put in for registering menu shortcuts, and just have it do exactly what the user requests, and then start adding it back in from there.

@robertmlinden
Copy link
Author

Thank you, I'll work with simple keybinds for now!

jarvisteach added a commit that referenced this issue Aug 14, 2018
Also,  added table properties #517
jarvisteach added a commit that referenced this issue Aug 14, 2018
jarvisteach added a commit that referenced this issue Aug 15, 2018
Also, more tidying up...
@jarvisteach
Copy link
Owner

OK - I've tested this on windows/Mac/Pi and it works consistently how I want it across all platforms.

There are two areas that might not be ideal:

  1. If Shift is included in the shortcut, then Shift must be pressed. However, if Shift is not included in a shortcut, but it is pressed, the binding may still fire. The only way to resolve this is to add a filter function - I'm not going to do that for now.
  2. Bindings are now case-insensitive, both upper & lowercase bindings are created. This means you can't have two different shortcuts: Ctrl-E & Ctrl-e. I think this is fine.

@jarvisteach jarvisteach added bug and removed question labels Aug 15, 2018
@jarvisteach jarvisteach added this to the 1.0 milestone Aug 15, 2018
@jarvisteach
Copy link
Owner

NB. None of this has been done to the bindKey() function - I'm leaving that for the menu refactoring - issue #305

jarvisteach added a commit that referenced this issue Aug 15, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants