Results 1 to 3 of 3

Thread: Key Codes -> ASCII?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 1999
    Location
    Prague, Czech Republic
    Posts
    350

    Post

    Hi!
    Is any way to convert a key code to the ascii numbers? I saw a API function, but I no longer have it.



    ------------------
    Thanks,
    John, 14 years old

  2. #2
    New Member
    Join Date
    Feb 1999
    Posts
    5

    Post

    John,

    Please see the following examples and reference the help files:

    This example converts text entered into a TextBox control to uppercase. To try this example, paste the code into the Declarations section of a form that contains a TextBox, and then press F5 and enter something into the TextBox.

    Private Sub Text1_KeyPress (KeyAscii As Integer)
    Char = Chr(KeyAscii)
    KeyAscii = Asc(UCase(Char))
    End Sub

    and the example:

    debug.print asc("m")
    109
    debug.print chr(109)
    m


    Hope that's what you're looking for,

    Michael

  3. #3
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,176

    Post

    Here's a couple of Functions I've written to Convert between ASCII and KeyCodes, Either way:
    Code:
    Private Declare Function VkKeyScan Lib "user32" Alias "VkKeyScanA" (ByVal cChar As Byte) As Integer
    Private Declare Function ToAscii Lib "user32" (ByVal uVirtKey As Long, ByVal uScanCode As Long, lpbKeyState As Byte, lpwTransKey As Long, ByVal fuState As Long) As Long
    
    Function GetKeyCode(ByVal AscII As Long) As Long
        GetKeyCode = Val("&H" & Right$(Hex$(VkKeyScan(AscII)), 2))
    End Function
    
    Function GetAscII(ByVal KeyCode As Long, Optional ByVal Shift As Boolean = False) As Long
            Dim sKeys(255) As Byte
            Dim lAscII As Long
            
            sKeys(KeyCode) = 1
            sKeys(16) = Abs(Shift) * 128
            Call ToAscii(KeyCode, 0&, sKeys(0), lAscII, 0&)
            GetAscII = lAscII
    End Function
    Usage: ASCII = GetAscII(KeyCode, [Shift])
    Usage: KeyCode = GetKeyCode(ASCII)


    ------------------
    Aaron Young
    Analyst Programmer
    aarony@redwingsoftware.com
    ajyoung@pressenter.com


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width