Creating a virtual numpad: stopping a form from activating / focusing

I want to create a virtual numeric keypad similar to an on-screen keyboard. How do I prevent the form from being formatted because when I click on the button the SendKeys.Send call goes to the correct form? I'm sure I have to use an unmanaged API for this, but other than that, I don't even know where to start. Thanks for any help!

0


a source to share


4 answers


You can control how your form is created by overriding the protected CreateParams property:

protected override CreateParams CreateParams {
  get {
    var cp = base.CreateParams;
    // Configure the CreateParams structure here
    return cp;
  }
}

      

I suggest trying the following style:



int WS_EX_NOACTIVATE = 0x08000000;
cp.ExStyle = cp.ExStyle | WS_EX_NOACTIVATE;

      

The style WS_EX_NOACTIVATE

prevents mouse pointers from being put into the window focus (you can use this to create tooltips, for example).

You can combine this with other styles (like WS_EX_TOPMOST

) to get your behavior after.

+1


a source


A form that has no focus usually does not receive keyboard input. So I'm not sure how you would do this.



0


a source


If numpad is only supposed to work in the applications you write, you can simply keep track of the last valid input control that had focus. This way you don't have to worry about something stealing focus. I recently did something similar by subscribing an empty EnterBoxes input line to the same event handler that set my focused control handle to the sender parameter. This exact method may not work for you, but the approach may make it easier.

0


a source


For applications you write, consider the Observer pattern.

In general, you can add a Subscribe method to your keyboard that accepts a delegate (also allows unsubscribing). To handle the key event, push all signed delegates by sending them a symbol code.

Code (mostly from memory, chunks probably won't compile, but should catch you)

delegate void KeyPadSubscription (char keyPressed);
List<KeyPadSubscription> subscriptions;

void KeyPress (object sender, KeyPressArgs e)
{
   foreach (KeyPadSubscription sub in subscriptions)
        sub (e.CharCode)
}

void Subscribe(KeyPadSubscription s)
{
    subscriptions.Add(s);
}

void Unsubscribe(KeyPadSubscription s)
{
    subscriptions.Remove(s);
}

      

In your case, you probably don't need a list of delegates, and a property might suffice. Then, when your forms change focus, the active form will set its method as a delegate for your keyboard.

void MyForm_Activated(...)
{
    //assumes keypad is global or static
    KeyPad.Subsribe (this.HandleKeyPadPress)
}

      

0


a source







All Articles