简体   繁体   中英

C#: How do you disable a key from being pressed indefinetly in textbox's keydown event?

C#: How do you disable a key from being pressed indefinetly in textbox's keydown event?

The standard way to handle that is to create an event handler for the Textbox.KeyDown event and then set KeyEventArgs.SuppressKeyPress to true if the key pressed matches the key you want to disable. Here is an example:

yourTextBox.KeyDown += delegate(Object sender, KeyEventArgs e)
{
    e.SuppressKeyPress = (e.KeyCode == YOUR_KEY);
}

Using e.SuppressKeyPress will prevent the key stroke from registering at all.

Assuming you'd want the first keystroke to register, but not continually register that key as a keystroke when the user holds it down, wrap the e.SuppressKeyPress in a class level variable that registers when a key is being held down.

public class nonRepeatingTextBox : TextBox
{
    private bool keyDown = false;

    protected override void OnKeyUp(KeyEventArgs e)
    {
        keyDown = false;
    }

    protected override void OnKeyDown(KeyEventArgs e)
    {
        if (keyDown)
        {
            e.SuppressKeyPress = true;
        }
        keyDown = true;
    }
}

Use this class as your text box. You'd probably want to make exceptions in the OnKeyDown override, for arrow keys etc.

I realize I'm late to the game, but I needed this very functionality in vb and thought I'd share my experience.

The accepted answer seems rather vague and while it did express a portion of the answer, I don't feel it fully flushed it out. Stuart Helwig had some interesting code, but unfortunately it had some pretty big drawbacks: 1) if you type so fast that you hadn't yet released the previous key before striking the next key, it was excluded and 2) KeyDown applies to the shift, alt, and control keys as well so no Capital letters without CAPSLOCK. Therefore, this is my attempt to flush out the answer. I hope someone finds it useful as this was much needed functionality for my RegexTextBox that has a restrict feature that could be bypassed by holding down a key.

Like Stuart Helwig, I inherited the TextBox Control and added the following Subs.

I am using KeyDown , KeyPress , and KeyUp events to solve the problem. The KeyDown event records the KeyCode to the KeyPressed field for later comparison, the KeyPress evaluates the Keychar (but not shift, alt, control) and uses KeyPressEventArgs.Handled to drop the char entry, and the KeyUp resets the keyPressed and KeyIsDown fields.

Private Sub PreventHoldKeyUp(sender As Object, e As KeyEventArgs) _
        Handles Me.KeyUp
        Me.KeyIsDown = False
        Me.KeyPressed = Keys.None
End Sub

Private Sub PreventHoldKeyDown(sender As Object, e As KeyEventArgs) _
         Handles Me.KeyDown
        /* Recording the current key that was hit for later comparison */
        Me.KeyPressed = e.KeyCode
End Sub

Private Sub PreventKeyPressHold(sender As Object, e As KeyPressEventArgs) _
        Handles Me.KeyPress
        /* If a key has been struck and is still down */
        If Me.KeyIsDown Then

            /* The following allows backspace which is picked up by
             keypress but does not have a keychar */
            Select Case Convert.ToInt32(e.KeyChar)
                Case Keys.Back
                    Exit Sub
            End Select
            /* If this is the same key picked up by keydown abort */
            /* this portion allows fast typing without char drop
             but also aborts multiple characters with one key press */
            If Me.KeyPressed = Convert.ToInt32(e.KeyChar) Then
                e.Handled = True
            End If
        Else
            e.Handled = False
        End If

        Me.KeyIsDown = True

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM