Tuesday, March 25, 2025

Dismissing the Context Menu with ESC

That's the menu that an app gives you when you RMB click

Fortunately, only the xschem.tcl had to be changed. Dankeschoen Stefan

-    bind $topwin <KeyPress> "xschem callback %W %T %x %y %N 0 0 %s"

+    bind $topwin <KeyPress> "

+      if {{%K} eq {Escape}} { destroy .ctxmenu }

+      xschem callback %W %T %x %y %N 0 0 %s"

What did chatGPT come up with?

Nice — the file defines a custom right-click context menu using a toplevel window named .ctxmenu, created inside a procedure called context_menu.

To allow dismissing this menu with the ESC key, you can bind the Escape key to destroy .ctxmenu. Here's what to add inside the context_menu procedure after .ctxmenu is created:

So the relevant part of the context_menu procedure should look like this:

proc context_menu { } { toplevel .ctxmenu wm overrideredirect .ctxmenu 1 bind .ctxmenu <Escape> { destroy .ctxmenu } ... }

Did it work? No:)

Why do you expect that to work? It didn't.

You're absolutely right to question that — let’s dig deeper and clarify why simply binding <Escape> to .ctxmenu might not work, and how to correctly dismiss the context menu with the ESC key in Tcl/Tk.

⚠️ The Problem:

In your code, the context menu is a toplevel widget (.ctxmenu) created manually — it's not a standard menu widget or a popup. Since it's overrideredirect, it has no window manager decorations or focus by default, meaning:

It doesn’t receive keyboard focus, so key bindings like <Escape> won’t work unless you explicitly give it focus.

✅ The Real Fix:

To ensure the ESC key works, you must:

Give .ctxmenu focus using focus or focus -force.

Then bind the <Escape> key to it.

Here’s what you should insert in proc context_menu immediately after toplevel .ctxmenu:

focus -force .ctxmenu bind .ctxmenu <Escape> { destroy .ctxmenu }

Did *that* work? Yes :)

Lesson, I could have done this myself and then let SS know that he could pull it :) Earned some points :)

No comments:

Post a Comment

Who Needs Cadence Anymore (for Schematic Tree Reporting that is :)? Make Your Own Reporter with chatGPT!

You know what I mean, if you've used Cadence - the SHIFT-T thing that gives you the schematic tree can be VERY useful, at times. No Cade...