Visual Basic Code , VB.NET Code, VB Code
  Home   :  Code   :  Forums   :  Submit   :  Mailing List   :  About   :  Contact
A1VBCode Forums
Home      Members   Calendar   Who's On
Welcome Guest ( Login | Register )
      

Home » Visual Basic (VB 4/5/6) » General Visual Basic » Stop Auto Key Repeat On Held Keys?


Stop Auto Key Repeat On Held Keys?Expand / Collapse
Author
Message
Posted 5/1/2008 12:46:18 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum 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
Post #24794
Posted 5/2/2008 1:09:24 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum 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?
Post #24796
Posted 5/2/2008 7:18:10 AM


Forum God

Forum GodForum GodForum GodForum GodForum GodForum GodForum GodForum God

Group: Forum Members
Last Login: Today @ 9:44:44 AM
Posts: 1,446, Visits: 3,307
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.
Post #24797
Posted 5/2/2008 11:38:33 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum 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
Post #24799
Posted 5/2/2008 3:31:28 PM


Forum God

Forum GodForum GodForum GodForum GodForum GodForum GodForum GodForum God

Group: Forum Members
Last Login: Today @ 9:44:44 AM
Posts: 1,446, Visits: 3,307
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.
Post #24806
Posted 5/2/2008 11:05:41 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum 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





Post #24807
Posted 5/8/2008 9:07:47 AM


Forum Member

Forum MemberForum MemberForum MemberForum MemberForum MemberForum MemberForum MemberForum 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)

Post #24846
Posted 5/10/2008 4:31:01 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum 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
Post #24866
Posted 5/10/2008 11:32:47 AM


Forum God

Forum GodForum GodForum GodForum GodForum GodForum GodForum GodForum God

Group: Forum Members
Last Login: Today @ 8:29:37 AM
Posts: 301, Visits: 1,560
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

Post #24872
Posted 5/10/2008 8:54:31 PM


Forum God

Forum GodForum GodForum GodForum GodForum GodForum GodForum GodForum God

Group: Forum Members
Last Login: Today @ 8:29:37 AM
Posts: 301, Visits: 1,560
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?
Jenna12

As 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
   &n