I want to make an application that will exit out of Microsoft Edge or Google Chrome whenever I type in "porn" in the interval of 10 seconds. This works fine when I have my application show the form, but I want it to be able to recognize the keys while my application is not focused.
Here is all of my code:
public partial class Form1 : Form
{
private NotifyIcon icon;
private int counter;
private Timer timer;
public Form1()
{
InitializeComponent();
this.Visible = false;
this.WindowState = FormWindowState.Minimized;
this.ShowInTaskbar = false;
counter = 0;
string path = System.IO.Path.GetDirectoryName(Application.ExecutablePath);
Icon pornIcon = new Icon(path + "/pornquitter.ico");
icon = new NotifyIcon();
icon.BalloonTipTitle = "Porn Quitter v0.1";
icon.Icon = pornIcon;
icon.Visible = true;
MenuItem item1 = new MenuItem();
item1.Text = "Porn Quitter v0.1";
MenuItem item2 = new MenuItem();
item2.Text = "Quit !";
ContextMenu menu = new ContextMenu();
menu.MenuItems.Add(item1);
menu.MenuItems.Add(item2);
icon.ContextMenu = menu;
item2.Click += Item2_Click;
timer = new Timer();
timer.Interval = 10000;
timer.Start();
timer.Tick += Timer_Tick;
}
private void Timer_Tick(object sender, EventArgs e)
{
counter = 0;
}
private void Item2_Click(object sender, EventArgs e)
{
icon.Dispose();
timer.Stop();
this.Close();
}
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
if(counter < 4)
{
if (e.KeyChar.ToString().ToLower().ToCharArray(0,1)[0] == 'p' && counter == 0) counter++;
else if (e.KeyChar.ToString().ToLower().ToCharArray(0, 1)[0] == 'o' && counter == 1) counter++;
else if (e.KeyChar.ToString().ToLower().ToCharArray(0, 1)[0] == 'r' && counter == 2) counter++;
else if (e.KeyChar.ToString().ToLower().ToCharArray(0, 1)[0] == 'n' && counter == 3) counter++;
else counter = 0;
}
if(counter == 4)
{
Process[] chromeInstances = Process.GetProcessesByName("chrome");
Process[] edgeInstances = Process.GetProcessesByName("MicrosoftEdge");
if (chromeInstances.Length > 0)
{
//then chrome is up
foreach (var p in chromeInstances)
{
p.Kill();
}
}
if(edgeInstances.Length > 0)
{
foreach(var p in edgeInstances)
{
p.Kill();
}
}
counter = 0;
}
}