如何在不关注表单的情况下在Windows窗体应用程序中记录密钥?

时间:2017-01-05 10:08:40

标签: c# windows winforms

每当我在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;
        }
    }

1 个答案:

答案 0 :(得分:0)

要在应用程序未清晰对焦时拦截按键,您可以使用SetWindowsHookEx

// Declarations

private const int WH_KEYBOARD_LL = 13;
private const int WM_KEYDOWN = 0x0100;
private static LowLevelKeyboardProc _proc = HookCallback;
private static IntPtr _hookID = IntPtr.Zero;

private delegate IntPtr LowLevelKeyboardProc(
    int nCode, IntPtr wParam, IntPtr lParam); // MISSING

[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr GetModuleHandle(string lpModuleName);

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr SetWindowsHookEx(int idHook,
    LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool UnhookWindowsHookEx(IntPtr hhk);

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode,
    IntPtr wParam, IntPtr lParam);

private static IntPtr SetHook(LowLevelKeyboardProc proc)
{
    using (Process curProcess = Process.GetCurrentProcess())
    using (ProcessModule curModule = curProcess.MainModule)
    {
        return SetWindowsHookEx(WH_KEYBOARD_LL, proc,
            GetModuleHandle(curModule.ModuleName), 0);
    }
}

然后在你的程序中调用SetHook

_hookID = SetHook(_proc);
Application.Run();
UnhookWindowsHookEx(_hookID);

您还需要声明一个HookCallBack,这是您可以处理按键的地方:

private static IntPtr HookCallback(
    int nCode, IntPtr wParam, IntPtr lParam)
{
    if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
    {
        int vkCode = Marshal.ReadInt32(lParam);
        var keyName = Enum.GetName(typeof(Keys), vkCode);

        // Handle the key press here
        Console.WriteLine((Keys)vkCode);
    }

    return CallNextHookEx(_hookID, nCode, wParam, lParam);
}

上面的代码只会将按键写入控制台,无论您是否将应用程序置于焦点。你应该能够从那里计算出其余的逻辑。

查看PInvoke以获取更多详细信息,并MSDN查看我在实施此功能时发现有用的示例。