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. key press event

key press event

Scheduled Pinned Locked Moved C#
csharpquestion
5 Posts 4 Posters 1 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.
  • P Offline
    P Offline
    Parshant Verma
    wrote on last edited by
    #1

    Hi how can i use key press event in c#

    B 1 Reply Last reply
    0
    • P Parshant Verma

      Hi how can i use key press event in c#

      B Offline
      B Offline
      baerten
      wrote on last edited by
      #2

      Hi You can add for each component of the form a key_Down Event for example : the form is called xxx private: System::Void xxx_KeyDown(System::Object * sender, System::Windows::Forms::KeyEventArgs * e) { if(e->get_KeyCode() == 27 ) { // If the key ESC ... // Traitement } if (e.KeyChar == (char)Keys.Return) { // ENTER Key ... // Traitement } }

      P 1 Reply Last reply
      0
      • B baerten

        Hi You can add for each component of the form a key_Down Event for example : the form is called xxx private: System::Void xxx_KeyDown(System::Object * sender, System::Windows::Forms::KeyEventArgs * e) { if(e->get_KeyCode() == 27 ) { // If the key ESC ... // Traitement } if (e.KeyChar == (char)Keys.Return) { // ENTER Key ... // Traitement } }

        P Offline
        P Offline
        Parshant Verma
        wrote on last edited by
        #3

        thank u sir But i do not want write seperate keypress event for each control of form . i want only a single for all control

        E S 2 Replies Last reply
        0
        • P Parshant Verma

          thank u sir But i do not want write seperate keypress event for each control of form . i want only a single for all control

          E Offline
          E Offline
          Ed Poore
          wrote on last edited by
          #4

          Create a generic handler like so:

          private void KeyPress(object sender, KeyEventArgs e)
          {
              // Do your stuff here
          }

          Then assign all the event handlers to this method, i.e. in the Properties window select the Events tab, if you select the KeyPress event then a combobox should appear, select the KeyPress method and that event handler will be bound to the above function.  Repeat for all the controls you want bound.


          Just Google it. Failing that try phoning :bob:

          1 Reply Last reply
          0
          • P Parshant Verma

            thank u sir But i do not want write seperate keypress event for each control of form . i want only a single for all control

            S Offline
            S Offline
            Stefan Troschuetz
            wrote on last edited by
            #5

            Set KeyPreview property of your form true and register to the KeyPress event of your form.


            "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

            www.troschuetz.de

            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