Results 1 to 4 of 4

Thread: Keyboard Input

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2007
    Posts
    85

    Keyboard Input

    Hi, I've got a way of using keyboard input to what I want, But, it's very long. I'll post a snippet of what I've got, and I'd like to if anyone has a better way of doing it.

    Code:
    public sub frm_KeyPress(KeyAscii as integer)
    
        If KeyAscii = 97 Then
            frm1.msg_input.Caption = frm1.msg_input.Caption + "a"
        ElseIf KeyAscii = 98 Then
            frm1.msg_input.Caption = frm1.msg_input.Caption + "b"
        ElseIf KeyAscii = 99 Then
            frm1.msg_input.Caption = frm1.msg_input.Caption + "c"
        ElseIf KeyAscii = 100 Then
            frm1.msg_input.Caption = frm1.msg_input.Caption + "d"
        ElseIf KeyAscii = 101 Then
            frm1.msg_input.Caption = frm1.msg_input.Caption + "e"
        ElseIf KeyAscii = 102 Then
            frm1.msg_input.Caption = frm1.msg_input.Caption + "f"
        ElseIf KeyAscii = 103 Then
            frm1.msg_input.Caption = frm1.msg_input.Caption + "g"
        ElseIf KeyAscii = 104 Then
            frm1.msg_input.Caption = frm1.msg_input.Caption + "h"
        ElseIf KeyAscii = 105 Then
            frm1.msg_input.Caption = frm1.msg_input.Caption + "i"
        ElseIf KeyAscii = 106 Then
            frm1.msg_input.Caption = frm1.msg_input.Caption + "j"
        End If
    
    end sub
    To me, that seems like a long way of going about it, but whenever I tried:

    Code:
    frm1.msg_input.Caption = frm1.msg_input.Caption + KeyAscii
    I got an error for putting an integer in the string. Any ideas?

    Syrillia

  2. #2
    Frenzied Member Jim Davis's Avatar
    Join Date
    Mar 2001
    Location
    Mars base one Username: Jim Davis Password: yCrm33
    Posts
    1,284

    Re: Keyboard Input

    It would be:

    frm1.msg_input.Caption = frm1.msg_input.Caption & Chr$( KeyAscii )

    You can use the Chr$(?) to retrieve the ascii character that the KeyAscii value is represents for you.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Dec 2007
    Posts
    85

    Re: Keyboard Input

    Works perfect. Ahhh...the change from '+' to '&', I never even suspects that would cause an error. They read the same to me. Just one to clarify then, is '&' the same as 'And'?

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Keyboard Input

    No... & is for string concatenation (ie: "join these strings together"), and + is to add numbers together.

    If you use + on two strings they will be concatenated - but if you use it on a string and a number, VB will try to convert the string to a number (so it can add them), and if the conversion isn't possible you will get a "Data Type Mismatch" error.

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