1

As stated in title, I have a form that doesn't have any control on itself (so I can't focus it!!! damn).

I keep it controlless because I need to show images on background and I need to move it by keeping mouse clicked.

Are there any way to detect the keyup event when this is the foreground window?should I use a global hook (and check which is the foreground image obviusly)?

Any simplier workaround?I tested with an hidden control but it's not working.

The problem of putting a control with opacity = 0 brings the possibility to "miss" the MouseDown and MouseUp events (because they could happen over the control instead of the form, but I can still redirect them)

Any suggestion?

Here is the question where I picked some resources: Fire Form KeyPress event

Community
  • 1
  • 1
Francesco Belladonna
  • 11,361
  • 12
  • 77
  • 147

3 Answers3

3

Can't you just set the Form's KeyPreview to true and use the Form's KeyUp Event? (or am i missing something?)

Bala R
  • 107,317
  • 23
  • 199
  • 210
  • I tested with it because was the fastest solution, however **after** I click one time on the form the events stops being fired, can't understand why – Francesco Belladonna Jun 23 '11 at 14:12
  • I understood the problem: the window must be the foreground one, I just set it when mouse pass over it (which is the behaviour I wanted, so it's not a problem for me). Thanks – Francesco Belladonna Jun 24 '11 at 15:31
3

I would override OnKeyUp as it seems to be exactly what you are asking for. Here is an example of popping up a Message Box when the Escape key is released.

    protected override void OnKeyUp(KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Escape)
        {
            MessageBox.Show("Escape was pressed");
            e.Handled = true;
        }

        base.OnKeyUp(e);
    }
mattygs
  • 112
  • 5
0

It looks that you are seeking for GlobalHook. Please have a look at SetWindowsHookEx Native Api. You can easily write your Pinvoke statements. Here is an example from pinvoke.net

using System.Windows.Forms;
public class MyClass
{
     private HookProc myCallbackDelegate = null;

     public MyClass()
     {
     // initialize our delegate
     this.myCallbackDelegate = new HookProc(this.MyCallbackFunction);

     // setup a keyboard hook
     SetWindowsHookEx(HookType.WH_KEYBOARD, this.myCallbackDelegate, IntPtr.Zero, AppDomain.GetCurrentThreadId());
     }

     [DllImport("user32.dll")]
     protected static extern IntPtr SetWindowsHookEx(HookType code, HookProc func, IntPtr hInstance, int threadID);

     [DllImport("user32.dll")]
     static extern int CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);

     private int MyCallbackFunction(int code, IntPtr wParam, IntPtr lParam)
     {
    if (code < 0) {
        //you need to call CallNextHookEx without further processing
        //and return the value returned by CallNextHookEx
        return CallNextHookEx(IntPtr.Zero, code, wParam, lParam);
    }
     // we can convert the 2nd parameter (the key code) to a System.Windows.Forms.Keys enum constant
     Keys keyPressed = (Keys)wParam.ToInt32();
     Console.WriteLine(keyPressed);
    //return the value returned by CallNextHookEx
    return CallNextHookEx(IntPtr.Zero, code, wParam, lParam);
     }
}
crypted
  • 10,118
  • 3
  • 39
  • 52