
VB6 - Capture KeyUp at Form Level - Not FileListBox Control Level
I can't seem to capture the keyup event for "n" in the Form1_KeyUp event. I
have KeyPreview = True in the form Load event and I'm setting KeyCode = 0 in
the keyup event but when I press the "n" key, the File1 filelistbox Click
event fires first. The "n" keypress causes the listindex to find the first
file in the list which starts with "n" - which is november.zip. I want the
"n" keypress to simply add 1 to the listindex - n for next.
What am I missing here? Any ideas?
Email would be better for me but I'll check back here also.
Thanks,
Keith
My code:
' ViewMail
' Written by Keith Cornett
' March 4, 2002
Private Sub Form_Load()
Form1.KeyPreview = True
Me.Dir1 = "\\mail\exchsrvr\imcdata\in"
Me.Caption = "View Mail"
End Sub
Private Sub Form1_KeyUp(KeyCode As Integer, Shift As Integer)
MsgBox KeyCode
If KeyCode = vbKeyN Then
If Me.File1.ListIndex < Me.File1.ListCount - 1 Then
MsgBox Me.File1.ListIndex & ":" & Me.File1.ListIndex + 1
End If
ElseIf KeyCode = vbKeyD Then
MsgBox "d"
End If
KeyCode = 0
End Sub
Private Sub File1_Click()
Dim FoundPosition As Integer
If Me.File1.FileName Like "*.zip" Then
MsgBox "Zip files cannot be viewed."
Exit Sub
End If
MarkLine "To:", vbRed
Me.File1.SetFocus
End Sub
Private Sub Command1_Click()
Unload Me
End Sub
Private Sub Dir1_Change()
Me.File1 = Me.Dir1.Path
End Sub
Sub MarkLine(WithText As String, WithColor As Integer)
With RichTextBox1
.FileName = Me.Dir1.Path & "\" & Me.File1.FileName
.Enabled = False
.Find WithText, .Find(vbNewLine & WithText)
.UpTo vbNewLine, False, False
.Span vbNewLine, True, True
.Enabled = True
.SetFocus
.SelColor = WithColor
.SelFontSize = 16
.SelBold = True
.SelLength = 0
End With
End Sub
From the MSDN library:
To handle keyboard events only at the form level and not allow controls to
receive keyboard events, set KeyAscii to 0 in the form's KeyPress event, and
set KeyCode to 0 in the form's KeyDown event.
Note Some controls intercept keyboard events so that the form can't
receive them. Examples include the ENTER key when focus is on a
CommandButton control and arrow keys when focus is on a ListBox control.