Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Catch key event in .NET form used as modeless dialog

Catch key event in .NET form used as modeless dialog

Scheduled Pinned Locked Moved .NET (Core and Framework)
csharphelptutorialquestion
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    wolfgangpuelm
    wrote on last edited by
    #1

    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!

    L N L 3 Replies Last reply
    0
    • W wolfgangpuelm

      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!

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      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!


      W 1 Reply Last reply
      0
      • W wolfgangpuelm

        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!

        N Offline
        N Offline
        N a v a n e e t h
        wrote on last edited by
        #3

        It worked for me. Added a button on to the form, wrote this.Close() on that button's click event handler, set that button as CancelButton of the form. It works well.

        Navaneeth How to use google | Ask smart questions

        W 1 Reply Last reply
        0
        • L Luc Pattyn

          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!


          W Offline
          W Offline
          wolfgangpuelm
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • N N a v a n e e t h

            It worked for me. Added a button on to the form, wrote this.Close() on that button's click event handler, set that button as CancelButton of the form. It works well.

            Navaneeth How to use google | Ask smart questions

            W Offline
            W Offline
            wolfgangpuelm
            wrote on last edited by
            #5

            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!

            L 1 Reply Last reply
            0
            • W wolfgangpuelm

              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!

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              • W wolfgangpuelm

                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!

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                If you are catching the Escape key in the Form's Keypress or KeyDown events, you must set the form's KeyPreview property to true.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups