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. C#
  4. Keyboard focus in Visual Studio C#

Keyboard focus in Visual Studio C#

Scheduled Pinned Locked Moved C#
csharpvisual-studioperformancehelpquestion
8 Posts 3 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.
  • S Offline
    S Offline
    sysrev
    wrote on last edited by
    #1

    Can anyone explain to me how I can detect a key pressed and deal with it in one piece of code whatever component has the current focus. I have trawed through the help files but most of what is thrown up pre-supposes a certain level of knowledge (i.e. that you know what you are doing!). I have a little program which controls the speed and timing (on/off) of a motor and I need to be able to stop the motor when any key is pressed as quickly as possible for safetly reasons whatever control currently has the focus. regards, Ted

    Ted Edwards

    M 1 Reply Last reply
    0
    • S sysrev

      Can anyone explain to me how I can detect a key pressed and deal with it in one piece of code whatever component has the current focus. I have trawed through the help files but most of what is thrown up pre-supposes a certain level of knowledge (i.e. that you know what you are doing!). I have a little program which controls the speed and timing (on/off) of a motor and I need to be able to stop the motor when any key is pressed as quickly as possible for safetly reasons whatever control currently has the focus. regards, Ted

      Ted Edwards

      M Offline
      M Offline
      Martin 0
      wrote on last edited by
      #2

      Hello, I'm not sure if it works in every situation (actually I know it's not, because I had problems with a PDF viewer Control in this case), but you can try this in your main form. this.KeyPreview = true; this.KeyDown += new KeyEventHandler(...); this.KeyPress + = ... Hope that helps a little! All the best, Martin

      S 1 Reply Last reply
      0
      • M Martin 0

        Hello, I'm not sure if it works in every situation (actually I know it's not, because I had problems with a PDF viewer Control in this case), but you can try this in your main form. this.KeyPreview = true; this.KeyDown += new KeyEventHandler(...); this.KeyPress + = ... Hope that helps a little! All the best, Martin

        S Offline
        S Offline
        sysrev
        wrote on last edited by
        #3

        Thanks Martin, I think you have pointed me in the right direction. It works if I point each component's KeyDown event handler at the new form KeyDown event handler. I will have to differentiate between keys or the components which are suppose to accept new values from the keyboard - won't! Thanks for your help,

        Ted Edwards

        A M S 3 Replies Last reply
        0
        • S sysrev

          Thanks Martin, I think you have pointed me in the right direction. It works if I point each component's KeyDown event handler at the new form KeyDown event handler. I will have to differentiate between keys or the components which are suppose to accept new values from the keyboard - won't! Thanks for your help,

          Ted Edwards

          A Offline
          A Offline
          aSarafian
          wrote on last edited by
          #4

          You need either to grab the parents form key down event or you can set up a hook to grab the event whether this form is focused or not.

          1 Reply Last reply
          0
          • S sysrev

            Thanks Martin, I think you have pointed me in the right direction. It works if I point each component's KeyDown event handler at the new form KeyDown event handler. I will have to differentiate between keys or the components which are suppose to accept new values from the keyboard - won't! Thanks for your help,

            Ted Edwards

            M Offline
            M Offline
            Martin 0
            wrote on last edited by
            #5

            Hello, I wanted to say that it's a feature of the Forms class. You don't have to hook on the Controls eventhandler. Try it out on your main form and you will see. All the best, Martin

            S 1 Reply Last reply
            0
            • M Martin 0

              Hello, I wanted to say that it's a feature of the Forms class. You don't have to hook on the Controls eventhandler. Try it out on your main form and you will see. All the best, Martin

              S Offline
              S Offline
              sysrev
              wrote on last edited by
              #6

              Yes, I think I see what you mean - I am trying it but the new form keyboard event handler is having trouble with the statment: if (e.KeyChar >= 48 && e.KeyChar <= 57) which gives an error Error: 'System.Windows.Forms.KeyEventArgs' does not contain a definition for 'KeyChar' What you both suggested seems to be the correct approach for what I need. I will investigate further...

              Ted Edwards

              M 1 Reply Last reply
              0
              • S sysrev

                Yes, I think I see what you mean - I am trying it but the new form keyboard event handler is having trouble with the statment: if (e.KeyChar >= 48 && e.KeyChar <= 57) which gives an error Error: 'System.Windows.Forms.KeyEventArgs' does not contain a definition for 'KeyChar' What you both suggested seems to be the correct approach for what I need. I will investigate further...

                Ted Edwards

                M Offline
                M Offline
                Martin 0
                wrote on last edited by
                #7

                Hello, Looks like you are trying the KeyDown EventHandler. You have to use the KeyPress eventhandler. In my example I just wanted to point out that you have to choose the eventhandler that fitts your needs. All the best, Martin

                1 Reply Last reply
                0
                • S sysrev

                  Thanks Martin, I think you have pointed me in the right direction. It works if I point each component's KeyDown event handler at the new form KeyDown event handler. I will have to differentiate between keys or the components which are suppose to accept new values from the keyboard - won't! Thanks for your help,

                  Ted Edwards

                  S Offline
                  S Offline
                  sysrev
                  wrote on last edited by
                  #8

                  Thanks for all the help - everything is working now. A quirk was that I had event handler for form plugged in from both code and form event handler properties which compiled okay but handler was called twice when run. regards,

                  Ted Edwards

                  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