全球注册键盘按键

use*_*413 3 .net c# winforms

我正在编写一种安全应用程序

它记录键盘键..

我想隐藏应用程序,然后在用户按下某个键时显示它

我尝试了以下内容

隐藏按钮:

    private void button4_Click(object sender, EventArgs e)
    {
        ShowInTaskbar = false;
        this.Visible = false;
        this.TopMost = true;
    }
Run Code Online (Sandbox Code Playgroud)

和关键事件

private void KeyEvent(object sender, KeyEventArgs e)
    {

      if (e.KeyCode == Keys.Control && e.Modifiers== Keys.F12)  {
            this.Visible = true;
        }
    }
Run Code Online (Sandbox Code Playgroud)

当然还有表格负荷

 private void Form2_Load(object sender, EventArgs e)
    {
        KeyPreview = true;
        this.KeyUp+=new System.Windows.Forms.KeyEventHandler(KeyEvent);
    }
Run Code Online (Sandbox Code Playgroud)

但无论我按键多少次......我都不会表现出来!!

我该怎么办 ??

DGi*_*bbs 6

正如其他人所说,你的应用程序将没有输入焦点,也不会听按键操作.

你需要挂接到RegisterHotKeyuser32.dll,如:

[DllImport("user32.dll")]
private static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vk);
Run Code Online (Sandbox Code Playgroud)

例:

public class GlobalHotKey
{
    private int modifier;
    private int key;
    private IntPtr hWnd;
    private int id;

    public GlobalHotKey(int modifier, Keys key, Form form)
    {
        this.modifier = modifier;
        this.key = (int)key;
        this.hWnd = form.Handle;
        id = this.GetHashCode();
    }

    public bool Register()
    {
        return RegisterHotKey(hWnd, id, modifier, key);
    }

    public bool Unregister()
    {
        return UnregisterHotKey(hWnd, id);
    }

    public override int GetHashCode()
    {
        return modifier ^ key ^ hWnd.ToInt32();
    }

    [DllImport("user32.dll")]
    private static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vk);

    [DllImport("user32.dll")]
    private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
}

public static class Constants
{
    public const int NOMOD = 0x0000;
    public const int ALT = 0x0001;
    public const int CTRL = 0x0002;
    public const int SHIFT = 0x0004;
    public const int WIN = 0x0008;
    public const int WM_HOTKEY_MSG_ID = 0x0312;
}
Run Code Online (Sandbox Code Playgroud)

用法:

private GlobalHotKey globalHotKey;

// Registering your hotkeys
private void Form2_Load(object sender, EventArgs e)
{
    globalHotKey = new HotKeys.GlobalHotKey(Constants.CTRL, Keys.F12, this);
    bool registered = globalHotKey.Register();

    // Handle instances where the hotkey failed to register
    if(!registered)
    {
        MessageBox.Show("Hotkey failed to register");
    }
}

// Listen for messages matching your hotkeys
protected override void WndProc(ref Message m)
{
    if (m.Msg == HotKeys.Constants.WM_HOTKEY_MSG_ID)
    {
        HandleHotkey();
    }

    base.WndProc(ref m);
}

// Do something when the hotkey is pressed
private void HandleHotkey()
{
    if(this.Visible)
        this.Hide();
    else
        this.Show();
}
Run Code Online (Sandbox Code Playgroud)

您还需要确保在应用关闭时取消注册密钥:

private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
    if (!globalHotKey.Unregister())
    {
        Application.Exit();
    }
}
Run Code Online (Sandbox Code Playgroud)