Keydown event under Windows

I use last Xojo version 2021r1. I develop under Mac for Mac and some of my applications run under Windows.
I just realised that on one of my application a Keydown event in a Window doesn’t fire anymore. This window only contains a Canvas (and Threads and Timers).
I simply wrote "MessageBox “key " + Key” in this event Keydown of the window but I only get a Beep.
I create a new window which only have a keydown event with a Messagebox but it doesn’t fire neither.

I have another application with a Keydown event in a window which works fine.

Is there a settings somewhere to make Windows fire Keydown event? It works on Mac.

Thank you, I don’t see “Allow focus” in the properties of a window but I did some test. If I allow focus on my Canvas then the Keydown event of the window fire. If I disable it and add a button (whish does nothing) then it works too.

On the other program where it works, the Canvas (unique object too) hasn’t the “Allow focus” checked. But I have old timer that I added in old version of Xojo (RealBasic or RealStudio) which have the “Tab panel index” properties. This does not exists on new Timer. I will replace those old timer by new one and see what happens. I suppose the Keydown event of the window won’t work anymore.
It seems that the window needs an object which can have the focus in order to work.
I investigate further and will fill a bug report if I find something.

This window has no control at all?
(and you’re not opening another window which may conflict?)

In the two program I have another window.
In the program which work, the other window have not any Keydown event.
In the program which doesn’t work, the window has a Keydown event which launch the same Method as the Main window (the two event have a single line which call a Method ( KeyPressed(Key) ).

Markus, my Keydown event is in the Window event, not in the Canvas event.

1 Like

Using MsgBox is a bad idea.

No beep does means nothing (only the key press was handled).

In Firefox, pressing a key without any text object to receive the key down does not send a beep…

There is no MsgBox usually, I just replace my code by a MsgBox to verify the event fire (and it does not). And I have a Beep, I don’t know from where it come from, I have some Beep in my code but as my KeyDown event does not fire, I hope it does not fire another event when I hit a key.

Could you replicate your case in a small sample?

1 Like

No I can’t. I created a new project, added a Canvas (its “Allow focus” property was uncheck) and I added a MessageBox in the event KeyDown of the window and it works as expected.

Some times ago we were speaking about “xojo_xml_project” and I saved one of my project in this format just to see. I’m not sure but I remember I saw that some settings which are now obsoletes and cannot be changed in Xojo (because no object to change them in the Xojo IDE) are still in the XML file. Do you think those hidden old settings could change something? And why are they still there?

Only Xojo can answer that.

Pfffff, those kind of bugs are frustrating.
I wrote I have one another window, in fact I have 2 others more windows (which are hidden, not closed). But those windows have a KeyDown event which launch the same Method.

And I found what to do to resolve the problem : I launch my application, I distribute cards (it’s a memory game) and I hit key, I have the problem. I have a small window which display the Time with a CheckBox to play or not the sound TicTac. I click this checkbox and it works fine. If I show (because hidden) one of the other window and I click on of their controls (ListBox, close button), all works fine too.

If you’re curious and have a windows System, you can download the Memory game Neuronyx
It is donationware, in the remind window which will appear, just click the “Never” button.
Note : In the Options menu there is “Show keys” which display keys on the card to play with keyboard instead of cards). A woman asked me this feature many years ago to her child who was blind.

This is a sample code which reproduce the problem. It appears after I open and close another window (problem windows only, no problem on Mac).

  • Run the code
  • Hit a key of your keyboard → it works
  • Menu Edit - WinControl or WinScores (it open a window)
  • Hit a key of your keyboard → it works
  • Close the window you just opened
  • Hit a key of your keyboard → Beep, it bugs
  • Menu Edit - WinControl or WinScores (it open a window)
  • Hit a key of your keyboard → Beep, it bugs
  • Click an object in one of the windows Scores or Control (listbox, checkbox, reset button)
  • Hit a key of your keyboard → it works
  • Menu Edit - ShowModalWin (it open a ModalWindow)
  • Hit a key of your keyboard → Beep (there is not Keydown event in this window)
  • Close this ModalWindow
  • Hit a key of your keyboard → Beep, it bugs
  • Click an object in the windows Scores or Control (listbox, checkbox, reset button)
  • Hit a key of your keyboard → it works

Another things, while it’s buggy (beep when you hit a key), go to the debugger, put it in Pause, and resume it. Now it’s ok, no more bug when you hit a key until you open and close one of the additional window.

As far as I understand, if no object has the focus, the keydown event of the window does not fire.
It took me many time to find the problem and describe it in a sample code. I hope it will help Xojo team to resolve it quickly. :slight_smile:

I see some crazy menu bar behaviors. Go to edit your menu bar, right click on any item.
Lots of repeated Handlers pop up.

When debugging the app, it seems to enter into an eternal loop calling App.EnableMenuItems. I have put a break point here:

Sub WinControle.BoutonFermer.Action() Handles Action
End Sub

And followed the step and it loops there

You need to report it using the feedback app to get some deeper answer. Maybe something silly, maybe a collection of bugs in a meeting. :upside_down_face:

the menu access in app open event is the wrong place.
remove also Self.Show in WinMain.Open
use it outside in app open WinMain.Show (or by default visible=true)
this timer in winscores make trouble, you could remove it and enable the has close button.
you could also try to disable the implicit instance and create and close the windows.
myself using the window behavior visible with default to true.
after this few changes the focus and keyinput in main work after open and close the score window, at windows 10 and 2021r1.

i think using Self.Close at a implicit instance is wrong.

Thank you for yours answers. In the event, I add some menus I use in DebugMode. I remove those lines.
I also removed the Self.Show in the WinMain.Open event and I set the visible of WinMain to True.
I removed the Timer.Close in WinScores.
All of those lines of code are for my game Neuronyx, they don’t have anything to do with the problem describe. All my tests have the same results.
I never used “implicit instance”, it is always set to the default. Then I leave it to True.
I think that what changed for you if you set it to False.

Note : If you set the “Allow focus” of the Canvas of WinMain to True, the problem disappears.

I don’t have the infinite loop you describe Rick.

I will feel a FeedBack. Thank you.

Add this event to your WinControle Window

Sub WinControle.Close() Handles Close
End Sub

And the keyboard control will return to WinMain after WinControle has been closed.

Do something like that wherever you need to.

that is the meaning of implicit instance,
you can use the class name without creating the object by the new keyword


usually you would create windows in this way

var w as new WinScore

at least by removing (for me) uncommon source code in some methods the problem gone and if me closed other window the input in main window gain focus again and got key down events.