Results 1 to 9 of 9

Thread: KeyDown event

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2001
    Location
    BX, NY
    Posts
    42

    KeyDown event

    I have a text box. If the text box has text entered previously then I want to discourage the use of the BACKSPACE and ARROW keys thereby preventing the previously entered text from being deleted. New text is OK. It seems to work fine with the arrow keys but not for the BACKSPACE key. The following is my code.
    Help.

    Private Sub txtComment_KeyDown(KeyCode As Integer, Shift As Integer)
    dim strLen as String
    dim blnInsert as Boolean
    strLen = Len(txtComment.text)

    Select Case KeyCode
    Case 8
    If strLen > 0 Then
    'SendKeys "(^Z)"
    KeyCode =0
    End If
    Case 37, 38, 39, 40
    If strLen > 0 Then
    KeyCode = 0
    End If
    Case Else
    If Not blnInsert Then
    If txtComment.Text = "" Then
    'txtComment.Text = CStr(Now()) & " Modified By "
    Else
    txtComment.Text = txtComment.Text & vbCrLf & CStr(Now()) & " Modified By "
    End If
    txtComment.SelStart = Len(txtComment.Text)
    blnInsert = True
    End If
    End Select

    End Sub

  2. #2
    Fanatic Member
    Join Date
    Oct 1999
    Location
    England
    Posts
    982
    I'd suggest using the KeyPress event.

    check the ascii of the key pressed if it is considered invalid cancel it. The code below is not tested but I have used this type of thing many a time.
    Code:
    Private Sub txtComment_KeyPress(Keyascii As Integer) 
        'I think the ascii for backspace is 8 to check uncomment the
        'next line and press the backspace key while the text box has focus
        'msgbox keyascii
        if keyascii = 8 then
            '0 will cancel the keypress
            keyascii = 0 
        endif
    end sub
    best of luck

  3. #3
    Lively Member Deucy's Avatar
    Join Date
    Mar 2003
    Location
    Cali
    Posts
    85
    keep in mind that KeyPress() will fire only after the user releases the key pressed, not while he's holding it.. KeyDown() will fire just when the user put his finger on the key..

  4. #4
    Fanatic Member
    Join Date
    Oct 1999
    Location
    England
    Posts
    982
    Is the keycode for backspace the same as the ascii for backspace.

    Thinking about it I think the keydown event does't respond to the same keys as keypress.
    to test it out put the following in the keydown event
    Code:
    msgbox keycode

  5. #5
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    ...If the text box has text entered previously then I want to discourage the use of the BACKSPACE and ARROW keys thereby preventing the previously entered text from being deleted. New text is OK....

    NB: Emphases mine.
    Therefore, rather than handling BackSpace, Arrow Keys - Delete, CTRL-X, Right Click Menu - Cut, and all other such combinations, U could code the KeyDown and KeyUp events to handle for any changes and correct/reset the data in the textbox to what is required.
    VB Code:
    1. 'Variable to hold the existing text
    2. Dim strOldText As String
    3.  
    4. Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
    5. 'Assign the text in the text box to our variable
    6. 'before it gets changed
    7.     strOldText = Text1.Text
    8. End Sub
    9.  
    10. Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
    11. 'Check if the old text remains undisturbed
    12.     If InStr(1, Text1.Text, strOldText, vbBinaryCompare) <> 1 Then
    13.         MsgBox "You have changed the existing text. " & _
    14.         "Only additions are permitted", vbInformation, "Go Back"
    15.         'If the old text is disturbed, revert back to the old text
    16.         Text1.Text = strOldText
    17.     End If
    18. End Sub
    19.  
    20. Private Sub Command1_Click()
    21. 'Do something with the new text
    22. MsgBox StrReverse(Text1.Text)
    23. End Sub

    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

  6. #6

    Thread Starter
    Member
    Join Date
    Aug 2001
    Location
    BX, NY
    Posts
    42
    Thanks KayJay. That worked out really well.

  7. #7
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    Ur Welcome Cheers and Good Luck

    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

  8. #8
    Fanatic Member
    Join Date
    Oct 1999
    Location
    England
    Posts
    982
    Deucy,

    The message box appears when I press a key before I release it.

    Code:
    Private Sub Text1_KeyPress(KeyAscii As Integer)
        MsgBox KeyAscii
    End Sub

  9. #9
    Fanatic Member laserman's Avatar
    Join Date
    Jan 2002
    Location
    Wales U.K
    Posts
    775
    I thought this was dusted.

    Kayjay had it in the keyup which is what you want.
    you said to kayjay it was ok.

    Keypress will show the messagebox as soon as the finger is pressed.

    Are you ok now

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