VB6 - Capture KeyUp at Form Level - Not FileListBox Control Level 
Author Message
 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.



Sat, 21 Aug 2004 22:29:36 GMT  
 VB6 - Capture KeyUp at Form Level - Not FileListBox Control Level
The FileListBox, like the ListBox invokes its ClickEvent whenever the
ListIndex changes.

The firing-order of events in your scenario is:
Form_KeyDown
Form_KeyPress
File1_KeyPress
File1_Click
Form_KeyUp never fires
(I know this because I set a break-point in each event)

Try this:

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    Debug.Print KeyCode & " KeyDown"
    Form_KeyUp KeyCode, Shift
End Sub

Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
    Debug.Print KeyCode & " KeyUp"
End Sub

You will notice that you get two KeyUp events to fire for each keystroke.
You might need to flag it to avoid twice-running code...
Hope this helps some...



Sat, 21 Aug 2004 23:34:28 GMT  
 VB6 - Capture KeyUp at Form Level - Not FileListBox Control Level
Thanks for the reply but that doesn't even begin to stop the control from
changing the index value to the first item in the list whose filename begins
with the letter I press ("n").  It did lead me to testing each event to see
the order of events that is actually occurring.  Seems like that should be
documented somewhere.  Anyone know where?  I have MSDN but haven't found it
yet and I've been working on this SIMPLE program all day!!!

The instructions say that if I set KeyCode = 0, the control events won't
trigger.  The instructions LIE!!!  The control STILL does some hidden
function to highlight the list entry which matches the letter I type.  It is
working like Windows Explorer file pane, if that helps.  Is there something
peculiar about the filelistbox control that causes this to happen?  Is there
a way to disable this behavior?  Should I hide this control and make my own
listbox control with entries to match the filelistbox entries?  I guess I'll
try that tomorrow.

If anyone has had success at getting a filelistbox control to NOT jump
around with each letter typed, please email me or reply to this message.

Thanks for any help,
Keith

ps:  thanks again, steve, for your quick reply.


Quote:
> The FileListBox, like the ListBox invokes its ClickEvent whenever the
> ListIndex changes.

> The firing-order of events in your scenario is:
> Form_KeyDown
> Form_KeyPress
> File1_KeyPress
> File1_Click
> Form_KeyUp never fires
> (I know this because I set a break-point in each event)

> Try this:

> Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
>     Debug.Print KeyCode & " KeyDown"
>     Form_KeyUp KeyCode, Shift
> End Sub

> Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
>     Debug.Print KeyCode & " KeyUp"
> End Sub

> You will notice that you get two KeyUp events to fire for each keystroke.
> You might need to flag it to avoid twice-running code...
> Hope this helps some...



Sun, 22 Aug 2004 06:05:23 GMT  
 VB6 - Capture KeyUp at Form Level - Not FileListBox Control Level
Quote:
> yet and I've been working on this SIMPLE program all day!!!

These "simple" programs get "not so simple" when trying to change the
default behaviour of a control. The author of that control most likely had
the exact opposite problems when building it in the first place <g>

Forget about the form's Key events and use the File boxes events. The code
below cancels all keys after trapping and processing "N"
'============
Option Explicit

Private Sub File1_KeyDown(KeyCode As Integer, Shift As Integer)
   Dim iNewIndex As Integer
   If KeyCode = vbKeyN Then
      With File1
         iNewIndex = .ListIndex + 1
         If iNewIndex < .ListCount Then
            .ListIndex = iNewIndex
         Else 'Start over
            .ListIndex = 0
         End If
      End With
   End If
   KeyCode = 0
End Sub

Private Sub File1_KeyPress(KeyAscii As Integer)
   KeyAscii = 0
End Sub

Private Sub File1_KeyUp(KeyCode As Integer, Shift As Integer)
   KeyCode = 0
End Sub
'============

--
Ken Halter
MS-MVP-VB
Please keep it in the groups..


<SNIP>



Sun, 22 Aug 2004 06:21:15 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Problem in Share-Level to User-Level, VB6,Jet4

2. Form level variables not being released when form is unloaded in VB5.0

3. Remote Data Objects in combination with level 1 compliance level

4. Module Level Versus Subroutine Level

5. dim at procedure level or module level

6. How to cancel a large multiple level procedure from several levels in

7. Works at Module level but not at Form

8. Trapping arrow keys at form level in VB6????

9. Form Level Control Scope

10. Capturing Mouse/Keyboard Events at the Application Level

11. Form Level Help Files in Modal forms !

12. FileListBox, etc VB6 controls in a VBA Form?

 

 
Powered by phpBB® Forum Software