I am attempting to simulate several keyboard actions in a wpf textbox, including arrow presses (to move caret etc) from another event (e.g. a button click). I have no problem with adding text by raising the
TextCompositionManager.TextInputEvent
event, but attempting to send keys through Keyboard events is not working:
Keyboard.Focus(targetTextBox);
KeyEventArgs ke = new KeyEventArgs(Keyboard.PrimaryDevice, PresentationSource.FromVisual(targetTextBox), 0, Key.UP);
ke.RoutedEvent = Keyboard.KeyDownEvent;
targetTextBox.RaiseEvent(ke);
Is there a way to send control keys through the TextInputEvent, or making the Keyboard event work - i have tried using previewDownEvent and pairing with keyUp events.
EDIT: Also i would prefer to do this through wpf if possible, without using windows forms.
Thanks
I believe this is what you're looking for, though it doesn't use events.
...
SendKeys.Send("{LEFT}");
SendKeys.Send("{RIGHT}");
...
Remember to set focus to the control you want to input in first.
Related
Is there any way I can still catch the keypress if Enter is pressed on a clickable element.
Because the Windows Store Apps API treats Enter as click event rather than a keypress if the focus is on a clickable element (i.e. a GridViewItem).
Unfortunately I have to let the user right-click on a GridViewItem which sets the focus to the element.
Moving the focus would be an option but I'd prefer not to do that if I don't have to.
It seems, this is not in the nature of WInrt. You can follow the specification provided on MSDN:
https://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.uielement.keydown.aspx
You can still try the Key Down event of GridView
In WinRT, some events are user-initiated only. As a result, some WinRT APIs can only be invoked in those types of events. Button.Click is one of those events. As a result, you cannot programmatically raise this event because it would undermine the security feature that limits certain API to those types of events.
Make sense?
Best of luck!
I have a C# WinForm application. On the main form, I have a user control. What I want to be able to do is, whenever a key is pressed on the keyboard, I would like to have my user control receive the keyboard input, so that the keyboard related events (KeyDown, KeyUp and KeyPress) all fire inside the one specific user control.
I would like the actual main form and any other user control on the form to ignore the keypress. Is this possible?
You can do this by ensuring that your control always has focus. An easy way is in the Control.LostFocus event set the focus back. The UserControls base class isn't great about recieving focus, so instead you may want to pick a control within your user control to always have focus and receive events.
On Windows 7 Phone, using Silverlight framework
I'd like to handle when a Button is released.
It's easy to tell when the button is pressed (Click event which is fired either when pressed or when released according to the ClickMode property)
I've played with all the other events provided with the Events editor (KeyUp, LostFocus, MouseLeave etc..)
But I'm yet to find something that is definitive in regards to getting an event when a button is released.
Ultimately, I'm trying to handle doing a click vs a long click when pressing on a button
Thanks
For your situation, KeyUp is only half of the story. You also need to handle KeyDown where you will save the current time which you will then compare to the current time value after KeyUp to determine whether the press was short or long. You also need to make sure that you track one particular key in case your handler(s) is/are intercepting all key strokes.
If for some reason ClickDown/Up don't work out you could try handling the Click event but starting with a ClickMode of press, then changing ClickMode to release on the press handler. This process, though not simple, would give you a chance to implement the down-hold and timer-release sequence that you're looking for.
I have a handler for the TextBox's PreviewLostKeyboardFocus event. It fires when I leave the TextBox using the keyboard (Tab key) or the mouse (by clicking on another TextBox on the form).
The handler takes a KeyboardFocusChangedEventArgs, which has a property named KeyboardDevice, which isn't null in either scenario (I was hoping to find null here when using the mouse).
Question: How can I tell whether a user used the keyboard or the mouse to leave a WPF TextBox?
The e.KeyboardDevice.GetKeyStates(Key.Tab) (where e is of type KeyboardFocusChangedEventArgs) reports:
None (when mouse was used to change focus)
Down, Toggled (when TAB was use to leave the TextBox)
Would that work for you?
I have 6 Buttons, labeled "_0" through "_5". I would like for each button to be pressed when the user presses the corresponding number key. Right now, they must press Alt + the corresponding number key.
I can sort of work around this by giving each button an Accelerator, but it's not quite the same thing. With accelerators, as soon as the key is pressed, the button's Clicked signal is triggered. With mnemonics, the button becomes depressed when the key is pressed, and the Clicked signal isn't triggered until the button is released. I prefer this, because it helps the user to see what is happening.
Is there any way I can get the behavior of Mnemonics, but without requiring the Alt key?
You can have the gtk window catch key events with the event mask settings in the window class. I can't be much more specific than that with callbacks and key types, because I use GTKmm (C++ bindings) but the approach should be similar. Basically when you catch the required key event in your window you can call the button press in code. The window has be to selected (in focus) however.