using KeyCode on KeyDown event

jguscs

Registered User.
Local time
Today, 02:33
Joined
Jun 23, 2003
Messages
148
How can I use the KeyCode of the KeyDown event from within the event?

Private Sub ListBox_KeyDown(KeyCode As Integer, Shift As Integer)
If ____.KeyCode = 30 Then MsgBox "Up"
If ____.KeyCode = 31 Then MsgBox "Down"
End Sub
 
What do you want to do with it?
 
KeyCode[/] and Shift are parameters of the KeyDown Event. See below for example on how to reference.

Note: adding this line KeyCode=0 cancels the KeyDown
Code:
Private Sub ListBox_KeyDown([i]KeyCode[/i] As Integer, [i]Shift[/i] As Integer) 
  If KeyCode = 30 Then MsgBox "Up" 
  If KeyCode = 31 Then MsgBox "Down" 
End Sub
 
What does the numbers 30 and 31 represent? Try...
Code:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
        
    Select Case KeyCode
        Case vbKeyUp
            MsgBox "The Up key was Pressed"
            KeyCode = 0 'cancels the key pressed
        Case vbKeyDown	
            MsgBox "The Down key was Pressed"
            KeyCode = 0 'cancels the key pressed
        Case vbKeyF2
            MsgBox "The F2 key was Pressed"
            KeyCode = 0 'cancels the key pressed
        Case Else
            'MsgBox "No match!" 'testing
    End Select
    
End Sub
Does that work for your needs to test if the Up or down key was pressed?

Check the help files for the Keycode Constants for all the key codes.
 
OK, dcx693:
I've got a list box that, when an entry is CLICKED, will automatically update some text boxes with data from the clicked entry. This works with the ListBox_Click() Procedure.

The problem is that if the user presses an up or down arrow to change from entry to entry, the text boxes will not update their data because a key is being pressed, not the mouse being clicked.

So, I've started looking into the KeyPress, KeyDown and KeyUp procedures.
KeyPress is useless because it doesn't respond to arrow keys.
KeyDown seemed like the way to go, but unfortunately, it responds to the user's key strokes BEFORE the selection changes in the list box.
KeyUp seems to be the way to go because it responds to the user's key stroke AFTER the selection changes in the list box, but it's quirky-- doesn't seem to be working.
 
ghudson:
31 is supposed to be ASCII for the arrow down key and 30 is supposed to be for the arrow up key.

I was trying to figure out if I could use the KeyUp procedure based on certain cases.
 
What about using the AfterUpdate event instead? That should cover any changes made by the user, no matter how they maneuver around.
 
It's a good point, ghudson, and you'd think that AfterUpdate would handle changes made by the user.

And it does handle mouse-click changes correctly.
But it does not handle up-arrow and down-arrow selection changes correctly:
It handles these events in a KeyDown-style method.
IE: text fields that should be effected by changing from selection to selection are effected with the previous selection (instead of the destination selection).

Therefore, I am currently using my update methods in the Click() procedure as well as a variation on the update mentods (including a Select Case KeyCode procedure) in the KeyUp(...) procedure.

Thanks for your help.
 

Users who are viewing this thread

Back
Top Bottom