# Keydown event under Windows

**URL:** <https://forum.xojo.com/t/keydown-event-under-windows/62652>\
**Category:** Windows\
**Created:** [April 11, 2021, 11:31am UTC](https://forum.xojo.com/t/keydown-event-under-windows/62652 "2021-04-11T11:31:47Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thomas\_ROBISSON](https://forum.xojo.com/user_avatar/forum.xojo.com/thomas_robisson/32/15868_2.png) [@Thomas\_ROBISSON](https://forum.xojo.com/u/Thomas_ROBISSON)\
**Post date:** [April 11, 2021, 11:31am UTC](https://forum.xojo.com/t/keydown-event-under-windows/62652/1 "2021-04-11T11:31:47Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Thomas\_ROBISSON](https://forum.xojo.com/user_avatar/forum.xojo.com/thomas_robisson/32/15868_2.png) [@Thomas\_ROBISSON](https://forum.xojo.com/u/Thomas_ROBISSON)\
**Post date:** [April 11, 2021, 1:13pm UTC](https://forum.xojo.com/t/keydown-event-under-windows/62652/3 "2021-04-11T13:13:53Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Arnaud\_N](https://forum.xojo.com/user_avatar/forum.xojo.com/arnaud_n/32/21824_2.png) [@Arnaud\_N](https://forum.xojo.com/u/Arnaud_N)\
**Post date:** [April 11, 2021, 1:23pm UTC](https://forum.xojo.com/t/keydown-event-under-windows/62652/5 "2021-04-11T13:23:05Z")

</div>

> [@Thomas\_ROBISSON](#):
>
> I create a new window which only have a keydown event with a Messagebox but it doesn’t fire neither.

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

---

<div class="post-metadata">

**Author:** ![Thomas\_ROBISSON](https://forum.xojo.com/user_avatar/forum.xojo.com/thomas_robisson/32/15868_2.png) [@Thomas\_ROBISSON](https://forum.xojo.com/u/Thomas_ROBISSON)\
**Post date:** [April 11, 2021, 1:26pm UTC](https://forum.xojo.com/t/keydown-event-under-windows/62652/6 "2021-04-11T13:26:26Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Emile\_Schwarz](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/90ced4/32.png) [@Emile\_Schwarz](https://forum.xojo.com/u/Emile_Schwarz)\
**Post date:** [April 11, 2021, 2:08pm UTC](https://forum.xojo.com/t/keydown-event-under-windows/62652/7 "2021-04-11T14:08:01Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![Thomas\_ROBISSON](https://forum.xojo.com/user_avatar/forum.xojo.com/thomas_robisson/32/15868_2.png) [@Thomas\_ROBISSON](https://forum.xojo.com/u/Thomas_ROBISSON)\
**Post date:** [April 11, 2021, 2:22pm UTC](https://forum.xojo.com/t/keydown-event-under-windows/62652/8 "2021-04-11T14:22:36Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Rick\_Araujo](https://forum.xojo.com/user_avatar/forum.xojo.com/rick_araujo/32/251_2.png) [@Rick\_Araujo](https://forum.xojo.com/u/Rick_Araujo)\
**Post date:** [April 11, 2021, 3:15pm UTC](https://forum.xojo.com/t/keydown-event-under-windows/62652/9 "2021-04-11T15:15:30Z")

</div>

Could you replicate your case in a small sample?

---

<div class="post-metadata">

**Author:** ![Thomas\_ROBISSON](https://forum.xojo.com/user_avatar/forum.xojo.com/thomas_robisson/32/15868_2.png) [@Thomas\_ROBISSON](https://forum.xojo.com/u/Thomas_ROBISSON)\
**Post date:** [April 11, 2021, 4:06pm UTC](https://forum.xojo.com/t/keydown-event-under-windows/62652/10 "2021-04-11T16:06:43Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Rick\_Araujo](https://forum.xojo.com/user_avatar/forum.xojo.com/rick_araujo/32/251_2.png) [@Rick\_Araujo](https://forum.xojo.com/u/Rick_Araujo)\
**Post date:** [April 11, 2021, 4:16pm UTC](https://forum.xojo.com/t/keydown-event-under-windows/62652/11 "2021-04-11T16:16:43Z")

</div>

> [@Thomas\_ROBISSON](#):
>
> Some time ago we were speaking about “xojo xml project format” and I saved one of my projects in this format… I’m not sure but I remember I saw that some settings which are now obsolete (and cannot be changed in the 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.

---

<div class="post-metadata">

**Author:** ![Thomas\_ROBISSON](https://forum.xojo.com/user_avatar/forum.xojo.com/thomas_robisson/32/15868_2.png) [@Thomas\_ROBISSON](https://forum.xojo.com/u/Thomas_ROBISSON)\
**Post date:** [April 11, 2021, 6:48pm UTC](https://forum.xojo.com/t/keydown-event-under-windows/62652/12 "2021-04-11T18:48:20Z")

</div>

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](https://throb.pagesperso-orange.fr/site/Prg_JeuxRB.html#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.

---

<div class="post-metadata">

**Author:** ![Thomas\_ROBISSON](https://forum.xojo.com/user_avatar/forum.xojo.com/thomas_robisson/32/15868_2.png) [@Thomas\_ROBISSON](https://forum.xojo.com/u/Thomas_ROBISSON)\
**Post date:** [April 12, 2021, 11:28am UTC](https://forum.xojo.com/t/keydown-event-under-windows/62652/13 "2021-04-12T11:28:51Z")

</div>

[Neuronyx-KeyDwn](https://throb.pagesperso-orange.fr/Documents/Neuronyx-KeyDwn.zip)  
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. 🙂

---

<div class="post-metadata">

**Author:** ![Rick\_Araujo](https://forum.xojo.com/user_avatar/forum.xojo.com/rick_araujo/32/251_2.png) [@Rick\_Araujo](https://forum.xojo.com/u/Rick_Araujo)\
**Post date:** [April 12, 2021, 12:18pm UTC](https://forum.xojo.com/t/keydown-event-under-windows/62652/14 "2021-04-12T12:18:05Z")

</div>

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:

```auto
Sub WinControle.BoutonFermer.Action() Handles Action
  
  Self.Close
 
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. 🙃

---

<div class="post-metadata">

**Author:** ![MarkusR](https://forum.xojo.com/user_avatar/forum.xojo.com/markusr/32/685_2.png) [@MarkusR](https://forum.xojo.com/u/MarkusR)\
**Post date:** [April 12, 2021, 5:35pm UTC](https://forum.xojo.com/t/keydown-event-under-windows/62652/15 "2021-04-12T17:35:37Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Thomas\_ROBISSON](https://forum.xojo.com/user_avatar/forum.xojo.com/thomas_robisson/32/15868_2.png) [@Thomas\_ROBISSON](https://forum.xojo.com/u/Thomas_ROBISSON)\
**Post date:** [April 12, 2021, 5:50pm UTC](https://forum.xojo.com/t/keydown-event-under-windows/62652/16 "2021-04-12T17:50:58Z")

</div>

Thank you for yours answers. In the App.open 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.

---

<div class="post-metadata">

**Author:** ![Rick\_Araujo](https://forum.xojo.com/user_avatar/forum.xojo.com/rick_araujo/32/251_2.png) [@Rick\_Araujo](https://forum.xojo.com/u/Rick_Araujo)\
**Post date:** [April 12, 2021, 6:23pm UTC](https://forum.xojo.com/t/keydown-event-under-windows/62652/17 "2021-04-12T18:23:23Z")

</div>

Add this event to your WinControle Window

```auto
Sub WinControle.Close() Handles Close
  WinMain.SetFocus
End Sub

```

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

Do something like that wherever you need to.

---

<div class="post-metadata">

**Author:** ![MarkusR](https://forum.xojo.com/user_avatar/forum.xojo.com/markusr/32/685_2.png) [@MarkusR](https://forum.xojo.com/u/MarkusR)\
**Post date:** [April 12, 2021, 6:43pm UTC](https://forum.xojo.com/t/keydown-event-under-windows/62652/18 "2021-04-12T18:43:20Z")

</div>

> [@Thomas\_ROBISSON](#):
>
> I never used “implicit instance”, it is always set to the default. Then I leave it to True.

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

`WinMain.`  
`WinScores.`

usually you would create windows in this way

```
var w as new WinScore
w.DoSomething
w.Show

```

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.
