I always get a '255' value as keycode of these characters. How to retrieve the right keycode?

StackOverflow https://stackoverflow.com/questions/22290526

سؤال

I don't understand the next issue.

I'm using the function VkKeyScanEx to retrieve the virtual-keycode of a character, then I get the low-order byte of the value that returns the function to retrieve the keycode.

All works as expected but with these characters I always get the same keycode 255

áéíóú ÁÉÍÓÚ àèìòù ÀÈÌÒÙ äëïÖÜ ÄËÏÖÜ

Both the low-order byte and the high-order byte that returns me the VkKeyScanEx function are 255 for those chars.

I don't know what to do with that 255, 'cause I would like to print the character like this:

MsgBox(Convert.ToChar(255))

I don't know if this is an error or is the proper value, anyways I've tried to specify my keyboard layout (which is 10) using the GetKeyboardLayout function but I still get the same 255 value.

Someone could explain me if the value is right and what means that 255 keycode?, if not, How to transform those chars in the right keycode?


This is the code that I'm testing:

Imports System.Runtime.InteropServices

Public Class Form1

    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, GetKeyboardLayout(IntPtr.Zero))))
            MessageBox.Show(sb.ToString)
            sb.Clear()
        Next c

    End Sub

    <DllImport("user32.dll", CharSet:=CharSet.Unicode)>
    Private Shared Function VkKeyScanEx(
                            ByVal c As Char,
                            Optional ByVal KeyboardLayout As IntPtr = Nothing
    ) As Short
    End Function

    <DllImport("user32.dll", EntryPoint:="GetKeyboardLayout", CharSet:=CharSet.Unicode)>
    Private Shared Function APIGetKeyboardLayout(
                            Optional ByVal idThread As IntPtr = Nothing
    ) As UInteger
    End Function

    ''' <summary>
    ''' Translates a character to the corresponding keycode. 
    ''' </summary>
    Public Shared Function GetKeyCode(ByVal c As Char,
                                      Optional ByVal KeyboardLayout As IntPtr = Nothing) As Short
        Return BitConverter.GetBytes(VkKeyScanEx(c)).First
    End Function

    ''' <summary>
    ''' Retrieves the active input locale identifier (formerly called the keyboard layout).
    ''' </summary>
    Public Shared Function GetKeyboardLayout(Optional ByVal idThread As IntPtr = Nothing) As Short
        Return BitConverter.GetBytes(APIGetKeyboardLayout(idThread)).First
    End Function

End Class
هل كانت مفيدة؟

المحلول

MSDN says:

If the function finds no key that translates to the passed character code, both the low-order and high-order bytes contain –1.

Which is what you're getting, interpreted as an unsigned byte. I guess your keyboard (or the layout you're specifying) does not have keys for those characters.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top