有没有办法抑制键盘哔哔声

本文关键字:键盘 有没有 | 更新日期: 2023-09-27 18:35:13

我想在我的应用程序中抑制键盘哔哔声,或者至少在特定事件处理程序中抑制键盘哔哔声。这可能吗?

更新

好的,你要求它(代码示例):

预览键关闭:

    private void textBoxDuckbill_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) {
        switch (e.KeyCode) {
            case Keys.Down:
            case Keys.Up:
                e.IsInputKey = true;
                break;
        }
    }

按下键:

    private void textBoxDuckbill_KeyDown(object sender, KeyEventArgs e) {
        TextBox tb = (TextBox)sender;
        if (e.KeyCode.Equals(Keys.Up)) {
            SetFocusOneRowUp(tb.Name);
            e.Handled = true;
            return;
        }
        if (e.KeyCode.Equals(Keys.Down)) {
            SetFocusOneRowDown(tb.Name);
            e.Handled = true;
            return;
        }
        if (e.KeyCode.Equals(Keys.Left)) {
            SetFocusOneColumnBack(tb.Name);
            e.Handled = true;
            return;
        }
        if (e.KeyCode.Equals(Keys.Right)) {
            SetFocusOneColumnForward(tb.Name);
            e.Handled = true;
            return;
        }
    }

按键:

    private void textBoxDuckbill_KeyPress(object sender, KeyPressEventArgs e) {
        TextBox tb = (TextBox)sender;
        errorProviderCRLogins.SetError(tb, String.Empty);
        // If user presses "%" (37) move back/left one TextBox column; 
        // if user presses "'"(39) move forward/right one TextBox column.
        // Also now allowing navigational arrows to do the same thing (KeyDown event)
        if (e.KeyChar == '%') {
            SetFocusOneColumnBack(tb.Name);
            e.Handled = true;
            return;
        }
        if (e.KeyChar == Convert.ToChar(@"'")) {
            SetFocusOneColumnForward(tb.Name);
            e.Handled = true;
            return;
        }
        // Preclude values (1,2,3) that would normally be allowed (see below) but do 
        // not have a value in the corresponding PlatypusID TextBox
        if (((e.KeyChar == '1') && (String.IsNullOrWhiteSpace(textBoxPlatypusNum1.Text))) ||
            ((e.KeyChar == '2') && (String.IsNullOrWhiteSpace(textBoxPlatypusNum2.Text))) ||
            ((e.KeyChar == '3') && (String.IsNullOrWhiteSpace(textBoxPlatypusNum3.Text)))) {
            e.Handled = true;
            return;
        }
        // Now, having gotten to here, we can assume that 1, 2, and 3 are valid (as are
        // Space and Backspace all the time).
        if ((e.KeyChar != '1') &&
            (e.KeyChar != '2') &&
            (e.KeyChar != '3') &&
            (e.KeyChar != (char)Keys.Space) &&
            (e.KeyChar != (char)Keys.Back)) {
            e.Handled = true;
            return;
        }
        // Added Space as an allowable entry so user can delete a val with that key
        // (which will automatically happen on tabbing into the TextBox, as it is
        // now being highlighted)
        if ((e.KeyChar == (char)Keys.Space) || (e.KeyChar == (char)Keys.Back)) {
            tb.Text = String.Empty;
            buttonSave.Enabled = true;
            // Don't return here, as they might continue to hit Space to zero out 
            // subsequent cells
        }
        // Now, if there is already a value in the cell (this is a repeated val, as shown
        // by TextLength being 1 instead of 0), move it to the next cell and give it the 
        // value just entered (even if space for "delete")
        if ((tb.TextLength == 1) || (e.KeyChar == (char)Keys.Space)) {
            buttonSave.Enabled = true;
            MoveToNextCellAndEnterVal(e.KeyChar.ToString(), tb.Name);
        }
        // Although KeyChar has a val such as 49/("1"), TextLength == 0
        if ((e.KeyChar == '1') ||
        (e.KeyChar == '2') ||
            (e.KeyChar == '3')) {
            buttonSave.Enabled = true;
        }
    }

文本更改:

    private void textBoxDuckbill_TextChanged(object sender, EventArgs e) {
        TextBox tb = (TextBox)sender;
        if (tb.Text == "1") {
            tb.BackColor = PlatypusID1_BACKCOLOR;
            tb.ForeColor = PlatypusID1_FORECOLOR;
            return;
        }
        if (tb.Text == "2") {
            tb.BackColor = PlatypusID2_BACKCOLOR;
            tb.ForeColor = PlatypusID2_FORECOLOR;
            return;
        }
        if (tb.Text == "3") {
            tb.BackColor = PlatypusID3_BACKCOLOR;
            tb.ForeColor = PlatypusID3_FORECOLOR;
            return;
        }
        tb.BackColor = System.Drawing.SystemColors.Window;
        tb.ForeColor = System.Drawing.SystemColors.WindowText;
    }
    private void MoveToNextCellAndEnterVal(string APlatypusID, string ATextBoxName) {
        String numericPortionOfTextBoxName = ATextBoxName.Remove(0, LENGTH_OF_TEXT_BOX_BASE);
        String sTextBoxToFind;
        int textBoxNumber = 0;
        int nextTextBoxNumber;
        int.TryParse(numericPortionOfTextBoxName, out textBoxNumber);
        nextTextBoxNumber = ++textBoxNumber;
        // "wrap around"
        if (nextTextBoxNumber > NUMBER_OF_QUARTER_HOURS) {
            nextTextBoxNumber = nextTextBoxNumber - NUMBER_OF_QUARTER_HOURS;
        }
        sTextBoxToFind = String.Format("textBoxDuckbill{0}", nextTextBoxNumber);
        TextBox tb = (TextBox)this.Controls.Find(sTextBoxToFind, true).First();
        tb.Focus();
        tb.Text = APlatypusID;
    }
    private void SetFocusOneRowDown(string ATextBoxName) {
        String numericPortionOfTextBoxName = ATextBoxName.Remove(0, LENGTH_OF_TEXT_BOX_BASE);
        String sTextBoxToFind;
        int textBoxNumber = 0;
        int nextTextBoxNumber;
        int.TryParse(numericPortionOfTextBoxName, out textBoxNumber);
        if (!(textBoxNumber == NUMBER_OF_QUARTER_HOURS)) {
            nextTextBoxNumber = ++textBoxNumber;
        } else {
            nextTextBoxNumber = 1;
        }
        sTextBoxToFind = String.Format("textBoxDuckbill{0}", nextTextBoxNumber);
        TextBox tb = (TextBox)this.Controls.Find(sTextBoxToFind, true).First();
        tb.Focus();
    }

有没有办法抑制键盘哔哔声

您必须将密钥设置为已处理。

    e.Handled = true

或者在某些情况下:

    e.SuppressKeyPress = true

编辑:没关系,OP已经声明这不是无效的按键哔哔声。

我通过.NET 4.0 Winforms代码在ReSharper中做了一些挖掘,我无法找到产生"哔哔声"的位置。

这让我相信它超出了 .NET 的控制范围,并且没有阻止任何会导致哔哔声的键输入,我认为您无法抑制它。即使这样做也可能不够,因为您编写的任何用于阻止此操作的处理程序实际上最终可能会在导致哔哔声的代码之后运行。

(左 以防有人点击这个寻找那个答案)

此外,这感觉就像你在攻击错误的问题。我最好的猜测是你有一些操作导致你的应用程序发出很多哔哔声。与其抑制蜂鸣声,不如确定导致蜂鸣音的原因并从根源上消除问题?

编辑:

好的,我已经查看了背景颜色更改的代码。实际上,更改此属性不应引起系统蜂鸣音。我唯一能想象的是:

  1. 您在某处有一个处理程序连接到.BackColorChanged,它引起了哔哔声。
  2. 也许只是您的调试环境没有因可能发生的异常而中断。

      if (!value.Equals((object) System.Drawing.Color.Empty) && !this.GetStyle(ControlStyles.SupportsTransparentBackColor) && (int) value.A < (int) byte.MaxValue)
         throw new ArgumentException(System.Windows.Forms.SR.GetString("TransparentBackColorNotAllowed"));
    

    这是控件上.BackColor资源库中的第一个代码块。基本上,如果您使用的表单样式(主题)不支持透明度,并且您提供的颜色在 alpha 通道中具有 255 以外的任何颜色,它将引发异常。您的错误处理/调试环境的设置方式可能不是抛出该异常,而是被吞噬,并且系统蜂鸣音可能是一个指示器。

这里真的有太多的变量,无法给你一个明确的答案,但我强烈建议你从那里开始。背景色实际上没有理由引起系统蜂鸣声。我几乎可以向你保证,这实际上是另一回事。

这是其他事情的症状,简单地抑制蜂鸣声实际上可以隐藏可能导致其他地方其他错误的潜在问题。

通常,最佳做法是不隐藏错误/异常,除非您对特定内容有明确定义的操作过程。这就是为什么不鼓励盲目尝试/捕获的原因,因为您会认为这是理所当然的,并且当另一个错误落入该陷阱时,您将无法获得查找/修复它所需的调试信息。

一旦我在KeyPress事件底部更改了此代码:

if ((e.KeyChar == '1') ||
    (e.KeyChar == '2') ||
    (e.KeyChar == '3')) {
    buttonSave.Enabled = true;
}

..对此:

if ((e.KeyChar == '1') ||
    (e.KeyChar == '2') ||
    (e.KeyChar == '3')) {
    buttonSave.Enabled = true;
    e.Handled = true;
    tb.Text = e.KeyChar.ToString();
}

..它的工作原理就像众所周知的魅力手镯。IOW,我不得不告诉它不允许输入密钥(尽管它是有效的),然后以编程方式将其放在那里。

我不知道为什么这有效或"必须这样",但它有效,所以我或多或少感到满意。