Results 1 to 9 of 9

Thread: Using KeyPress to compare KeyAscii Values

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    May 2003
    Location
    So Cal
    Posts
    1,564

    Using KeyPress to compare KeyAscii Values

    Need assistance in using the KeyPress Sub to translate a Carriage Return and a Space into a different character.

    What I am trying to accomplish is when a person hits Carriage Return in a text field, it converts it into \n and when they hit space it converts it into _ .

    I understand I need to use

    Private Sub Form_KeyPress (KeyAscii as Integer)
    If KeyAscii = Asc (13) (I assume this is right for a CR?)
    Then KeyAscii = Asc (92) & Asc (110) (Hoping this produces a \n)

    But for some reason, it wont recognize Asc (13) as a carriage return. I even tried Asc ("13") but that doesnt seem to work either.

    Any help would be appreciated.

  2. #2
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629
    Asc() returns an integer from the first character in the string.

    VB Code:
    1. Select Case KeyAscii
    2. Case vbKeyReturn
    3.    KeyAscii = vbNewline
    4. Case vbKeySpace
    5.    KeyAscii = Asc("_")
    6. Case Else
    7.  
    8. EndSelect

  3. #3
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758
    The KeyAscii paramter already contains the ansi value for the keystroke, there is no reason for the conversion.

    VB Code:
    1. Private Sub Text1_KeyPress(KeyAscii As Integer)
    2.     If KeyAscii = 13 Then
    3.         Text1.SelText = "\n"
    4.         KeyAscii = 0
    5.     End If
    6. End Sub

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    May 2003
    Location
    So Cal
    Posts
    1,564
    Thanks to both of you, worked like a charm.

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    May 2003
    Location
    So Cal
    Posts
    1,564
    One other question I have, if I want to write a Quote, how would I go about entering that?

    I have a API call to write keys and strings to an .ini file, but one entry needs to have quotes around it, like "entry".

    So my current code is:

    API_WritePrivateProfileString("Engine.R6MissionDescription", "m_AudioBankName", Text12.Text, spath2)

    I need the text in text12.text to have quotes around it when I write it.

  6. #6
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    Somehting like this? (I'm just guessing) :

    VB Code:
    1. API_WritePrivateProfileString("Engine.R6MissionDescription", "m_AudioBankName", """" & Text12.Text & """", spath2)


    Has someone helped you? Then you can Rate their helpful post.

  7. #7
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758
    I assume you do not want to have the quotes appear in the TextBox. To include quotes in a string you need to double them up. Its best to use a constant, makes your code easier to read

    VB Code:
    1. Public Const Double_Quotes As String = """"
    2.  
    3. Dim strData As String
    4.  
    5. strData = Double_Quotes & Text12.Text & Double_Quotes
    6.  
    7. API_WritePrivateProfileString("Engine.R6MissionDescription", "m_AudioBankName", strData,spath2)

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    May 2003
    Location
    So Cal
    Posts
    1,564
    Fantastic, thanks again guys.

  9. #9
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    You're welcome


    Has someone helped you? Then you can Rate their helpful post.

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