All-wheel drive keyboards

I am programming a kind of security application

it records keyboard keys.

I want to hide the application and then show it when the user presses a key

I tried the following

Hide button:

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

      

and key event

private void KeyEvent(object sender, KeyEventArgs e)
    {

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

      

and, of course, the formal burden

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

      

But no matter how many times I press the keys .. I will not show !!

What should I do?

+3


source to share


4 answers


This is because your application does not have input focus and therefore will not press keys. You need to connect to the downstream OS to receive keyboard input when your application has no focus.



A similar question was posted and answered here: Global keyboard capture in C # application

+2


source


As others have stated, your application will not have input focus and will not listen for keystrokes.

You need to connect to RegisterHotKey in user32.dll

, for example:

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

      

Example:

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

      



Using:

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

      

You want you to unregister the key when you close the app:

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

      

+6


source


Read the documentation? The Aan app only receives keystrokes for its windows. Logically, this means that a hidden window cannot receive keystrokes.

You hook into a form handler so that you only see the clicks on your forms, which are invisible, so they can never focus on keystrokes.

There are HOOKS you can use on Windows to hook into general processing, but the side effects are beare (i.e. other programs react or block keys too).

+1


source


I suggest looking into this:

Handling global mouse and keyboard hooks from C #

Basically, what you want is beyond the capabilities of .net and must be implemented through the Windows API and therefore using the native language. However, when you get winAPI login, you can pipe it to your application using the i project associated with it as a guide.

+1


source







All Articles