KeyCodes

GaelicFatboy

Registered User.
Local time
Today, 23:04
Joined
Apr 17, 2007
Messages
100
I'm having a spot of bother getting my head round trapping which key has been pressed when using the KeyDown event.

I'm looking for when the left arrow key is pressed so I can move the cursor to the end of the text in an text box. I've looked up the KeyCodes list in the help screen under KeyCode constants for VB applications and got the constant vbKeyLeft as the one I need to use. However using the following code...

If KeyCode = vbKeyleft Then Me.Text_SubAssyNumber.SelStart = Len(Me.Text_SubAssyNumber)

...the code just doesn't trap the left arrow key and won't move the cursor. If I use the following code...

If vbKeyleft Then Me.Text_SubAssyNumber.SelStart = Len(Me.Text_SubAssyNumber)

...the code traps every key press and moves the cursor regardless of which key was actually pressed.

However if I use the following code the arrow key is trapped correctly and the cursor is moved to the end of the text.

If KeyCode = 39 Then Me.Text_SubAssyNumber.SelStart = Len(Me.Text_SubAssyNumber)


I know I've been able to get the code working, but why does using the vbKeyLeft constant not work? Is the KeyCode list I'm looking at the wrong one.

Cheers muckers

D
 
Sorry punters, I've got a laptop with a remote usb keyboard which has been chucking the occassional "odd" key stroke all day and has just locked up altogether.

One re-boot and all seems to be working fine, including the vbKeyCode constants.

Guess I need a new key board.

Sorry muckers

D
 
If you add:
Debug.Print KeyCode
...to your KeyDown event and look at the immediate window after striking your keys, you can see what KeyCodes you are getting. You seem to be looking at the correct list of KeyCodes.
 

Users who are viewing this thread

Back
Top Bottom