Results 1 to 4 of 4

Thread: enable keycode ascii after pressed once

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2000
    Posts
    2


    well basicly i want to know if it is possible to enable
    lets say for example i pressed the letter "A" is there a way for me to change that so the next time i press it a msgbox appears with a warning
    if u r wondering why i'm asking it's cause i'm making a hangman game and if i press the same letter twice my program doesn't work but it works if i only press it once

    i would appreciate the help
    thanks



  2. #2
    Lively Member
    Join Date
    Jun 1999
    Posts
    120

    one way is to use the KeyPress event:

    Code:
    Private Sub Text1_KeyPress(KeyAscii As Integer)
    Dim i As Integer
        
        If IsNull(Text1.Text) Then Exit Sub
        For i = 0 To (Len(Text1.Text))
            If KeyAscii = Asc(Mid(Text5.Text, i + 1, 1)) Then
                MsgBox "You have entered this letter before!"
                KeyAscii = 0
                Exit Sub
            End If
        Next
    
    End Sub

  3. #3
    Addicted Member
    Join Date
    Jan 2000
    Location
    Sydney, Australia
    Posts
    196

    A bit of a solution

    Hi, this code needs VB6 i think because of the dynamic array in it. Its a bit crude because it doesnt use the first element of the array to store anything, but i couldnt be bothered mucking around with it any more.

    So, give this a go, it runs on a form with one text box on it (called the default, "text1"):

    Code:
    Option Explicit
    'The Array
    Dim Thearray() As Integer
    
    Private Sub Form_Load()
    'Initialise the array
    ReDim Thearray(0)
    End Sub
    
    Private Sub Text1_KeyPress(KeyAscii As Integer)
    
        'Have we pressed it already
        If isInArray(KeyAscii) Then
            MsgBox "You have already pressed that letter"
        End If
    
    End Sub
    
    Private Function isInArray(KeyAscii As Integer) As Boolean
    
        Dim i As Integer
        
        'Start from 1 - the first element (index 0) is not used
        For i = 1 To UBound(Thearray)
            'If the key pressed is already in the array
            If Thearray(i) = KeyAscii Then
                'Return True
                isInArray = True
                Exit Function
            End If
        Next i
        
        'So not in array, put it in the array
        ReDim Preserve Thearray(UBound(Thearray) + 1)
        Thearray(UBound(Thearray)) = KeyAscii
        'Return False
        isInArray = False
    
    End Function
    Hope this helps

  4. #4

    Thread Starter
    New Member
    Join Date
    Mar 2000
    Posts
    2
    I was able to put parts of both your codes together but
    is it possible for me to put that in a command button?

    lets say I press "A" in the text box I want it to check if it was pressed before only when i press on the command button .

    I also want to know i have to different programmes that do the same thing one is with text1_press and the other command_keypress.

    The text1_press check a soon as i press the letter but i want it to check only when i press the command button .

    The command_keypress . the command button has to be selected and instead of the mouse click i have to press the letter i whant and if i write "O" in the text box nothing will happen if i click with the mouse on the command button
    but if i press "A" when the command button is selected then i will check for an "A" in the hidden word



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