Catch key event in .NET form used as modeless dialog
-
Hi, I created a form that is invoked as a modeless dialog: MyForm dlg = new MyForm(); dlg.Show(); // instead of ShowDialog() This form should be closed after a user hits the Escape key. Unfortunately, neither the assignment of a cancel button (with Dialogresult 'Cancel') to the form's property 'Cancelbutton' nor any attempt to catch a key event (KeyDown, keyPress) are working. I even tried to catch a WM_KEYDOWN (0x100) by overriding OnWndProc, no success. Does anyone have an idea where this message gets stuck or how to trap it? Thanks a lot in advance!
-
Hi, I created a form that is invoked as a modeless dialog: MyForm dlg = new MyForm(); dlg.Show(); // instead of ShowDialog() This form should be closed after a user hits the Escape key. Unfortunately, neither the assignment of a cancel button (with Dialogresult 'Cancel') to the form's property 'Cancelbutton' nor any attempt to catch a key event (KeyDown, keyPress) are working. I even tried to catch a WM_KEYDOWN (0x100) by overriding OnWndProc, no success. Does anyone have an idea where this message gets stuck or how to trap it? Thanks a lot in advance!
Hi, a dialog is modal by definition; it has focus and gets all user input (mouse, keyboard) for as long as it is visible. a modeless Form (any Form shown with Show() method) may loose focus, in which case it does not get user input events, as something else now has focus and gets them. You could organize your code in such a way that the ESC key closes a particular form no matter what, but that would take some doing and be very counterintuitive. :)
Luc Pattyn
Local announcement (Antwerp region): Lange Wapper? Neen!
-
Hi, I created a form that is invoked as a modeless dialog: MyForm dlg = new MyForm(); dlg.Show(); // instead of ShowDialog() This form should be closed after a user hits the Escape key. Unfortunately, neither the assignment of a cancel button (with Dialogresult 'Cancel') to the form's property 'Cancelbutton' nor any attempt to catch a key event (KeyDown, keyPress) are working. I even tried to catch a WM_KEYDOWN (0x100) by overriding OnWndProc, no success. Does anyone have an idea where this message gets stuck or how to trap it? Thanks a lot in advance!
It worked for me. Added a button on to the form, wrote
this.Close()
on that button's click event handler, set that button asCancelButton
of the form. It works well.Navaneeth How to use google | Ask smart questions
-
Hi, a dialog is modal by definition; it has focus and gets all user input (mouse, keyboard) for as long as it is visible. a modeless Form (any Form shown with Show() method) may loose focus, in which case it does not get user input events, as something else now has focus and gets them. You could organize your code in such a way that the ESC key closes a particular form no matter what, but that would take some doing and be very counterintuitive. :)
Luc Pattyn
Local announcement (Antwerp region): Lange Wapper? Neen!
Thanks, unfortunately, it's not a focus problem. Nevertheless, it seems that handling the 'escape' button click is the only way to get around. Still wondering why i can't trap a key event. Thanks again.
-
It worked for me. Added a button on to the form, wrote
this.Close()
on that button's click event handler, set that button asCancelButton
of the form. It works well.Navaneeth How to use google | Ask smart questions
Looks like it'll be the only way. For some reasons i would have favoured an approach to trap the key event and handle it by myself. For now, it remains a mystery to me why i can't do so. Anyway, thanks!
-
Looks like it'll be the only way. For some reasons i would have favoured an approach to trap the key event and handle it by myself. For now, it remains a mystery to me why i can't do so. Anyway, thanks!
You have to think of it in the Windows way. Under normal circumstances it is the active window that receives the keyboard input. So if you have a modeless dialog that does not have the focus then it will not receive the escape key. The only way to signal that dialog is by making it the active window.
-
Hi, I created a form that is invoked as a modeless dialog: MyForm dlg = new MyForm(); dlg.Show(); // instead of ShowDialog() This form should be closed after a user hits the Escape key. Unfortunately, neither the assignment of a cancel button (with Dialogresult 'Cancel') to the form's property 'Cancelbutton' nor any attempt to catch a key event (KeyDown, keyPress) are working. I even tried to catch a WM_KEYDOWN (0x100) by overriding OnWndProc, no success. Does anyone have an idea where this message gets stuck or how to trap it? Thanks a lot in advance!