Main Menu don't receive Keydown

I already have this kind of problem in an other desktop app.

The main menu doesn’t handle keydown events like cmd-q (Quit), cmd-n (New document)…

I remember it’s something about keydown management in my controls (I use some keydown handler on List and Text control) or maybe it’s about the focus ring?

Any idea ?

A newly added window does not have access to Menus by default… Check the Properties (sreen bottom) to add it.

My window is correctly configured. I can handle menu bar selection when the user select an item with the mouse. It’s problem is about the short-cut (cmd-q, cmd-o, cmd-n, …).I think it’s something about keydown management.

Or:

In this case, you have access with the Mouse to the Menus, but nothing from the keyboard.

Good luck.

You may be correct. Maybe, try to remove the Focus from a Control once it finished processing the Keydown Event of that Control and hand the Keydown Event over to the Window.

Taken from the Docs:

If the Window has no controls that can receive the focus, any keys that are pressed will generate KeyDown events for the Window.

Some controls, such as text fields, consume keystrokes and they never get to the window.

I identified the problem : In my window no control Allow Focus Ring was set to true. My screen is composed of 2 Listboxes. If I set Allow Focus Ring to true everything works fine.

Problem : The focus ring draw an ugly rectangle around my listbox

My problem is not resolved.

I need to click on my window to be able to press cmd-q and quit my app ?¿

It’s time to find the bug!

I just found the control that’s causing the problem: HTMLViewer

To reproduce this bug I make a small project

  • Create a desktop app
  • On Window1 just drag an HTMLViewer
  • Add an Open menu item in File menu with cmd-o key
  • run the project
  • Press cmd-o or select Open
  • The Windows1 is shown
  • Press cmd-o or cmd-q have no effect
  • Click anywhere in the window
  • cmd-o open a new window and cmd-q quit the app

You can find the project here

Add an issue and add the project to it. Then a Xojo engineer will take a look.

https://tracker.xojo.com/xojoinc/xojo/-/issues/
and hit the “New issue” button.

Describe the problem, even better provide a project (which you have) and the steps to reproduce that problem (which you have). :slight_smile:

1 Like

I don’t find a The solution but a trick :

I just create a Textfield and put in the window’s controls (out of the user view)
I set the focus to this not shown Textfield

it’s a trick not a solution.

Yes, that is a workaround, but if you report the issue that the key is swallowed then Xojo can look into solving the route cause of the problem, removing the need to work around the issue.

I just opened a issue

https://tracker.xojo.com/xojoinc/xojo/-/issues/72423

Quite a cool bug find!

1 Like

I don’t think there’s anything new about this problem. The HTMLViewer is a bit different to other controls. The renderer software that drives it will simply absorb any keypresses. If you want cmd-q to quit, you have to put some javascript in the HTMLViewer that catches keypresses, and passes each one to the parent control via, for example, the control’s title or error event. Then you either interpret the characters yourself and call Quit, or somehow raise the KeyDown event on the parent (if that’s even possible).

Edit: I did try one thing: exchange the HTMLViewer for a TextArea. Then, after pressing cmd-o to open your test app’s window, I found that cmd-q quit it immediately - no need to click in the window.

The HTMLViewer has probably not fully initialised to stop absorbing the keypress as its not shown any content yet, if you add the following to the HTMLViewer1.Opening event, it works as expected.

Me.LoadURL("about:blank")

1 Like

Or:

me.LoadPage ("", Nil)

in the Opening event.

1 Like

Thank you @anon20074439

This is the solution

I will update my bug/ report and suggest a default LoadURL / LoadPage was set in contructor