opentk / GLControl

WinForms control for OpenTK 4.x.
https://opentk.net
Other
51 stars 24 forks source link

Keyboard Event Disabled #18

Open userolle opened 2 years ago

userolle commented 2 years ago

After using this GLControl in my form, The events like KeyDown KeyPress etc don't work anymore, not just for GLControl but for the form as well. Does anyone know why and how to fix this? (KeyPreview set to true already)

MarcioAB commented 2 years ago

I have the same issue:

Steps to reproduce: (1) Create a simple Form with OnKeyDown: key down works fine. (2) Add nuget package OpenTK 4.7.1: key down works fine. (3) Drop GLControl on the Form: key down does not work anymore. (4) Add handler for GLControl (from GLControl events panel): key down does not work.

Workarround:

Use ProcessCmdKey and ProcessDialogKey in the mean time.

NogginBops commented 2 years ago

@seanofw any ideas?

seanofw commented 2 years ago

I just built and tested the GLControl Input Test program, and it's working fine: I get KeyDown and KeyUp messages, both in native and WinForms mode.

@userolle and @MarcioAB, have you tried compiling the GLControl Input Test program to see if that's misbehaving for you? I can't reproduce the failures you're describing.

MarcioAB commented 2 years ago

Before follow this route (built GLControl Input Test) I just tested with a simple WinForms application with only nuget OpenTK.WinForms (4.0.0-pre6) and KeyDown still does not work when the control is dropped into de Form. Having this simple WinForms application with only this package from OpenTK team seems to be a valid test environment.

MarcioAB commented 2 years ago

Same issue with "GLControl Input Test" program. I'm using net6.0-windows TargetFramework and not netcoreapp3.1

ttaneff commented 2 years ago

I also have run into this issue. After adding OpenTK.WinForms (4.0.0-pre.6) on NetCore 3.1 WinForms project and dropping the GLControl on the Form - onKeyDown() is not fired neither on the GLControl, nor on the host Form. However, interestingly enough when setting GLControl.Enabled = false, the OpenGL Context is still active AND the KeyDown event is being invoked on the Form...

P.S. KeyPreview = true for both test cases (MS VisualStudio Community 2019 v16.11.10)

FrancisMartin-VB commented 2 years ago

Globalement c'est un problème de Focus. Le formulaire et le glcontrol ne le reçoivent pas à l'ouverture. Pour Il suffit de changer de fenêtre et de revenir sur le formulaire ou Ajouter dans le designer du formulaire this.Shown += new System.EventHandler(this.Form1_Shown); Ajouter dans le code du formulaire private void Form1_Shown(object? sender, EventArgs e) { bool ret = Focus(); }

NogginBops commented 2 years ago

@MarcioAB are you able to try doing what is suggested here? Adding these lines to the appropriate places

this.Shown += new System.EventHandler(this.Form1_Shown);

private void Form1_Shown(object? sender, EventArgs e)
{
    bool ret = Focus();
}
MarcioAB commented 2 years ago

@NogginBops Yes, but it still does not work.

Program pass on Form1_Show(..) but still does not pass on OnKeyDown(..). And continues to pass on ProcessCmdKey(..) what I am using as a workaround.

ttaneff commented 2 years ago

After looking into the GLFW code of GLControl, I also considered that the issue is related to the focus of the NativeWindow which is created in the background. I noted that after starting the application the KeyPreviewEvent on the GLControl was called, but not passed to the Form. After minimizing and showing the form - the issue was resolved, so I forced GLControl.Focus() in the Form.Shown event. Doing what is propsed by @FrancisMartin-VB and @NogginBops is identical, and also works in my case.

MarcioAB commented 2 years ago

Over here forcing GLControl.Focus() does not solve the problem, including minimizing and showing the form. But adding GLControl.Enabled = false is another workaround: GLControl keeps working and OnKeyDown(..) starts to work.

robertk92 commented 2 years ago

I have the same issue but ProcessCmdKey and ProcessDialogKey don't work for me either. Perhaps it's caused by something else but I worked around this by calling Focus() inside OnMouseDown handler of the GLControl which is good enough for my use-case.

xPaw commented 1 year ago

It seems that one of the problems is that GLControl isn't entirely focusable/selectable. I have buttons next to the GL control, and clicking a button breaks the keyboards stuff not registering key presses. Even clicking on the GL control still appears to retain focus the button I clicked before.

If I press the tab key to cycle to the GL control, it registers key presses again.

EDIT: After some testing, I was calling GLControl.Focus in VisibilityChanged event, and the native window gains focus and then immediately loses it (GLFW unsets itself?)

MeltyPlayer commented 1 month ago

I was struggling with this issue for some time but I finally figured it out: I focused the GLControl in the mousedown event, as is done in the test example: https://github.com/opentk/GLControl/blob/4796deca94ff644cf0b345d103d85cef756b49d1/OpenTK.WinForms.InputTest/Form1.cs#L103

Hopefully this helps out any future folks who run into this same issue.

vg132 commented 6 days ago

I have also had issues with key events not triggering as expected with the GLControl. I have a form with a GLControl that takes all space on the form so no other controls on the form and this causes issues for me with form key events not triggering until I have minimized/maximized/alt-tabbed the form. Once the events started to trigger I could not get the "KeyDown" event to trigger for arrow keys. I addad a text control to the form and everything started working as expected again. So there is deffenetly something with the focus that is causing the issue.

I have not tested much with the native input events but I noticed basicly the same pattern with them.

Hope this helps someone in the future or maybe someone can find a fix :)