3

I'm programming kind of security application

it records the 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 form load

 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 wont show !!

What should i do ??

DGibbs
  • 14,316
  • 7
  • 44
  • 83
majdsalloum
  • 516
  • 1
  • 4
  • 17

4 Answers4

5

As others have stated, your app won't have input focus and won't be listening to key presses.

You need to hook into RegisterHotKey in user32.dll, e.g:

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

Usage:

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'll want to make sure you unregister the key when the app closes too:

private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
    if (!globalHotKey.Unregister())
    {
        Application.Exit();
    }
}
DGibbs
  • 14,316
  • 7
  • 44
  • 83
2

This is because your application does not have input focus and therefore will not be picking up the key presses. You need to hook into lower-level OS to get keyboard input when your application does not have focus.

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

Community
  • 1
  • 1
Belogix
  • 8,129
  • 1
  • 27
  • 32
1

Read documentation? Aan application only gets the key presses for its windows. Logically this means a hidden window can not get key presses.

You hook into the forms handler so you only see the presses on your forms, which are invisible so never can have the focus to get key presses.

There are HOOKS you can use in Windows to hook into the general processing, but beare side effects (i.e. other programs also reacting or you blocking keys).

TomTom
  • 61,059
  • 10
  • 88
  • 148
1

I suggest looking into this:

Processing global mouse and keyboard hooks from C#

In essense, what you want lies outside of .net capabilities, and must be implemented via Windows API, and, consequently, using a native language. However when you recieve input through winAPI, you can pass it to your application, using project i linked to as a guide.

Srv19
  • 3,458
  • 6
  • 44
  • 75