How to determine if Pressed key is Underscore Or Minus? C#

北城余情 提交于 2021-02-10 05:10:46

问题


The problem is that for both underscore and minus the keyvalue is 189, and the keycode is Keys.OemMinus. So I am unable to check whether pressed key is underscore or minus. Please Help.

private void Some_KeyDown(object sender, KeyEventArgs e)
{
       if(Pressed key is minus/dash)
       {
           MessageBox.Show("minus");
       }

       if(pressed key is underscore)
       {
          MessageBox.Show("underscore");
       }
}

回答1:


If this is a WinForms project, use the KeyPress event instead of the KeyDown event:

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if(e.KeyChar == '-')
    {
        MessageBox.Show("Minus");
    }
    if (e.KeyChar == '_')
    {
        MessageBox.Show("Underscore");
    }
}


来源:https://stackoverflow.com/questions/37087435/how-to-determine-if-pressed-key-is-underscore-or-minus-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!