繁体   English   中英

按下“。”键时如何捕获system.windows.forms.keys

[英]how to capture system.windows.forms.keys when “.” key is pressed

多亏汉斯! 这是下面的把戏

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)

消息框显示此KeyPress事件:。

我只是似乎无法弄清楚什么是正确的代码....我一直在尝试从msdn 密钥枚举中找出来

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

谢谢杰森

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

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

尝试使用e.KeyCode代替e.KeyChar

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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