全球注册键盘按键

时间:2014-11-18 11:26:39

标签: c# .net winforms

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

它记录键盘键..

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

我尝试了以下

隐藏按钮:

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

和关键事件

private void KeyEvent(object sender, KeyEventArgs e)
    {

      if (e.KeyCode == Keys.Control && e.Modifiers== Keys.F12)  {
            this.Visible = true;
        }
    }

当然还有表单加载

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

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

我该怎么办?

4 个答案:

答案 0 :(得分:6)

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

您需要在user32.dll中加入RegisterHotKey,例如:

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

示例:

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

用法:

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

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

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

答案 1 :(得分:2)

这是因为您的应用程序没有输入焦点,因此不会接受按键操作。当您的应用程序没有焦点时,您需要挂钩到较低级别的操作系统以获取键盘输入。

在此发布并回答了类似的问题:Global keyboard capture in C# application

答案 2 :(得分:1)

阅读文档? Aan应用程序只能获取其窗口的按键。从逻辑上讲,这意味着隐藏的窗口无法获得按键。

您挂钩到表单处理程序,因此您只能看到表单上的按键,这些按键是不可见的,因此永远无法将焦点放在按键上。

您可以在Windows中使用HOOKS挂钩进行常规处理,但可以产生副作用(即其他程序也会做出反应或阻止按键)。

答案 3 :(得分:1)

我建议调查一下:

Processing global mouse and keyboard hooks from C#

在本质上,您想要的是.net功能之外,并且必须通过Windows API实现,因此,使用本机语言。但是,当您通过winAPI接收输入时,可以使用链接的项目作为指南将其传递给您的应用程序。

相关问题