4

I've written a WPF touchscreen application. Within this application I've written a user control which is a touch screen keyboard.

I have all the keys working apart from the backspace. Obviously all the keys are buttons and on the backspace button click I'd like to send the back space key to the focused textbox.

I've tried the following but this just inserts a square character in my textbox:

private void buttonBackSpace_Click(object sender, RoutedEventArgs e)
{
    PresentationSource source = PresentationSource.FromDependencyObject(CurrentControl);
    //CurrentControl is my Textbox
    KeyEventArgs ke = new KeyEventArgs(Keyboard.PrimaryDevice, source, 0, Key.Back)
    {
        RoutedEvent = UIElement.KeyDownEvent
    };
    System.Windows.Input.InputManager.Current.ProcessInput(ke);
}

Any ideas? I'm using Visual Studio 2012 & Windows 8.

Sun
  • 4,458
  • 14
  • 66
  • 108
  • just guessing, what is the purpose of the 3rd timestamp parameter? Does it help if you increment it with every key press. – Jodrell Jun 27 '13 at 14:32
  • 2
    To be honest I don't know what that parameter is. Nothing different happens if I change or increment it. – Sun Jun 27 '13 at 14:35
  • presumably its used to synchronise KeyPress Events in some way. I was just clutching at straws in absence of an answer. – Jodrell Jun 27 '13 at 14:58

1 Answers1

2

I'm not sure if this is a typo or not, but I think your issue is that

KeyEventArgs ke = new KeyEventArgs(Keyboard.PrimaryDevice, source, 0, Key.Back)

Should actually be:

KeyEventArgs ke = new KeyEventArgs(Keyboard.PrimaryDevice, source, 0, Keys.Back)

I think the Keys enumeration is what you're actually looking for here.

EDIT: The above answer does not work. The below answer should, however, solve the problem.

private void buttonBackSpace_Click(object sender, RoutedEventArgs e)
{
    CurrentControl.Focus();
    keybd_event(BACK, 0, KEYEVENTF_KEYDOWN, 0);
    keybd_event(BACK, 0, KEYEVENTF_KEYUP, 0);
}
// Virtual Keypress Function
[DllImport("user32.dll", SetLastError = true)]
static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
public const int KEYEVENTF_KEYDOWN = 0x0001; //Key down flag
public const int KEYEVENTF_KEYUP = 0x0002; //Key up flag
public const int BACK = 0x08; // Backspace keycode.
Sean Cogan
  • 2,516
  • 2
  • 27
  • 42
  • 1
    Hi Sean, good try but that isn't the answer. The KeyEventArgs 4th parameter expects a System.Windows.Input.Key. The Keys enumberation is in the System.Windows.Forms namespace so causes an exception – Sun Jun 28 '13 at 07:38
  • Ah, sorry about that. I was actually installing updates to Visual Studio so I couldn't test my answer when I posted it. In that case, I don't know any more about this answer at the moment, but I'll give it a shot. Either way, hopefully you get your answer! – Sean Cogan Jun 28 '13 at 13:23
  • @user1131657 I copied your function into a new project with a button and a textbox, and I've even changed some of the keys, and nothing shows up in my textbox. I'm using Windows 7, so there's a difference there, but I don't think it should affect anything. Would you be open to using a different method to send virtual key commands? – Sean Cogan Jun 28 '13 at 13:29
  • Yes, I'd be open to using a different method but I would need to send it via the button press – Sun Jun 28 '13 at 13:37
  • @user1131657 Ok, give me a few minutes to write something up for you that should work. I'll edit my answer when it's done. – Sean Cogan Jun 28 '13 at 13:41
  • @user1131657 I've updated my answer with a solution that I just tested and had it work. This is how I always send my virtual keypresses when I need them. It's not quite as elegant as your original solution, but it works. – Sean Cogan Jun 28 '13 at 13:48
  • The second solution works for me so I voted up. Not sure why you had a negative vote to begin with... –  Nov 14 '16 at 01:28