2

I've created a custom form for a numeric keyboard which implements the following code:

private void btn1_Click(object sender, EventArgs e)
    {
        PressKey(Keys.D1);
    }

    [DllImport("user32.dll", SetLastError = true)]
    static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, UIntPtr dwExtraInfo);

    public static void PressKey(Keys key)
    {
        const int KEYEVENTF_EXTENDEDKEY = 0x1;

        keybd_event((byte)key, 0x45, KEYEVENTF_EXTENDEDKEY, (UIntPtr)0);
    }

I've also used the following block

protected override bool ShowWithoutActivation
    {
        get { return true; }
    }

It works as charm for notepad, word or other editors (and also using the basic SendKeys method), but in my app, in the grid cell i intend to use it (or other editable control) it fails to insert a value. Accidentally i've found that it can insert a value only once, when the cell is not on edit mode, but it has focus.

Any example of virtual keyboard implementation from one form to another would be highly appreciated

Sen Alexandru
  • 1,953
  • 3
  • 19
  • 35
  • Start -> Run -> osk.exe. Problem solved, no code necessary. – Cody Gray - on strike Apr 26 '17 at 14:22
  • Thanks Cody, but this is not a solution. the app will run on a POS device with Windows 7 embedded for which the keyboard does not have a numeric variant as the numpad, requested by the customer – Sen Alexandru Apr 27 '17 at 08:15
  • My guess would be that you are sending the first keys to the cell that has the focus, but after that you somehow took the focus. Maybe you should check where the focus goes, and who receives the next inputs... – Martin Verjans May 04 '17 at 14:42

1 Answers1

0

(Too long for a comment, but maybe not enough for an answer)

We had once to develop such a keyboard, and we used the SendKeys class from the .Net Framework (which is merely a wrapper for the SendInput Win32 API call).

The keyboard form was overriding the Property CreateParams so we could explicitly tell it not to take the focus (or the keys are send to the keyboard form...)

protected override CreateParams CreateParams
{
    get
    {
        var CreateParams cp = base.CreateParams;
        cp.ExStyle = cp.ExStyle | WS_EX_NOACTIVATE;
        return cp;
    }
}

Note: We noticed that some programs like Ultra VNC (used for remote control) prevent the correct usage of the keyboard. I guess UltraVNC is rerouting the input keys through its driver and something is lost during the way, but never found out what.

Martin Verjans
  • 4,675
  • 1
  • 21
  • 48
  • Thanks @Martin. I also use something similar `protected override CreateParams CreateParams { get { CreateParams baseParams = base.CreateParams; const int WS_EX_NOACTIVATE = 0x08000000; const int WS_EX_TOOLWINDOW = 0x00000080; baseParams.ExStyle = (int)(WS_EX_NOACTIVATE | WS_EX_TOOLWINDOW); return baseParams; } }` but it doesnt seem to work. – Sen Alexandru May 04 '17 at 11:27