Is there a generic input event in WPF? - wpf

I'm creating a WPF application and I need to do something when I get some user input (it may be keyboard, mouse, ...) my question is if WPF have a generic event that tells me if the user have entered some input or do I have to handle all the events (MouseMove, KeyDown, TouchDown, ...)

Check InputManager.PreProcessInput/PostProcessInput.

Related

Triggering on-screen touch keyboard on custom control in C

I'm writing a C win32 application that has a custom text box input with onFocus/onBlur callbacks. This control cannot be changed and I cannot use C++.
As you could guess, I'd like the on-screen touch keyboard to appear onFocus and to disappear onBlur. What's the best way of achieving that? 
I was thinking of creating an invisible textbox over it or something along those lines, thinking the OS would trigger the OSK if needed? Should I use UI Automation? I struggle to find C examples of such implementation
Regards

Prevent user from typing certain char in TextBox

I am using WPF, MVVM-Light.
In my UI I have a textbox, and I want to prevent the user from typing certain characters in the textbox.
I know if we use code-behind I could handle the key down keyPress events, can I achieve it through MVVM?
Can we use some behaviors or some interactivity triggers?
Using code-behind is perfectly OK with MVVM providing the code-behind is related to your View only.
So if you have some view-specific logic that says "User can only type numbers in this box", then it's perfectly OK to write a KeyPress event for the TextBox that only allows numeric keys to be processed. You could even throw this into a UserControl so it can be reusable.
However if your allowed character logic is based on application logic, such as "User can only use the characters defined in the app.config file for this string value", then you'd be better off validating that in the ViewModel.
Also note that restriction is different from validation.
If you want to validate a user's entry, then I would do so using IDataErrorInfo from the ViewModel layer, and possibly a binding with a mode of UpdateSourceTrigger=PropertyChanged so the validation is checked after every key press.
If you want to restrict what characters can be typed into a TextBox, then I would probably do that from the View layer in the code behind, as that is a functionality of the View.
Yes, to filter input the MVVM way, I would suggest either using a custom control (such as a masked TextBox control) or a Behavior.
I was recently looking for a good masked TextBox and there is a free one out there from Xceed which you can find here. I can't speak to this one, as I haven't used it, but I've been happy with other Xceed components I've used in the past.
However I didn't want to go third party and include a bunch of controls I didn't need, so I ended up creating a behavior that simply attaches to the TextBox and filters the input based on a FilterType. The behavior is pretty easy to create, and you simply use the PreviewTextInput event to filter out characters that you don't want.
This SO Answer has a number of suggestions and links to how to filter/mask the input and if you're not familiar with creating Attached Behaviors, this example shows how to create an Attached Behavior for a Masked Text Box.

How can I intercept the text to be inserted into a RichTextBox?

There is a TextChanged event for the RichTextBox, what I require is a TextChanging event so I have chance to perform an action before the text is changed. The KeyDown event is not enough as my application uses a speech recognition engine which means it is possible to enter text without using the keyboard.
I was hoping I could intercept something in the WndProc method but nothing stands out.
Any ideas or help would be much appreciated. Thanks.
Try using the TextChanged Event from the RichTextBox class. Based on the description from MSDN
This event is raised if the Text property is changed by either a programmatic modification or user interaction.
It should be able to handle what you are trying to do.
Edit: You could have some sort of intermediary storage of the text so that when the text changes it is stored somewhere else first and after the text changed event is done, you can put the text back into the RichTextBox. But without knowing specifically what you are trying to accomplish, this would be my recommendation.

Submit Logon form from PasswordBox in Silverlight

Hoping this is an easy question that I'm just overlooking something...
I want to submit my logon form when a user hits enter from within a passwordbox. Is there a way to do this without capturing keystrokes & determine when enter is being hit?
Thx
Scott
Silverlight doesn't support AcceptButton property/behavior that WinForms has. You'll have to manage the KeyDown event, looking for an Enter key. There's nothing wrong with that, if you wanted the password you could just look at the text property in the text box. Your program has full access to the password, so shying away from a KeyDown event seems kind of frivolous. Monitoring the KeyDown event is a legitimate way to get things done in your code.

Previous focused control in WinForms

I have a win form with lot of controls:
I want to know when user enters 'txt2' exactly after he entered 'txt1'. In other words when user is in 'txt2' I want to know was previous control 'txt1' or not.
What is the best way to such thing?
You could just keep a module-level variable of the type Control that stores the last control to receive focus. Since you need to keep track of each control which receives focus, each control needs to subscribe to a common event handler for the Enter event. In that event you will assign the control that has been entered to the module-level variable, but before that you can test for the jump between 'txt1' and 'txt2'.

Resources