Fix Command-V for API key fields and add paste buttons

This commit is contained in:
Vickenty Zherebov 2026-10-20 12:58:35 +03:00
parent f88f6052b0
commit 8d6dc1ab22
4 changed files with 63 additions and 3 deletions

View file

@ -2,4 +2,4 @@
from __future__ import annotations
__version__ = "0.3.0b5"
__version__ = "0.3.0b6"

View file

@ -0,0 +1,34 @@
"""Explicit paste bindings for packaged Tk entry widgets on macOS."""
from __future__ import annotations
import sys
import tkinter as tk
from collections.abc import Callable
import customtkinter as ctk
def paste_into(entry: ctk.CTkEntry, reader: Callable[[], str] | None = None) -> str:
"""Paste without exposing clipboard data, replacing the current selection."""
try:
value = (reader or entry.clipboard_get)()
if entry.select_present():
entry.delete("sel.first", "sel.last")
entry.insert("insert", value)
except tk.TclError:
pass # An empty/non-text clipboard is harmless.
return "break"
def bind_paste(entry: ctk.CTkEntry, reader: Callable[[], str] | None = None) -> None:
"""Handle Command-V in English and Russian layouts on macOS."""
if sys.platform != "darwin":
return
def on_command(event: tk.Event[tk.Misc]) -> str | None:
if event.keysym in {"v", "V", "Cyrillic_em", "Cyrillic_EM", "м", "М"}:
return paste_into(entry, reader)
return None
entry.bind("<Command-KeyPress>", on_command)

View file

@ -69,6 +69,12 @@ def build_recognition_tab(
key_entry = ctk.CTkEntry(cloud_frame, show="•", width=460, placeholder_text="OpenAI API-ключ")
key_entry.pack(anchor="w", padx=12, pady=4)
from voice_dictation.ui.entry_shortcuts import bind_paste, paste_into
bind_paste(key_entry)
ctk.CTkButton(cloud_frame, text="Вставить ключ", command=lambda: paste_into(key_entry)).pack(
anchor="w", padx=12, pady=4
)
key_status = ctk.CTkLabel(
cloud_frame,
text="Ключ сохранён"

View file

@ -10,6 +10,7 @@ import customtkinter as ctk
from voice_dictation.core.config import ConfigStore
from voice_dictation.infrastructure.credentials import read_api_key, save_api_key
from voice_dictation.ui.entry_shortcuts import bind_paste, paste_into
def run_setup(root: Path, store: ConfigStore, *, smoke_test: bool = False) -> bool:
@ -33,6 +34,9 @@ def run_setup(root: Path, store: ConfigStore, *, smoke_test: bool = False) -> bo
ctk.CTkLabel(content, text="OpenAI API-ключ или строка OPENAI_API_KEY=…:").pack()
key = ctk.CTkEntry(content, show="•", width=540, placeholder_text="Вставьте ключ здесь")
key.pack(pady=6)
test_reader = (lambda: "sk-test-not-a-real-key") if smoke_test else None
bind_paste(key, test_reader)
ctk.CTkButton(content, text="Вставить ключ", command=lambda: paste_into(key)).pack(pady=4)
ctk.CTkLabel(
content,
text="Ключ сохраняется отдельно в вашем профиле с правами 0600.\n"
@ -91,6 +95,7 @@ def run_setup(root: Path, store: ConfigStore, *, smoke_test: bool = False) -> bo
hotkey = ctk.CTkEntry(content, width=400)
hotkey.insert(0, config.hotkey)
hotkey.pack()
bind_paste(hotkey)
message = ctk.CTkLabel(
content,
text="После запуска: хоткей → фраза → хоткей → проверьте вставку.\n"
@ -147,10 +152,25 @@ def run_setup(root: Path, store: ConfigStore, *, smoke_test: bool = False) -> bo
refresh()
if smoke_test:
backend.set("OpenAI API")
key.insert(0, "sk-test-not-a-real-key")
def test_paste() -> None:
key.focus_set()
window.update()
focused = window.focus_get()
if focused is not None:
focused.event_generate("<Command-KeyPress-v>")
if key.get() != "sk-test-not-a-real-key":
raise RuntimeError("Synthetic Command-V test failed")
key.select_range(0, "end")
if focused is not None:
focused.event_generate("<Command-KeyPress-Cyrillic_em>")
if key.get() != "sk-test-not-a-real-key":
raise RuntimeError("Russian layout paste/selection test failed")
finish()
hotkey.delete(0, "end")
hotkey.insert(0, "cmd+space")
window.after(200, finish)
window.after(300, test_paste)
window.after(700, window.destroy)
window.mainloop()
return completed