Código
' Character Is Diacritic? ' ( By Elektro ) ' ' Usage Examples: ' MsgBox(CharacterIsDiacritic("á")) ' Result: True ' ''' <summary> ''' Determines whether a character is diacritic or else contains a diacritical mark. ''' </summary> ''' <param name="Character">Indicates the character.</param> ''' <returns><c>true</c> if character is diacritic or contains a diacritical mark, <c>false</c> otherwise.</returns> Public Function CharacterIsDiacritic(ByVal Character As Char) As Boolean If String.IsNullOrEmpty(CharacterIsDiacritic) Then Return False Else Dim Descomposed As Char() = Character.ToString.Normalize(System.Text.NormalizationForm.FormKD).ToCharArray Return (Descomposed.Count <> 1 OrElse String.IsNullOrWhiteSpace(Descomposed)) End If End Function
Convierte un caracter diacritico
Código
' Convert Diacritic Character ' ( By Elektro ) ' ' Usage Examples: ' MsgBox(ConvertDiacritic("á", UnicodeNormalization:=System.Text.NormalizationForm.FormKD)) ' Result: 'a' ' ''' <summary> ''' Converts the diacritic characters in a String to an equivalent normalized English characters. ''' </summary> ''' <param name="Character"> ''' Indicates the diacritic character. ''' </param> ''' <param name="UnicodeNormalization"> ''' Defines the type of Unicode character normalization to perform. ''' (Default is 'NormalizationForm.FormKD') ''' </param> ''' <returns>The converted character.</returns> Public Function ConvertDiacritic(ByVal Character As Char, Optional ByVal UnicodeNormalization As System.Text.NormalizationForm = System.Text.NormalizationForm.FormKD) As String Dim Chars As Char() = CStr(Character).Normalize(System.Text.NormalizationForm.FormKD).ToCharArray For Each c As Char In Chars Select Case Globalization.CharUnicodeInfo.GetUnicodeCategory(c) Case Globalization.UnicodeCategory.NonSpacingMark, Globalization.UnicodeCategory.SpacingCombiningMark, Globalization.UnicodeCategory.EnclosingMark ' Do nothing. Exit Select Case Else Return c End Select Next c Return Character End Function
Obtiene el keyboardlayout
Código
' Get Keyboard Layout ' ( By Elektro ) ' ' Usage Examples: ' MsgBox(GetKeyboardLayout(IntPtr.Zero)) ' Result: 10 ' MsgBox(GetKeyboardLayout(Process.GetCurrentProcess.MainWindowHandle)) ' Result: 10 ' ''' <summary> ''' Retrieves the active input locale identifier (formerly called the keyboard layout). ''' </summary> ''' <param name="idThread"> ''' A window handle identifier of the thread to query, or 'IntPtr.Zero' to query the current thread. ''' </param> ''' <returns> ''' The return value is the input locale identifier for the thread. ''' </returns> Public Shared Function GetKeyboardLayout(Optional ByVal idThread As IntPtr = Nothing) As Short Return BitConverter.GetBytes(APIGetKeyboardLayout(idThread)).First End Function ''' <summary> ''' Retrieves the active input locale identifier (formerly called the keyboard layout). ''' </summary> ''' <param name="idThread"> ''' A window handle identifier of the thread to query, or 'IntPtr.Zero' to query the current thread. ''' </param> ''' <returns> ''' The return value is the input locale identifier for the thread. ''' ''' The low-order byte contains a Language Identifier for the input language, ''' and the high-order byte contains a device handle to the physical layout of the keyboard. ''' </returns> <System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint:="GetKeyboardLayout", CharSet:=System.Runtime.InteropServices.CharSet.Unicode)> Private Shared Function APIGetKeyboardLayout( Optional ByVal idThread As IntPtr = Nothing ) As UInteger End Function
Obtiene el keycode de un caracter (ojo, no el keycode virtual).
Código
' Get KeyCode ' ( By Elektro ) ' ' Usage Examples: ' MsgBox(GetKeyCode("a")) ' Result: 65 ' MsgBox(GetKeyCode("á")) ' Result: 65 ' MsgBox(GetKeyCode("á", IntPtr.Zero)) ' Result: 65 ' MsgBox(GetKeyCode("a", Process.GetCurrentProcess.MainWindowHandle)) ' Result: 65 ' 'Private Sub Test() Handles MyBase.Shown ' Dim sb As New System.Text.StringBuilder ' Dim Characters As Char() = "ABCDEFGHIJKLMNOPQRSTUVWXYZ ñÑçÇ áéíóú ÁÉÍÓÚ àèìòù ÀÈÌÒÙ äëïÖÜ ÄËÏÖÜ º\'¡`+´-.,ª!·$%&/()=?¿".ToCharArray ' For Each c As Char In Characters ' sb.AppendFormat("Character: {0}", CStr(c)) ' sb.AppendLine() ' sb.AppendFormat("KeyCode : {0}", CStr(GetKeyCode(c, IntPtr.Zero))) ' MessageBox.Show(sb.ToString) ' sb.Clear() ' Next c 'End Sub ''' <summary> ''' Translates a character to the corresponding keycode. ''' </summary> ''' <param name="Character">Indicates the character.</param> ''' <param name="KeyboardLayout">Indicates the keyboard layout.</param> ''' <returns> ''' If the function succeeds, the return value contains the keycode. ''' ''' If the function finds no key that translates to the passed character code, ''' the return value contains "-1". ''' </returns> 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 <> 255S ' Character is found on the current KeyboardLayout. Return Keycode Case Else ' Character is not found on the current KeyboardLayout. ' Descompose the character. Dim Descomposed As Char() = Character.ToString.Normalize(System.Text.NormalizationForm.FormKD).ToCharArray ' If character is diacritic then... If Descomposed.Count <> 1 OrElse String.IsNullOrWhiteSpace(Descomposed) Then For Each c As Char In Descomposed 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 convert the diacritic and try to find the Keycode. Return GetKeyCode(c, KeyboardLayout) End Select Next c End If ' Chars.Count <> 1 End Select ' Keycode Return -1S ' Character is not diacritic and the keycode is not found. End Function ''' <summary> ''' Translates a character to the corresponding virtual-key code and shift state. ''' The function translates the character using the input language and ''' physical keyboard layout identified by the input locale identifier. ''' For more info see here: ''' http://msdn.microsoft.com/en-us/library/ms646332%28v=VS.85%29.aspx ''' </summary> ''' <param name="c">Indicates the character.</param> ''' <param name="KeyboardLayout">Indicates the keyboard layout.</param> ''' <returns> ''' If the function succeeds, ''' the low-order byte of the return value contains the virtual-key code, ''' and the high-order byte contains the shift state. ''' ''' If the function finds no key that translates to the passed character code, ''' both the low-order and high-order bytes contain '255'. ''' </returns> <System.Runtime.InteropServices.DllImport("user32.dll", CharSet:=System.Runtime.InteropServices.CharSet.Unicode)> Private Shared Function VkKeyScanEx( ByVal c As Char, Optional ByVal KeyboardLayout As IntPtr = Nothing ) As Short End Function
Envio de peticion por el método POST
Código
' Send POST ' ( By Elektro ) ' ' Usage Examples: ' 'Dim Response As String = ' SendPOST("http://es.wikipedia.org/wiki/Special:Search?", ' New Dictionary(Of String, String) From { ' {"search", "Petición+POST"}, ' {"sourceid", "Mozilla-search"} ' }) ' Formated POST Data: "search=Petición+POST&sourceid=Mozilla-search" 'Clipboard.SetText(Response) ' Copy the response to Clipboard. ' ''' <summary> ''' Sends a POST method petition and returns the server response. ''' </summary> ''' <param name="URL">Indicates the URL.</param> ''' <param name="PostData">Indicates the post data.</param> ''' <returns>The response.</returns> Public Function SendPOST(ByVal URL As String, Dim Data As New System.Text.StringBuilder ' PostData to send, formated. Dim Request As Net.HttpWebRequest = HttpWebRequest.Create(URL) ' HTTP Request. Dim Response As HttpWebResponse ' Server response. Dim ResponseContent As String ' Server response result. ' Set and format the post data of the query. For Each Item As KeyValuePair(Of String, String) In PostData Data.AppendFormat("{0}={1}&", Item.Key, Item.Value) Next Item ' Set the Request properties. With Request .Method = "POST" .ContentType = "application/x-www-form-urlencoded" .ContentLength = Data.ToString.Length .Proxy = Nothing ' .UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0" End With ' Write the POST data bytes into the Stream. Using RequestStream As IO.Stream = Request.GetRequestStream() RequestStream.Write(System.Text.Encoding.UTF8.GetBytes(Data.ToString), 0, Data.ToString.Length) RequestStream.Close() End Using ' Get the response. Response = Request.GetResponse() ' Get the response content. Using Reader As New IO.StreamReader(Response.GetResponseStream) ResponseContent = Reader.ReadToEnd Response.Close() End Using ' Return the response content. Return ResponseContent End Function