ragardner / tksheet

Python tkinter table widget for displaying tabular data
https://pypi.org/project/tksheet/
MIT License
400 stars 48 forks source link

Is there a way to set copy and paste bindings on cyrillic layout? #223

Closed serghPeppa closed 4 months ago

serghPeppa commented 4 months ago

In Cyrillic layout copy and paste works on same keys but 'v' is 'м'(em), and 'c' is 'с'(es). And when i change layout to Russian or Ukrainian copy and paste stops working in sheet. keyboard image

serghPeppa commented 4 months ago

Also i have this peace of code that work for my window, except sheet, maybe this can help you

def _onKeyRelease(event):
    ctrl  = (event.state & 0x4) != 0

    if event.keycode==86 and  ctrl and event.keysym.lower() != "v": 
        event.widget.event_generate("<<Paste>>")

    if event.keycode==67 and  ctrl and event.keysym.lower() != "c":
        event.widget.event_generate("<<Copy>>")

main.bind_all("<Key>", _onKeyRelease, "+")
ragardner commented 4 months ago

Hello,

Thanks for your question and the interesting code snippet,

I am not sure how best to get those custom events working in tksheet at the moment, it will take some experimentation if I do decide to implement it

In the meantime, would this solution work for you? You can replace the characters with whichever you need

sheet.set_options(paste_bindings=["<Control-м>", "<Control-М>"])

More info here: https://github.com/ragardner/tksheet/wiki/Version-7#changing-key-bindings

serghPeppa commented 4 months ago

It's not working if i type cyrillic symbol inside it. It gives this error: _tkinter.TclError: bad event type or keysym "с" Maybe you can make an overload of this option, where I will be able to use keycode instead of keysym? This will immediately solve my problem.

ragardner commented 4 months ago

Hello,

I am working on this and I think I have a solution that will make the code you provided work for tksheet, meaning that widget.event_generate("<<Copy>>") will tell tksheet to run the copy function.

I am not currently working on ways of binding the keycodes though sorry, this seems a bit more complex and would warrant more investigation.

There will also separately be an optional way of specifically binding the "<<Copy>>" event to tksheet.

Your help has been invaluable in figuring this out thank you, I will let you know when there is a new release

serghPeppa commented 4 months ago

I'm very glad to hear this! I hope you can do this. Thank you for your work!

ragardner commented 4 months ago

Hello,

I have released a new version which should allow you to call event_generate() on a Sheet, with "<<Copy>>" and so also for paste, delete, cut, undo, redo and selectall

Some new documentation here: https://github.com/ragardner/tksheet/wiki/Version-7#tkinter-and-tksheet-events

Version 7.1.22

Addressed:

Fixed:

Changed:

Added:

Improved:

serghPeppa commented 4 months ago

You are a genius! You solve it so quick and elegant! Thank you! But there was a little issue, if you want your sheet to be readonly you need to set filter on Canvas in your <<Paste>> event:

def _onKeyRelease(event):
    ctrl  = (event.state & 0x4) != 0
    if event.keycode==86 and  ctrl and event.keysym.lower() != "v": 
        if event.widget.winfo_class() not in ("Canvas"):
            event.widget.event_generate("<<Paste>>")

    if event.keycode==67 and  ctrl and event.keysym.lower() != "c":
        event.widget.event_generate("<<Copy>>")

main.bind_all("<Key>", _onKeyRelease, "+")

But for me it's 100% resolved. And again, thank you so much!