Welcome to Dream.In.Code
Getting VB Help is Easy!

Join 131,766 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 2,143 people online right now. Registration is fast and FREE... Join Now!




Keydown

 
Reply to this topicStart new topic

Keydown

Videege
post 13 Apr, 2003 - 07:20 AM
Post #1


rêvant.toujours

Group Icon
Joined: 25 Mar, 2003
Posts: 1,404



Dream Kudos: 150
My Contributions


Hey...I am having some issues with the Form_Keydown event. It works fine normally, but what can I do if I have to have buttons and textboxes in my form? You can't naturally reselect the form, and the Keydown events never take place...any help?

P.S. Thanks to all the people who made suggestions in my "compiling" form..I just had to select "enable compile" from some options menu deep in VB ('doh!)
User is offlineProfile CardPM

Go to the top of the page

DaFeliX
post 13 Apr, 2003 - 10:54 PM
Post #2


D.I.C Head

**
Joined: 10 Nov, 2002
Posts: 192

textboxes and button has the "keydown" event to, or isn't that the problem?
User is offlineProfile CardPM

Go to the top of the page

Videege
post 14 Apr, 2003 - 03:22 PM
Post #3


rêvant.toujours

Group Icon
Joined: 25 Mar, 2003
Posts: 1,404



Dream Kudos: 150
My Contributions


yes, but since the controls are left (arrow keys) right up down sorta thing, it doesn't work that well. I am using the following case statements:

CODE

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
   Dim KeySimp
   KeySimp = KeyCode - 36
   
   
   Select Case KeyCode
       
       
       Case vbKeyLeft
           If shpClientBox.Left - shpServerBox.Left = 1200 And Abs(shpClientBox.Top - shpServerBox.Top) <= 480 Then
           Exit Sub
           Else
           If shpClientBox.Left > 0 Then
           shpClientBox.Left = shpClientBox.Left - 120
           SubmitCoords
           Else
           Exit Sub
           End If
           End If
         
           
           
       Case vbKeyUp
           If shpClientBox.Top - shpServerBox.Top = 480 And Abs(shpServerBox.Left - shpClientBox.Left) <= 1200 Then
           Exit Sub
           Else
           If shpClientBox.Top > 0 Then
           shpClientBox.Top = shpClientBox.Top - 120
           SubmitCoords
           Else
           Exit Sub
           End If
           End If
           
       Case vbKeyRight
       If shpServerBox.Left - shpClientBox.Left = 1200 And Abs(shpServerBox.Top - shpClientBox.Top) <= 480 Then
       Exit Sub
       Else
           If shpClientBox.Left < 5880 Then
           shpClientBox.Left = shpClientBox.Left + 120
           SubmitCoords
           Else
           Exit Sub
           End If
           End If
           
       Case vbKeyDown
       If Abs(shpServerBox.Top - shpClientBox.Top) = 480 And Abs(shpServerBox.Left - shpClientBox.Left) <= 1200 Then
       Exit Sub
       Else
       
         If shpClientBox.Top < 2880 Then
           shpClientBox.Top = shpClientBox.Top + 120
           SubmitCoords
           Else
           Exit Sub
           End If
           End If
           
   End Select
End Sub


the if thens within the cases are just some collision detection, don't mind them. but when i try to put this code in the text box and command button keydown events, it just doesn't work..(e.g., i hit arrow left and it switches focus to another button or text box like I hit the tab button or something).

Please help! pirate.gif

videege
User is offlineProfile CardPM

Go to the top of the page

DaFeliX
post 15 Apr, 2003 - 03:41 AM
Post #4


D.I.C Head

**
Joined: 10 Nov, 2002
Posts: 192

true. this is because the keydown_ event is only for the form itself, so when you press an button in a textbox it has it own event, try to solve it like this:

CODE

Private Function IHitAKey(tKeyASCII As Integer)
' Function can be called anywhere, with only the keyasccii as integer

Private Sub Form_KeyDown(KeyASCII As Integer, Shift As Integer)
  Dim KeySimp
  KeySimp = KeyASCII - 36
 
 
  Select Case KeyASCII
     
     
      Case vbKeyLeft
          If shpClientBox.Left - shpServerBox.Left = 1200 And Abs(shpClientBox.Top - shpServerBox.Top) <= 480 Then
          Exit Sub
          Else
          If shpClientBox.Left > 0 Then
          shpClientBox.Left = shpClientBox.Left - 120
          SubmitCoords
          Else
          Exit Sub
          End If
          End If
       
         
         
      Case vbKeyUp
          If shpClientBox.Top - shpServerBox.Top = 480 And Abs(shpServerBox.Left - shpClientBox.Left) <= 1200 Then
          Exit Sub
          Else
          If shpClientBox.Top > 0 Then
          shpClientBox.Top = shpClientBox.Top - 120
          SubmitCoords
          Else
          Exit Sub
          End If
          End If
         
      Case vbKeyRight
      If shpServerBox.Left - shpClientBox.Left = 1200 And Abs(shpServerBox.Top - shpClientBox.Top) <= 480 Then
      Exit Sub
      Else
          If shpClientBox.Left < 5880 Then
          shpClientBox.Left = shpClientBox.Left + 120
          SubmitCoords
          Else
          Exit Sub
          End If
          End If
         
      Case vbKeyDown
      If Abs(shpServerBox.Top - shpClientBox.Top) = 480 And Abs(shpServerBox.Left - shpClientBox.Left) <= 1200 Then
      Exit Sub
      Else
     
        If shpClientBox.Top < 2880 Then
          shpClientBox.Top = shpClientBox.Top + 120
          SubmitCoords
          Else
          Exit Sub
          End If
          End If
         
  End Select
End Sub
End Function

Private Sub Form_KeyDown(KeyASCII As Integer, Shift As Integer)

IHitAKey KeyASCII

End sub

Private Sub shpServerBox_KeyDown(KeyCode As Integer, Shift As Integer)

IHitAKey KeyASCII

End Sub

Private Sub shpClientBox_KeyDown(KeyCode As Integer, Shift As Integer)

IHitAKey KeyASCII

End Sub


Put all the KeyDown_ events you want to happen the calling to the function

hope this helps ya out
User is offlineProfile CardPM

Go to the top of the page

Videege
post 15 Apr, 2003 - 01:24 PM
Post #5


rêvant.toujours

Group Icon
Joined: 25 Mar, 2003
Posts: 1,404



Dream Kudos: 150
My Contributions


thanks for the help, but that code isn't working...no errors or anything, but still 0 movement. any ideas?
User is offlineProfile CardPM

Go to the top of the page

DaFeliX
post 15 Apr, 2003 - 11:19 PM
Post #6


D.I.C Head

**
Joined: 10 Nov, 2002
Posts: 192

my coding wasn't correct, try the updated code:

CODE
Private Function IHitAKey(tKeyASCII As Integer)
' Function can be called anywhere, with only the keyasccii as integer

 Dim KeySimp
 KeySimp = tKeyASCII - 36
 
 
 Select Case tKeyASCII
     
     
     Case vbKeyLeft
         If shpClientBox.Left - shpServerBox.Left = 1200 And Abs(shpClientBox.Top - shpServerBox.Top) <= 480 Then
         Exit Function
         Else
         If shpClientBox.Left > 0 Then
         shpClientBox.Left = shpClientBox.Left - 120
         SubmitCoords
         Else
         Exit Function
         End If
         End If
       
         
         
     Case vbKeyUp
         If shpClientBox.Top - shpServerBox.Top = 480 And Abs(shpServerBox.Left - shpClientBox.Left) <= 1200 Then
         Exit Function
         Else
         If shpClientBox.Top > 0 Then
         shpClientBox.Top = shpClientBox.Top - 120
         SubmitCoords
         Else
         Exit Function
         End If
         End If
         
     Case vbKeyRight
     If shpServerBox.Left - shpClientBox.Left = 1200 And Abs(shpServerBox.Top - shpClientBox.Top) <= 480 Then
     Exit Function
     Else
         If shpClientBox.Left < 5880 Then
         shpClientBox.Left = shpClientBox.Left + 120
         SubmitCoords
         Else
         Exit Function
         End If
         End If
         
     Case vbKeyDown
     If Abs(shpServerBox.Top - shpClientBox.Top) = 480 And Abs(shpServerBox.Left - shpClientBox.Left) <= 1200 Then
     Exit Function
     Else
     
       If shpClientBox.Top < 2880 Then
         shpClientBox.Top = shpClientBox.Top + 120
         SubmitCoords
         Else
         Exit Function
         End If
         End If
         
 End Select
End Function

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)

IHitAKey KeyCode
End Sub


Private Sub shpServerBox_KeyDown(KeyCode As Integer, Shift As Integer)

IHitAKey KeyCode

End Sub

Private Sub shpClientBox_KeyDown(KeyCode As Integer, Shift As Integer)

IHitAKey KeyCode

End Sub


if this isn't work try to run your project in DEBUG-MODE (F8) and you can see all value's and stuff. good luck
User is offlineProfile CardPM

Go to the top of the page

Videege
post 16 Apr, 2003 - 05:34 PM
Post #7


rêvant.toujours

Group Icon
Joined: 25 Mar, 2003
Posts: 1,404



Dream Kudos: 150
My Contributions


Thanks for your help Felix, but the code just doesn't work...I don't think the KeyAscii var is passing properly...but I solved the problem with my last resort: I seperated the chat controls and movement controls into two forms..it took a bit, but everything should work properly now. Thanks a lot for the help anyways!
User is offlineProfile CardPM

Go to the top of the page

DaFeliX
post 16 Apr, 2003 - 11:03 PM
Post #8


D.I.C Head

**
Joined: 10 Nov, 2002
Posts: 192

the code works @ my place :\

anyway no problem m8, when you need something else just ask, m'kay? wink2.gif
User is offlineProfile CardPM

Go to the top of the page

Videege
post 19 Apr, 2003 - 09:55 AM
Post #9


rêvant.toujours

Group Icon
Joined: 25 Mar, 2003
Posts: 1,404



Dream Kudos: 150
My Contributions


Sure thing. smile.gif
User is offlineProfile CardPM

Go to the top of the page

DaCodeBomb
post 6 Apr, 2008 - 09:36 PM
Post #10


New D.I.C Head

*
Joined: 6 Apr, 2008
Posts: 1

Hey,
I have vb2005. Its really quite simple. All you have to do is turn Key Preview on.
Hope this works
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/20/08 01:17PM

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month