问题
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