如何确定按下的键是下划线还是减号? C# [英] How to determine if Pressed key is Underscore Or Minus? C#

查看:103
本文介绍了如何确定按下的键是下划线还是减号? C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题在于,对于下划线和减号,键值均为189,而键码为Keys.OemMinus.因此,我无法检查按下的键是下划线还是减号.请帮忙.

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

推荐答案

如果这是WinForms项目,请使用KeyPress事件而不是KeyDown事件:

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

这篇关于如何确定按下的键是下划线还是减号? C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆