Domanda

I'm using the next function to convert a diacritic character and then get the Keycode of the character, but before calling this method I need to know first if the character is diacritic, to don't make redundance calls on this method that should difference a diacritic from a non-diacritic char.

So, how to determine whether a character diacritic?

PS: See the marked commens inside the code.

The idea is, for the character O the method should return a 79, and for the character Ó the method will remove the diacritic so I get an O and I call the function again with that O, returning another 79, but if the character can't be found on the keybouardlayout the method will be trying to remove diacritics even when the character is not diacritic, and calling again the same function all the time, so I need to determine whether the character is diacritic.

Public Shared Function GetKeyCode(ByVal Character As Char,
                                  Optional ByVal KeyboardLayout As IntPtr = Nothing) As Short

    ' Get the Keycode of the character.
    Dim Keycode As Short =
        BitConverter.GetBytes(VkKeyScanEx(Character)).First

    Select Case Keycode

        Case Is <> 255 ' Character is found on the current KeyboardLayout.
            Return Keycode

        Case Else ' Character is not found on the current layour (Maybe is a diacritic character?)

            ' ****************************************************************************
            ' I want to perform the instructions below only if the character is diacritic.
            ' ****************************************************************************

            Dim s As String = CStr(Character).Normalize(System.Text.NormalizationForm.FormKD)

            For Each c As Char In s

                Select Case Globalization.CharUnicodeInfo.GetUnicodeCategory(c)

                    Case Globalization.UnicodeCategory.NonSpacingMark,
                         Globalization.UnicodeCategory.SpacingCombiningMark,
                         Globalization.UnicodeCategory.EnclosingMark

                        ' Do nothing.
                        Exit Select

                    Case Else ' Character is diacritic so we remove the diacritic and try to return the Keycode.
                        Return GetKeyCode(c, KeyboardLayout)

                End Select

            Next c

            ' ****************************************************************************
            ' I want to perform the instructions above only if the character is diacritic.
            ' ****************************************************************************

            Return 255 ' Character is not diacritic and the keycode can't be found.

    End Select
È stato utile?

Soluzione

The safe bet to know if a charter is diacritic is to test for it.

One option would be to go through all Unicode once and put the diacritic in a HashSet.

If you are testing a long string then normalize the whole sting once.

If you want a broader mapping consider also encoding to win1252.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top