Skip to main content
 首页 » 编程设计

c#之如何确定按下的键是下划线还是减号 C#

2024年02月05日13txw1958

问题是下划线和减号的键值都是 189,键码是 Keys.OemMinus。所以我无法检查按下的键是下划线还是减号。请帮忙。

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 事件:

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