Results 1 to 7 of 7

Thread: [RESOLVED] Keys

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2006
    Posts
    129

    Resolved [RESOLVED] Keys

    Right now i'm using

    If keycode = vbkeyright then
    me.right = me.right + 100
    endif
    If keycode = vbkeyup then
    me.top = me.top - 100
    endif

    how do i make it so i can press them both at the same time so i can have both effects at the same time?
    Resizing
    System Balloons

    If your question is answered, please mark the thread resolved by pulling down the Thread Tools menu and clicking the Mark Thread Resolved button.

    Remember to rate a post if you find it helpful

  2. #2
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,729

    Re: Keys

    Well, you can't check 2 keys at once. Here is an alternative I found somewhere and modified it a little.

    This code is not very good, 'cause it needs a timer. But it works.

    First, in Key_Up/Down event check which keys are pressed, then inside the timer move the form depending on those keys.
    VB Code:
    1. Option Explicit
    2.  
    3. Dim bUpPressed As Boolean
    4. Dim bDownPressed As Boolean
    5. Dim bLeftPressed As Boolean
    6. Dim bRightPressed As Boolean
    7. '=============================================================
    8.  
    9. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    10.     Select Case KeyCode
    11.         Case vbKeyUp
    12.             bUpPressed = True
    13.             bDownPressed = False
    14.         Case vbKeyDown
    15.             bDownPressed = True
    16.             bUpPressed = False
    17.         Case vbKeyLeft
    18.             bLeftPressed = True
    19.             bRightPressed = False
    20.         Case vbKeyRight
    21.             bRightPressed = True
    22.             bLeftPressed = False
    23.     End Select
    24. End Sub
    25. '=============================================================
    26.  
    27. Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
    28.  
    29.     Select Case KeyCode
    30.         Case vbKeyUp
    31.             bUpPressed = False
    32.         Case vbKeyDown
    33.             bDownPressed = False
    34.         Case vbKeyLeft
    35.             bLeftPressed = False
    36.         Case vbKeyRight
    37.             bRightPressed = False
    38.     End Select
    39.  
    40. End Sub
    41. '=============================================================
    42.  
    43. Private Sub Form_Load()
    44.     Timer1.Interval = 10
    45. End Sub
    46. '=============================================================
    47.  
    48. Private Sub Timer1_Timer()
    49.     If bUpPressed = True Then
    50.         Me.Top = Me.Top - 50
    51.     ElseIf bDownPressed = True Then
    52.         Me.Top = Me.Top + 50
    53.     End If
    54.     '
    55.     If bLeftPressed = True Then
    56.         Me.Left = Me.Left - 50
    57.     ElseIf bRightPressed = True Then
    58.         Me.Left = Me.Left + 50
    59.     End If
    60. End Sub
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


  3. #3
    Addicted Member
    Join Date
    Apr 2006
    Location
    USA
    Posts
    207

    Re: Keys

    Hi lilwupster,

    This code involves a couple API calls, but it's a little more straight forward.
    It should also do what you want.

    Add a timer control first
    VB Code:
    1. 'in the declare section
    2. Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    3. Private Declare Function GetActiveWindow Lib "user32" () As Long
    4.  
    5.  
    6. Private Sub Form_Load()
    7.     Timer1.Interval = 10
    8. End Sub
    9.  
    10.  
    11. Private Sub Timer1_Timer()
    12.     'check if window has focus
    13.     'otherwise window will move even when another program has focus
    14.     If GetActiveWindow() <> Me.hWnd Then Exit Sub
    15.    
    16.     If GetAsyncKeyState(vbKeyUp) Then Me.Top = Me.Top - 15
    17.     If GetAsyncKeyState(vbKeyDown) Then Me.Top = Me.Top + 15
    18.    
    19.     If GetAsyncKeyState(vbKeyLeft) Then Me.Left = Me.Left - 15
    20.     If GetAsyncKeyState(vbKeyRight) Then Me.Left = Me.Left + 15
    21. End Sub

    Hi iPrank, I swear I'm not stalking you. thanks for the rate.
    Keith_VB6

    If you have any further questions, just ask.
    If this solves things, then please mark the thread resolved.
    [Thread Tools] --> [Mark Thread Resolved]

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Feb 2006
    Posts
    129

    Re: Keys

    thank you, they both work very well
    Resizing
    System Balloons

    If your question is answered, please mark the thread resolved by pulling down the Thread Tools menu and clicking the Mark Thread Resolved button.

    Remember to rate a post if you find it helpful

  5. #5
    Addicted Member
    Join Date
    Apr 2006
    Location
    USA
    Posts
    207

    Re: Keys

    Your welcome.
    Keith_VB6

    If you have any further questions, just ask.
    If this solves things, then please mark the thread resolved.
    [Thread Tools] --> [Mark Thread Resolved]

  6. #6
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: Keys

    DirectInput would have also done it.

  7. #7
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,729

    Re: Keys

    Quote Originally Posted by Keith_VB6
    Hi iPrank, I swear I'm not stalking you. thanks for the rate.
    Don't worry.



    iSpy on people everyday.
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width