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. Trapping a Keystroke

Trapping a Keystroke

Scheduled Pinned Locked Moved C#
question
7 Posts 5 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.
  • M Offline
    M Offline
    MarkMokris
    wrote on last edited by
    #1

    I have subclassed a Windows Form TextBox control. Inside my subclass I override the OnKeyDown like this: protected override void OnKeyDown(KeyEventArgs e) { // Trap keystrokes here???? } Shouldn't this have the effect of inhibiting the user from entering any keystrokes, since I don't pass the event up to the parent method? When I execute and type into the texbox, I see my keystrokes. I am trying to trap keystrokes this way. What am I doing wrong? Thanks! Mark

    G P D G 4 Replies Last reply
    0
    • M MarkMokris

      I have subclassed a Windows Form TextBox control. Inside my subclass I override the OnKeyDown like this: protected override void OnKeyDown(KeyEventArgs e) { // Trap keystrokes here???? } Shouldn't this have the effect of inhibiting the user from entering any keystrokes, since I don't pass the event up to the parent method? When I execute and type into the texbox, I see my keystrokes. I am trying to trap keystrokes this way. What am I doing wrong? Thanks! Mark

      G Offline
      G Offline
      Gareth H
      wrote on last edited by
      #2

      MarkMokris, You have misunderstood what this event does. The event is fired when someone types something, it does not stop or change the users input, since you do nothing in the event. If you want to stop users entering info into a textbox, set it to ReadOnly or Disabled. Regards, Gareth.

      M 1 Reply Last reply
      0
      • G Gareth H

        MarkMokris, You have misunderstood what this event does. The event is fired when someone types something, it does not stop or change the users input, since you do nothing in the event. If you want to stop users entering info into a textbox, set it to ReadOnly or Disabled. Regards, Gareth.

        M Offline
        M Offline
        MarkMokris
        wrote on last edited by
        #3

        Thanks. But then, is there anyway to trap a keystroke before it appears in the textbox? What if I needed to prohibit certain special characters from being typed? Thanks again! Mark

        G 1 Reply Last reply
        0
        • M MarkMokris

          Thanks. But then, is there anyway to trap a keystroke before it appears in the textbox? What if I needed to prohibit certain special characters from being typed? Thanks again! Mark

          G Offline
          G Offline
          Gareth H
          wrote on last edited by
          #4

          MarkMokris, If you want to stop certain user input, use regular expressions or in the KeyStroke event, do some checks and remove whatever you dont want. But it would be better to use regular exp. Regards, Gareth.

          1 Reply Last reply
          0
          • M MarkMokris

            I have subclassed a Windows Form TextBox control. Inside my subclass I override the OnKeyDown like this: protected override void OnKeyDown(KeyEventArgs e) { // Trap keystrokes here???? } Shouldn't this have the effect of inhibiting the user from entering any keystrokes, since I don't pass the event up to the parent method? When I execute and type into the texbox, I see my keystrokes. I am trying to trap keystrokes this way. What am I doing wrong? Thanks! Mark

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #5

            MarkMokris wrote:

            since I don't pass the event up to the parent method

            That's done automatically (events use multi-cast delegates). In OnKeyPress, in your // Trap keystrokes here???? test for particular characters and set e.Handled to true for the ones you want to ignore.

            1 Reply Last reply
            0
            • M MarkMokris

              I have subclassed a Windows Form TextBox control. Inside my subclass I override the OnKeyDown like this: protected override void OnKeyDown(KeyEventArgs e) { // Trap keystrokes here???? } Shouldn't this have the effect of inhibiting the user from entering any keystrokes, since I don't pass the event up to the parent method? When I execute and type into the texbox, I see my keystrokes. I am trying to trap keystrokes this way. What am I doing wrong? Thanks! Mark

              D Offline
              D Offline
              Dave Kreskowiak
              wrote on last edited by
              #6

              OnKeyDown is a method that raises the KeyDown event for handlers of that event. You're not stopping keystrokes from showing up because you haven't stopped the processing of the key. You've only stopped the raising of an event. If you want to stop the keystroke from occuring, inside your custom textbox control, handle the KeyDown event. Inside there, do your processing to figure out if you want the key to be stopped or not and set the e.SuppressKeyPress property to true if you want it stopped.

              A guide to posting questions on CodeProject[^]
              Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                   2006, 2007

              1 Reply Last reply
              0
              • M MarkMokris

                I have subclassed a Windows Form TextBox control. Inside my subclass I override the OnKeyDown like this: protected override void OnKeyDown(KeyEventArgs e) { // Trap keystrokes here???? } Shouldn't this have the effect of inhibiting the user from entering any keystrokes, since I don't pass the event up to the parent method? When I execute and type into the texbox, I see my keystrokes. I am trying to trap keystrokes this way. What am I doing wrong? Thanks! Mark

                G Offline
                G Offline
                GuyThiebaut
                wrote on last edited by
                #7

                Use the Handled property to trap keystrokes on the KeyUp event. On the KeyPress event you can capture the key that is pressed.

                void KeyRecord_KeyUp(Object sender, KeyEventArgs e)
                    {
                     e.Handled = true;
                    }
                
                void KeyRecord_KeyPress(Object sender, KeyPressEventArgs e)
                    {
                     e.Handled = true;
                     MessageBox.Show(e.KeyChar.ToString());
                    } 
                

                I hope this is of some help. Regards Guy P.S. you will also need to set Handled to true in the KeyDown event.

                You always pass failure on the way to success.
                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