keydown in c# doesn't work for some reason

寵の児 提交于 2019-11-29 15:34:11

Assuming you have a WinForms project, set the KeyPreview property of your form to true, like that (e.g. in the constructor) :

public Form1()
{
    InitializeComponent();
    KeyPreview = true;
}

and it should work like you expected.

Edit: Due to your comment I've added the code to catch all the signs (add that to your Form1_KeyDown method)

if (((e.KeyCode == Keys.D7) && (e.Modifiers == Keys.Shift)) || (e.KeyCode == Keys.Divide))
{
    sign("/");
}
else if (((e.KeyCode == Keys.Oemplus) && (e.Modifiers == Keys.Shift)) || (e.KeyCode == Keys.Multiply))
{
    sign("*");
}
else if ((e.KeyCode == Keys.OemMinus) || (e.KeyCode == Keys.Subtract))
{
    sign("-");
} 
else if ((e.KeyCode == Keys.Oemplus) || (e.KeyCode == Keys.Add))
{
    sign("+");
} 
else if (e.KeyCode == Keys.Enter)
{
    sign("=");
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!