Como posso fechar o programa apertando a tecla ESC? Mesmo que tenha uma MessageBox ou qualquer outra coisa impedindo? Tentei desta forma mas não consegui:
private void Form_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
Process.GetCurrentProcess().Kill();
}
}
Form:
public partial class Home : Form
{
public Home()
{
InitializeComponent();
MaximizeBox = false;
statusID.Visible = false;
this.KeyDown += Home_KeyDown;
}
private void Home_FormClosing(object sender, FormClosingEventArgs e)
{
aplicacaoRodando = false;
}
bool aplicacaoRodando = true;
private void materialFlatButton2_Click(object sender, EventArgs e)
{
txtID.Enabled = false;
aplicacaoRodando = true;
try
{
labelworking.Visible = true;
labelworking.Text = "Em funcionamento..";
btnscriptshutdown.Enabled = true;
btnTurn.Enabled = false;
Thread t = new Thread(() =>
{
while (aplicacaoRodando)
{
Thread.Sleep(1000);
}
});
t.Start();
}
catch (Exception ex)
{
}
}
private void Home_Resize(object sender, EventArgs e)
{
if (FormWindowState.Minimized == this.WindowState)
{
nicon.BalloonTipTitle = "HOME";
nicon.BalloonTipIcon = ToolTipIcon.Info;
nicon.Icon = new Icon(Icon, 40, 40);
nicon.Visible = true;
this.Hide();
}
else if (FormWindowState.Normal == this.WindowState)
{
nicon.Visible = false;
}
}
private void notifyIcon1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Visible = true;
WindowState = FormWindowState.Normal;
}
}
private void cmssair_Click(object sender, EventArgs e)
{
aplicacaoRodando = false;
Close();
}
private void Btnscriptshutdown_Click(object sender, EventArgs e)
{
txtID.Enabled = true;
aplicacaoRodando = false;
btnTurn.Enabled = true;
btnscriptshutdown.Enabled = false;
labelworking.Visible = false;
}
private void Home_KeyDown(object sender, KeyEventArgs e)
{
aplicacaoRodando = false;
if (e.KeyCode == Keys.Escape)
{
System.Windows.Forms.Application.Exit();
}
}
private void materialSingleLineTextField1_TextChanged(object sender, EventArgs e)
{
btnvalidar.Enabled = !(string.IsNullOrWhiteSpace(txtID.Text));
}
private void materialSingleLineTextField1_KeyPress(object sender, KeyPressEventArgs e)
{
if (!Char.IsDigit(e.KeyChar) && e.KeyChar != (char)8)
{
statusID.Visible = true;
statusID.Text = "Somente números";
e.Handled = true;
}
else
{
statusID.Visible = false;
}
}
}
}