Results 1 to 4 of 4

Thread: Using the Keys to move a button

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2000
    Posts
    240

    Question

    Can someone tell me how do i go about declaring certain keys on a keyboard that would allow the user of my application to move a command button around the form of my program?

    Thanks.








    NewBee Vbeer6.0()
    Working Model Edition!! haha
    End Sub

  2. #2
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    keys

    ' trap keys and use your code for their events
    '
    'you need to look in help for keycodes

    'use the Move() function to move your button
    'or simple...command1.left = bla bla
    Option Explicit

    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    Select Case KeyCode
    Case vbKeyF1: MsgBox "F1 pressed."
    Case vbKeyF2: MsgBox "F2 pressed."
    Case vbKeyEscape: MsgBox "ESC"
    End Select
    End Sub

    Private Sub Form_Load()
    Form1.KeyPreview = True
    End Sub


    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  3. #3
    Hyperactive Member Zaphod64831's Avatar
    Join Date
    Mar 2000
    Posts
    268
    In case you'd rather have the actual code, and because I'm extremely bored, here it is:

    Code:
    Option Explicit
    
    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    Dim Increment As Integer
    'Change the 5 to how far you want it to move
    Increment = 5
    Select Case KeyCode 
    Case vbKeyLeft: Command1.Left = Command1.Left - Increment
    Case vbKeyRight: Command1.Left = Command1.Left + Increment
    Case vbKeyUp: Command1.Top = Command1.Top - Increment
    Case vbKeyDown: Command1.Top = Command1.Top + Increment
    End Select 
    End Sub
    
    Private Sub Form_Load() 
    Form1.KeyPreview = True 
    End Sub
    Email: [email protected]

    Home Page: www.olemac.net/~hutch

    I'm bored, VERY bored, and I got bored with my sig. So I changed it to this.

  4. #4
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238

    Lightbulb

    Hi! Zaphod64831, I just try out your code and it seem not working, but it working find for those key others than the "Left", "Right", 'Up" and "Down".

    So, I just wrote something that use Timer to check my keyState, by right i should use the SetWindowHookEx and CallNextHookEx API function instead of Timer. But I fail to do it, may be anyone can show me how can i use the Hook.

    and below is my code
    Code:
    'Code under Form
    Option Explicit
    Private Sub Form_Load()
        load_SetTimer Me.hwnd, 100
    End Sub
    
    Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
        load_KillTimer Me.hwnd
    End Sub
    
    'Code under Basic Module
    Option Explicit
    Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    Private Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
    Private Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long
    
    Private Const VK_DOWN = &H28
    Private Const VK_LEFT = &H25
    Private Const VK_RIGHT = &H27
    Private Const VK_UP = &H26
    
    Public Sub load_SetTimer(ByVal MyhWnd As Long, ByVal MyInterval As Long)
        SetTimer MyhWnd, 0, MyInterval, AddressOf TimerProc
    End Sub
    
    Sub TimerProc(ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long)
        'We should check for the form boundary before moving the CommandButton.
        If GetAsyncKeyState(VK_UP) Then
            If frmMain.Command1.Top > 0 Then frmMain.Command1.Top = frmMain.Command1.Top - 10
        ElseIf GetAsyncKeyState(VK_DOWN) Then
            If frmMain.Command1.Top < frmMain.ScaleHeight Then frmMain.Command1.Top = frmMain.Command1.Top + 10
        ElseIf GetAsyncKeyState(VK_LEFT) Then
            If frmMain.Command1.Left > 0 Then frmMain.Command1.Left = frmMain.Command1.Left - 10
        ElseIf GetAsyncKeyState(VK_RIGHT) Then
            If frmMain.Command1.Left < frmMain.ScaleWidth Then frmMain.Command1.Left = frmMain.Command1.Left + 10
        End If
    End Sub
    
    Public Sub load_KillTimer(ByVal MyhWnd As Long)
         KillTimer MyhWnd, 0
    End Sub

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