| | | Forum Newbie
       
Group: Forum Members Last Login: 5/10/2008 5:53:32 AM Posts: 5, Visits: 19 |
| I've been looking everywhere for this. Does anybody know how to avoid keys auto repeating on held keys in VB6?
Thanks heaps!
Jenna12 |
| | | | Forum Newbie
       
Group: Forum Members Last Login: 5/10/2008 5:53:32 AM Posts: 5, Visits: 19 |
| I'm still rather new to this so please excuse any of my ignorance.
I've made a simple shortened example of my key pressed code below.
[code]
Dim LeftKey, RightKey, UpKey, DownKey As Boolean
Dim Keys, Speed As Integer
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer) '''Detecting KeyDown
If KeyCode = vbKeyLeft Then
LeftKey = True
Keys = Keys + 1
End If
If KeyCode = vbKeyRight Then
RightKey = True
Keys = Keys + 1
End If
If KeyCode = vbKeyUp Then
UpKey = True
Keys = Keys + 1
End If
If KeyCode = vbKeyDown Then
DownKey = True
Keys = Keys + 1
End If
End Sub
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer) '''Detecting KeyUp
If KeyCode = vbKeyLeft Then
LeftKey = False
Keys = Keys - 1
End If
If KeyCode = vbKeyRight Then
RightKey = False
Keys = Keys - 1
End If
If KeyCode = vbKeyUp Then
UpKey = False
Keys = Keys - 1
End If
If KeyCode = vbKeyDown Then
DownKey = False
Keys = Keys - 1
End If
End Sub
Private Sub tmrMove_Timer()
'''' Diagonal speed handling.. Keeps actual 'movement speed' correct on diagonals
If Keys >= 2 Then
Speed = 50
Else: Speed = 100
End If
'''' Object Movement
If LeftKey = True Then Shape1.Left = Shape1.Left - Speed ''' Speed is set elsewhere
If RightKey = True Then Shape1.Left = Shape1.Left + Speed
If UpKey = True Then Shape1.Top = Shape1.Top - Speed
If DownKey = True Then Shape1.Top = Shape1.Top + Speed
End Sub
[/code]
As you can probably gather, the problem here is that the value of 'Keys' keeps counting up once auto key repeat begins (after holding a direction for a while) and when the keys are released it doesn't take it back to 0.
I understand I can handle the diagonal speed differently, but I've ran into other problems with key repeat before, and thought it was about time if found a solution to prevent it rather than work with it..
Thanks
Edit: How come I can't get my code to display as code? |
| | | | 
Forum God
       
Group: Forum Members Last Login: Yesterday @ 4:36:16 PM Posts: 1,463, Visits: 3,400 |
| Welcome to A1vbcode Jenna.  jenna12 (5/2/2008)
Dim LeftKey, RightKey, UpKey, DownKey As Boolean Dim Keys, Speed As Integer
This doesn't sort out your problem but I see from your code that you are new to VB. When you declare variables you have to declare each one seperately otherwise only DownKey will be a Boolean the other 3 will be Variants. The same goes for Speed that will be an Integer, Keys will be a Variant.  Dim LeftKey As Boolean, RightKey As Boolean, UpKey As Boolean, DownKey As Boolean Dim Keys As Integer, Speed As Integer or Dim LeftKey As Boolean Dim RightKey As Boolean Dim UpKey As Boolean Dim DownKey As Boolean Dim Keys As Integer Dim Speed As Integer
Keith http://www.martin2k.co.uk/forums/ I've been programming with VB for 12 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning. |
| | | | Forum Newbie
       
Group: Forum Members Last Login: 5/10/2008 5:53:32 AM Posts: 5, Visits: 19 |
| Thanks, I was actually a little curious about that |
| | | | 
Forum God
       
Group: Forum Members Last Login: Yesterday @ 4:36:16 PM Posts: 1,463, Visits: 3,400 |
| | Thats ok. As regards your key presses, just looking at your code if you press an arrow key keys = keys + 1. When you release the key keys = keys - 1. So keys will never reach 2 when you use the arrow keys. Which part doesn't work correctly? P.S. A litte tip for you Jenna. When you post VB code it looks a lot more professional if you use Mark's Syntax.zip 
Keith http://www.martin2k.co.uk/forums/ I've been programming with VB for 12 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning. |
| | | | Forum Newbie
       
Group: Forum Members Last Login: 5/10/2008 5:53:32 AM Posts: 5, Visits: 19 |
| Just in case anybody else is having similar issues, a simple (and almost obvious) fix for this is to add an extra condition to your If statement on the KeyDown. A condition to simply check if your key is already down.
If KeyCode = vbKeyLeft And LeftKey = False Then...
This isn't exactly what I wanted initially, but it should work for you in most cases and I can't seem to work out a better solution yet (if anybody has any other ides please let me know) Thanks
|
| | | | 
Forum Member
       
Group: Forum Members Last Login: 6/11/2008 2:28:02 AM Posts: 33, Visits: 44 |
| | I think there's another way use form_keyup like this: Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer) If KeyCode = vbKeySpace Then 'whatever you want' End if End Sub This way the command will execute when you release the key, instead of when you hit it Now the problem is that if someone holds it down nothing will happen (until he stops)
|
| | | | Forum Newbie
       
Group: Forum Members Last Login: 5/10/2008 5:53:32 AM Posts: 5, Visits: 19 |
| I don't exactly understand where you're going with this? If you'd like to further explain what you're thinking I'd appreciate it.. Thanks for the response though |
| | | | 
Forum God
       
Group: Forum Members Last Login: Today @ 8:45:13 AM Posts: 349, Visits: 1,705 |
| jenna12 (5/10/2008) I don't exactly understand where you're going with this? If you'd like to further explain what you're thinking I'd appreciate it..Panik is saying that you can avoid the auto repeat issue entirely if you move your code into the KeyUp event. Of course, your object won't move until the key is released though!
"So much to learn. So little time to do it. Wise men know it's later than one thinks"! ***Vote here to make Keith a Moderator*** http://www.a1vbcode.com/vbforums/FindPost23630.aspx |
| | | | 
Forum God
       
Group: Forum Members Last Login: Today @ 8:45:13 AM Posts: 349, Visits: 1,705 |
| jenna12 (5/1/2008) I've been looking everywhere for this. Does anybody know how to avoid keys auto repeating on held keys in VB6? Jenna12As it turns out there is a simple fix. It came to me this afternoon over my second pitcher of beer at my favorite pub. Amazingly enough, it suddenly became very clear... Go figure? Here's an example that has the Shape in a PictureBox instead of the Form. I don't like using the Form for things like this. Option Explicit Dim blnKeyDown As Boolean
Private Sub Form_Load() blnKeyDown = True End Sub
Private Sub Picture1_KeyDown(KeyCode As Integer, Shift As Integer) If blnKeyDown = True Then Select Case KeyCode Case vbKeyUp Shape1.Top = Shape1.Top - 50 Case vbKeyDown Shape1.Top = Shape1.Top + 50 Case vbKeyRight Shape1.Left = Shape1.Left + 50 |
| |
|