Eto.Forms Call UI With Shortcut Regardless Of Focus


I have this bit of code:

        # self.CreateSearchBox()  # Call To Create SearchBox Instance
        self.CreateUI()  # Call Initially To Create Background UI

        # Initialize a flag to keep track of Ctrl key state
        self.ctrl_pressed = False

        # Create the key listener

    def CreateKeyListener(self):
        self.KeyDown += self.OnKeyDown  # Subscribe to KeyDown event
        self.KeyUp += self.OnKeyUp  # Subscribe to KeyUp event

    def OnKeyDown(self, e):
            if e.Key == Eto.Forms.Keys.LeftControl:
                self.ctrl_pressed = True  # Set the flag to True when Ctrl is pressed
        except Exception as ex:
            Rhino.RhinoApp.WriteLine(f"Error in OnKeyDown: {ex}")

    def OnKeyUp(self, e):
        global show_tray
        global show_search

            if e.Key == Eto.Forms.Keys.F and self.ctrl_pressed:
                self.ctrl_pressed = False
                self.update_button_state(1, True)
                show_search = show_tray = True

            elif e.Key == Eto.Forms.Keys.Escape:
                self.update_button_state(1, False)
                show_search = show_tray = False

        except Exception as ex:
            Rhino.RhinoApp.WriteLine(f"Error in OnKeyUp: {ex}")

That “expands” my UI when the mouse is over the Form AND Ctrl+F is pressed.

Hitting Escape will “contract” close the expanded UI.

My question is, while I like that this allows me to have focus on my form and doesn’t effect the viewport having focus to be able to select things/do things, can I listen for this shortcut without needing to have my Eto.Form in focus/being hovered on?

Thanks for any guidance!

I believe KeyDown/KeyUp will only fire if your form has focus. If you want to capture the key presses outside of your form you’ll need to actually capture them outside your form, i.e from the parent, which I assume in this case is Rhino.

1 Like

Understood, I’ll dig into that, thank you