开发者

KeyDown and keyUp event to avoid repeat letter in textbox control

My question is:

How can I disable the hold a key down in a textbox control using C#? e.g Boxxxxxxxxxxxxxxxxxxxxx

I don't want to allow user to repeat any character from the keyboard.

开发者_JAVA技巧

Any suggestion?

thanks


Just an extra note for everyone using KeyDown and KeyUp events to suppress key repeat. If you do this you need to exclude catching meta keys like Alt/Shift etc otherwise when those keys are held down the actual key you want will not be sent since this is a two key combination and KeyDown and KeyUp events catch all keys.

This applies to all two-key combinations. I did not add all of the meta keys to the following example, just the most common ones. You can easily add your own keys by adding them to the collection. Extending on the post from BFree:

public partial class Form1 : Form
{
    private static readonly System.Collections.Generic.ICollection<System.Windows.Forms.Keys) ExcludeKeys = new System.Collections.Generic.HashSet<System.Windows.Forms.Keys)()
    {
        System.Windows.Forms.Keys.None,
        System.Windows.Forms.Keys.Shift,
        System.Windows.Forms.Keys.ShiftKey,
        System.Windows.Forms.Keys.LShiftKey,
        System.Windows.Forms.Keys.RShiftKey,
        System.Windows.Forms.Keys.Alt,
        System.Windows.Forms.Keys.Control,
        System.Windows.Forms.Keys.ControlKey,
        System.Windows.Forms.Keys.LControlKey,
        System.Windows.Forms.Keys.RControlKey,
        System.Windows.Forms.Keys.CapsLock,
        System.Windows.Forms.Keys.NumLock,
        System.Windows.Forms.Keys.LWin,
        System.Windows.Forms.Keys.RWin
    }

    private bool isKeyPressed = false;
    public Form1()
    {
        InitializeComponent();
        this.textBox1.KeyDown += new KeyEventHandler(textBox1_KeyDown);
        this.textBox1.KeyUp += new KeyEventHandler(textBox1_KeyUp);
    }

    void textBox1_KeyUp(object sender, KeyEventArgs e)
    {
        if (!ExcludeKeys.Contains(e.KeyCode))
        {
            isKeyPressed = false;
        }
    }

    void textBox1_KeyDown(object sender, KeyEventArgs e)
    {
        if (!ExcludeKeys.Contains(e.KeyCode))
        {
            e.SuppressKeyPress = isKeyPressed;
            isKeyPressed = true;
        }
    }
}


You can use the SupressKeyPress property on KeyEventArgs:

public partial class Form1 : Form
{
    private bool isKeyPressed = false;
    public Form1()
    {
        InitializeComponent();
        this.textBox1.KeyDown += new KeyEventHandler(textBox1_KeyDown);
        this.textBox1.KeyUp += new KeyEventHandler(textBox1_KeyUp);
    }

    void textBox1_KeyUp(object sender, KeyEventArgs e)
    {
        isKeyPressed = false;
    }

    void textBox1_KeyDown(object sender, KeyEventArgs e)
    {
        e.SuppressKeyPress = isKeyPressed;
        isKeyPressed = true;
    }


}


bool prevent = false;
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    e.SuppressKeyPress = prevent;
}

private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
    prevent = false;
}

private void textBox1_TextChanged(object sender, EventArgs e)
{
    prevent = true;
}


I'm not familiar with the specifics in c# but I imagine something like:

while (keyIsDown){
//do nothing
}

would work. I would be surprised if there is no way to keep track of a key being pressed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜