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
Printable View
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
' 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
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
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