English 中文(简体)
重要和关键 避免在文本箱控制中重复写字
原标题:KeyDown and keyUp event to avoid repeat letter in textbox control
  • 时间:2010-06-22 16:13:18
  •  标签:
  • c#

我的问题是:

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

我不想让用户重复钥匙板的任何特性。

任何建议?

感谢

问题回答

仅对利用“关键发展”和“关键”活动镇压关键重复的每一个人附加说明。 如果你这样做,就必须排除诸如Alt/Shift等捕获杂货钥匙,否则这些钥匙不会被送去你想要的实际钥匙,因为这是两个关键的组合,而钥匙Down和“关键”事件会触及所有钥匙。

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;
        }
    }
}

您可使用主要能源企业的SupressKeyPress财产:

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;
}

我不熟悉C#中的具体内容,但我想到的是:

while (keyIsDown){
//do nothing
}

工作。 如果无法追踪一个关键因素,我将感到惊讶。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签