- Global keycode-based handler for Ctrl shortcuts (works with Russian, Chinese, any layout) - Tkinter maps <<Paste>> to <Control-v> by keysym which fails on non-Latin layouts - New handler intercepts <Control-Key> at 'all' level, checks keycodes and generates correct virtual events - Added Undo/Redo support for Entry widgets (tk.Entry has no built-in undo) - Tab switch focus management: terminal releases focus when switching away Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
51 lines
1.4 KiB
Python
51 lines
1.4 KiB
Python
"""
|
|
Ctrl+Z / Ctrl+Y undo/redo for CTkEntry widgets.
|
|
tk.Entry has no built-in undo, so we track history manually.
|
|
"""
|
|
|
|
|
|
def enable_undo(ctk_entry):
|
|
"""Add Ctrl+Z undo and Ctrl+Y redo to a CTkEntry widget."""
|
|
entry = ctk_entry._entry
|
|
history = [entry.get()]
|
|
redo_stack = []
|
|
_recording = [True]
|
|
|
|
def _snapshot(*_args):
|
|
if not _recording[0]:
|
|
return
|
|
val = entry.get()
|
|
if not history or history[-1] != val:
|
|
history.append(val)
|
|
redo_stack.clear()
|
|
if len(history) > 200:
|
|
del history[:100]
|
|
|
|
def _undo(event):
|
|
if len(history) > 1:
|
|
redo_stack.append(history.pop())
|
|
_recording[0] = False
|
|
entry.delete(0, "end")
|
|
entry.insert(0, history[-1])
|
|
_recording[0] = True
|
|
elif history:
|
|
redo_stack.append(history.pop())
|
|
_recording[0] = False
|
|
entry.delete(0, "end")
|
|
_recording[0] = True
|
|
return "break"
|
|
|
|
def _redo(event):
|
|
if redo_stack:
|
|
val = redo_stack.pop()
|
|
history.append(val)
|
|
_recording[0] = False
|
|
entry.delete(0, "end")
|
|
entry.insert(0, val)
|
|
_recording[0] = True
|
|
return "break"
|
|
|
|
entry.bind("<KeyRelease>", _snapshot, add="+")
|
|
entry.bind("<Control-z>", _undo)
|
|
entry.bind("<Control-y>", _redo)
|