【问题标题】:how to capture system.windows.forms.keys when "." key is pressed如何在“。”时捕获 system.windows.forms.keys键被按下
【发布时间】:2011-03-01 21:31:26
【问题描述】:

感谢汉斯!这是下面的技巧

this.KeyPress += new KeyPressEventHandler(TabbedTextBox_KeyPress); 
    void TabbedTextBox_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar == '.')
       {
           e.Handled = true;
           var nextControl = this.Parent.GetNextControl(this, forward: true);
           nextControl.Focus();
       }      
    }

好的,这里有更多细节。

这可行,但“。”显示在文本框控件中(不需要)

    this.KeyDown += new KeyEventHandler(TabbedTextBox_KeyDown);
    }
    void TabbedTextBox_KeyDown(object sender, KeyEventArgs e)
    {
        //MessageBox.Show("Event: " + e.KeyCode.ToString());
        if (e.KeyCode == Keys.Decimal || e.KeyCode == Keys.OemPeriod)
        {               
            var nextControl = this.Parent.GetNextControl(this, forward: true);
            nextControl.Focus();
        }
    }

当我使用此事件处理程序时,我无法绑定到 e.keycode,因为它在上下文中不存在

this.KeyPress += new KeyPressEventHandler(TabbedTextBox_KeyPress);
        void TabbedTextBox_KeyPress(object sender, KeyPressEventArgs e)
    {

        //MessageBox.Show("KeyPress Event: " + e.KeyChar.ToString());
        if (e.KeyChar == (char)Keys.Decimal || e.KeyChar == (char)Keys.OemPeriod)
       {
            MessageBox.Show("KeyPress Captured: " + e.KeyChar.ToString());
       }      
    }

我试图捕捉“。”键被按下,因为我创建了一个具有 IP 地址的表单,并且我想在“。”时自动选项卡。键被按下。当我按下“。”时,第一个消息框会显示此消息。无论是在小键盘上还是在 ALT 键上方,但从不进入 if 语句我都尝试过

if (e.KeyChar == (char)Keys.Decimal)

if (e.KeyChar == (char)Keys.OemPeriod)

消息框显示这个 按键事件:.

我似乎无法弄清楚正确的代码是什么...... 我一直试图从 msdn Keys Enumeration

    void TabbedTextBox_KeyPress(object sender, KeyPressEventArgs e)
    {
       MessageBox.Show("KeyPress Event: " + e.KeyChar.ToString());
        if (e.KeyChar == (char)Keys.Decimal)
       {
            MessageBox.Show("KeyPress Captured: " + e.KeyChar.ToString());
       }      
    }

谢谢 杰森

【问题讨论】:

    标签: c# .net


    【解决方案1】:

    不要混淆虚拟键码和字符。在 KeyPress 事件中,您将获得由活动键盘布局从虚拟键转换而来的实际字符。因此:

            if (e.KeyChar == '.') {
                MessageBox.Show("Period detected");
            }
    

    【讨论】:

    • 完美的工作!但是我怎么没有在文本框中输入句点?
    【解决方案2】:

    尝试使用e.KeyCode,而不是e.KeyChar

    还有

    if ((e.KeyCode == Keys.Decimal) || (e.Keyode == Keys.OemPeriod)) {
       //execute tabbing code
    }
    

    【讨论】:

      【解决方案3】:

      您正在寻找的是 OemPeriod 吗?我相信十进制键是 .在小键盘上。

      【讨论】:

      • Key.Decimal 不是“.”;但正如你所说的小键盘上的“,”键,它是根据文化信息写的。 (它按“。”或“,”与操作系统的语言相关)
      猜你喜欢
      • 1970-01-01
      • 2016-09-18
      • 2012-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多