开发者

how to capture system.windows.forms.keys when "." key is pressed

thanks to Hans! this is the trick below

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

okay here is a little more detail.

this works but the "." is displayed in the text box control(not desired)

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

when I use this event handler I cannot bind to e.keycode as it doesnt exist in the context

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

I am trying to capture when the "." key is pre开发者_C百科ssed as I have created a form that has IP address and I want to auto tab when the "." key is pressed. The first message box displays this message when I press the "." either on the numpad or above the ALT Key, but never enter into the if statement i have tried both

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

and

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

message box shows this KeyPress Event: .

I just can't seem to figure out what the propper code is.... i have been trying to figure it out from 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());
       }      
    }

Thanks Jason


Don't mix up virtual key codes and characters. In the KeyPress event you get the actual character that was translated from the virtual key by the active keyboard layout. Thus:

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


Instead of e.KeyChar try using e.KeyCode

Also

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


Could it be OemPeriod that you're looking for? I beleive the Decimal Key is the . on the numpad.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜