This code works great for single keys pressed, but I am looking to use this to make a graphic move using two keys. For instance, this code makes the graphic move up, down, left, & right. How about using Up-Left to make the graphic move diagonally Up-Left? Here's the code I have so far, but still need to know how to make the graphic move diagonally...
Create a Class and add the following...
Added to Form1...Code:Option Explicit Public Enum enmDirection kDirLeft = 1 kDirUp = 2 kDirRight = 4 kDirDown = 8 End Enum Event StateChange(ByVal Up As Boolean) Public State As enmDirection Public Sub ProcessKeyEvt(KeyCode As Integer, ByVal Up As Boolean) Dim D As enmDirection: D = State Select Case KeyCode Case 37, 65: D = IIf(Up, D And Not kDirLeft, D Or kDirLeft) Case 38, 87: D = IIf(Up, D And Not kDirUp, D Or kDirUp) Case 39, 68: D = IIf(Up, D And Not kDirRight, D Or kDirRight) Case 40, 83: D = IIf(Up, D And Not kDirDown, D Or kDirDown) End Select If D <> State Then State = D: RaiseEvent StateChange(Up) End Sub
Code:Option Explicit Private WithEvents GK As cGameKeys, x, y Private Sub Form_Load() KeyPreview = True: ScaleMode = vbPixels: DrawWidth = 12 x = ScaleWidth \ 2: y = ScaleHeight \ 2 Set GK = New cGameKeys Timer1.Enabled = False: Timer1.Interval = 50 End Sub Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer) GK.ProcessKeyEvt KeyCode, False End Sub Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer) GK.ProcessKeyEvt KeyCode, True End Sub Private Sub GK_StateChange(ByVal Up As Boolean) Debug.Print "Key-"; IIf(Up, "Up", "Down"), GK.State Timer1.Enabled = GK.State: If Not Up Then Timer1_Timer End Sub Private Sub Timer1_Timer() If GK.State And kDirLeft Then x = x - 1 If GK.State And kDirUp Then y = y - 1 If GK.State And kDirRight Then x = x + 1 If GK.State And kDirDown Then y = y + 1 If GK.State Then Cls: PSet (x, y), vbRed: Caption = x & ", " & y End Sub




Reply With Quote
