gpt4 book ai didi

c# - 如何在不关注窗体的情况下在 Windows 窗体应用程序中记录键?

转载 作者:可可西里 更新时间:2023-11-01 10:42:55 26 4
gpt4 key购买 nike

<分区>

我想制作一个应用程序,每当我在 10 秒的间隔内输入“色情”时,它就会退出 Microsoft Edge 或 Google Chrome。当我让我的应用程序显示表单时,这工作正常,但我希望它能够在我的应用程序未获得焦点时识别键。

这是我所有的代码:

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;
}
}

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com