Reputation: 4718
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.
Upvotes: 4
Views: 3921
Reputation: 2586
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.
Upvotes: 2