The goal is simple: When the user enters chars in a textbox, I want to detect if this char is the question mark key (?).
I don't care what to use (text changed, key down etc...)
One thing to mention: I am working on a german keyboard layout and therefore I need a solution independent from the keyboard (for example: e.Key = Keys.OemQuestion isn't working, it fires when I press the plus (+) key)
Edit: I tried Convert.toString((char)e.Key) which returned \u0095 and e.Key.ToString() which returned OemOpenBrackets
I chose the solution from #HansPassant and managed to do it with the TextInput event.
First in the constructor:
InitializeComponent();
CommandTextBox.AddHandler(TextBox.TextInputEvent, new TextCompositionEventHandler(CommandTextBox_TextInput), true);
You need this code to actually fire the event
in TextInput
if(e.Text == "?")
{
//Do something
}
NOTE:
This does not capture space, control, shift etc.
Related
Somehow the OnValueChanged() method doesn't get called when an user presses the delete key on an empty android mobile Inputfield.
I guess as it is empty the value of inputfield is "" before pressing delete and still "" after. Thus there isn't an OnValueChanged() called.
I tried checking for the delete key in Update() but Android doesn't seem to notify an application when the delete key gets pressed.
How can I check if the delete key is pressed?
What I want to achieve is that the Inputfield gets deactivated when the user presses the delete key and the inputfield is empty...
Using c# and Unity
Unfortunately, I am not familiar with C languages, I work with Java and Kotlin. In Java development, the implementation of the solution would look like this:
editText.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
//You can identify which key pressed buy checking keyCode value with KeyEvent.KEYCODE_
if(keyCode == KeyEvent.KEYCODE_DEL) {
//this is for backspace
}
return false;
}
});
Answer taken from this page: Android EditText delete(backspace) key event
I hope this helps you
As getting an actual KeyEvent from Android for the delete key seems to be quite complicated, I managed to do an easy workaround.
Simply set the text of the Inputfield on activation to " " (Simply space).
That way when the user presses the delete key, the space gets deleted and OnValueChanged gets called.
To avoid problems with the additional space in the input, you can simply implement a method that erases the space when more input is typed.
I have two text-boxes, one for English and one for Russian. For ease of use purposes, I'd like to know if there's something available in c# that tells my system to switch to one of the installed keyboard layouts.
I would then plan to set a method that does this as soon as one of the text-boxes get focused.
So when the Russian box is focused, the windows Russian keyboard layout is used and vice versa.
I was searching online for a bit but I didn't find any of the sort. Since I wanted it finished early I did a workaround and just simulated the key-presses necessary to switch keyboard layouts on windows using Input-simulator. Now I am looking for a better solution.
Public Form1()
{
// I use the method when either of the text-boxes are used.
// When I find a better solution, there will obviously be two separate methods
txtRussian.GotFocus += SwitchKeyboard;
txtEnglish.GotFocus += SwitchKeyboard;
}
private void SwitchKeyboard(object sender, EventArgs e)
{
// shift alt for keyboard layout switch
sim.Keyboard.ModifiedKeyStroke(VirtualKeyCode.SHIFT,VirtualKeyCode.LMENU);
// LMENU (Left Alt) tends to still be pressed after you he finished the modified keystroke.
// So that makes any first key the user presses be the "LAlt + {a key}" instead of just a key.
// By normally simulating its press again the issue is gone
sim.Keyboard.KeyPress(VirtualKeyCode.LMENU);
}
Of course, this isn't what I'd truly want, cause whenever you alt tab in and out and refocus on a text-box, it'll just switch to the next keyboard layout installed instead of a specified keyboard layout for which the text-box is meant.
So yeah, is there a way to switch to a specified windows keyboard layout with c#?
There is the "InputLanguage" class in the System.Windows.Forms namespace.
You could use the "InputLanguage.CurrentInputLanguage" property to change the currently used keyboard layout.
There is already another post on stackoverflow about that:
C# - Automatically switch between two different IME in the same keyboard layout
However, you can only change the input language with that, but not the layout inside the language. But I think changing the input language is what you need.
If you also need to change the input layout of the language you could do so with setting the .ImeMode property of the TextBox.
I want to disable entering all Emojis (emotion icons) into my input fields in a WPF application. The way I've implemented it is:
txtUserName.PreviewTextInput += LoginPreviewTextInput;
And the LoginPreviewTextInput looks as following:
private void LoginPreviewTextInput(object sender, TextCompositionEventArgs e)
{
if (!InputValidator.IsValidInput(e.Text))
e.Handled = true;
}
And the IsValidInput of InputValidator looks as following:
public class InputValidator
{
//These characters are allowed in the textbox
private static string pattern = #"^[\w\s,\.\(\)~!#\#\$%\^&\*-=\+\[\]\{\}:;'""<>\?\\|]*$";
public static bool IsValidInput(string previewedInput)
{
var matches = Regex.Matches(previewedInput, pattern);
if (matches.Count == 1)
{
return true;
}
return false;
}
}
The strange part is that it works for all Emoji icons in the virtual keyboard, except the Happy Emoji.
It does not work, because LoginPreviewTextInput is not invoked once I enter this Emoji in the Windows Virtual Keyboard (it works for all the other Emojis).
The Happy Emoji is the one shown in the following picture:
When the happy emoji is entered into the textbox ,the textbox looks as follows:
The happy emoji has been entered into the textbox. You can see that there is even a Watermark that is displayed when text propery of a textbox is empty.
When I look at the text property of the textbox in snoop, It is indeed empty, and the bounded property is the viewmodel is empty (the setter has never been invoked).
Again, happens only for this specific emoji (the happy one). All other Emoji get to the LoginPreviewTextInput method, do not match the regex and are ignored.
After spending some time looking into the problem I have come to a solution to this problem.
Why can you you press the Happy Emoji?
The reason that you're able to enter the happy emoji into your application from the virtual keyboard is because it's one of the default windows symbols / has an actual character attached to it. (something along those lines).
Although other emojis can display as such I believe it the default behaviour of the virtual keyboard to just use those symbols as they're already available and don't need a variety of characters to create.
This also happens with a lot of other emoji's such as stars and hearts.
This is what's shown when you press "Happy face":
This is what's shown when you press "Love eyes face":
As you can see what's actually happening is that all the other emoji's are being flagged for that backslash that is in front of them.
How can you solve your problem?
A solution to your problem could be for the regex to check that the code matches a set criteria. For example, that all characters are [a-Z] or whatever you want it to be. this is most likely the better way to fix your problem as it means you're not creating a massive exclusion list.
I'm creating an application which will capture a string from an USB attached scanner. I don't want a text box on the form I'm capturing the data on so I've added a handler to the KeyDown event of the window. There is a specific sequence of key presses I can look for to start capturing the data, however, what I can capture is a list of Virtualkeys. That would include LeftShift, v for 'V'
I'm looking for a way to take the array of Virtualkeys and convert that to a string.
Or if you can suggest another way to catpure the text, maybe hidden textbox?
UPDATE
I've positioned a textbox off window and was able to maintain keyboard focus on it so I could capture the data from the barcode scanner.
You can get info whether any VirtualKey is pressed using this:
bool isPressed = Window.Current.CoreWindow.GetKeyState(VirtualKey).HasFlag(CoreVirtualKeyStates.Down);
AFAIK this is the best way of getting to know whether more keys are pressed at the same time.
I am getting a VERY annoying behavior trying to use the WPF RichTextBox events for some text manipulation.
The idea is to have some logic that will extract the RichTextBox text once some criteria is met.
The issue is that TextChanged event is almost perfect, I can get all the text I want BUT the Enter key is ignored on TextChanged and the event is never fired.
So PreviewKeyDown would be the correct one, right? Wrong. PreviewKeyDown only returns the text BEFORE the last key was pressed...and just KeyDown will ignore the Enter key too. Also, KeyUp is not an option as well because I want to be able to block certain keys to be typed.
Is there a event where I can get the whole text of a RichTextBox including the Enter?
Edit:
I am building a TokenizingRichTextBox following pretty much this tutorial, with some minor modifications (I made it into a Blend Behavior instead of a new kind of Control).
The problem is when the user types Enter. Only the text on the last paragraph gets added to the new Token. I would like to user the Enter and the Tab (among other characters) to trigger the Token creation. But I can't, as mentioned before because the Enter is ignored.
Also, although this can be fixed with some Key events, I intent to add some kind of AutoComplete to the RichTextBox. With the AutoComplete none of the Key events will be triggered, just the TextChanged so taking this into account makes things even more complicated.
The TextChanged event is fired, and the newline has already been entered into document at that stage. To get the exact text changed, do something like
var doc = richTextBox.Document;
foreach(var c in evt.Changes.Where(x => x.AddedLength > 1))
{
var change = new TextRange(
doc.ContentStart.GetPositionAtOffset(c.Offset),
doc.ContentStart.GetPositionAtOffset(c.Offset + c.AddedLength));
Debug.WriteLine($"Change: <{change.Text}>");
}