Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Happy Emoji entered into a WPF textbox from a Windows 10 virtual keyboard is NOT captured in the textbox's PreviewTextInput event

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:

enter image description here

When the happy emoji is entered into the textbox ,the textbox looks as follows:

enter image description here

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.

like image 709
DimaK Avatar asked Jun 19 '18 10:06

DimaK


1 Answers

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":

Image showing the smiley symbol

This is what's shown when you press "Love eyes face":

Image showing the character code

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.

like image 63
user8478480 Avatar answered Oct 14 '22 09:10

user8478480