Hi all . I want to create a button so when i press it it press arrown key down in keyboard.could any one show me how this can be done?thanks
Printable View
Hi all . I want to create a button so when i press it it press arrown key down in keyboard.could any one show me how this can be done?thanks
SendKeys "{DOWN}"
The function: 'SendKeys' sends keys you typed in its parameter
For example:vb Code:
SendKeys(keys As String)
It will send the keys: A, B, C to the active window on your desktopvb Code:
SendKeys "ABC"
Thanks for your reply. i tried that but it does not press the arrow key down at all !
One thing to watch out for is that CommandButtons (and some other Controls) consume the KeyDown events of the arrow keys. (Pressing the Up Arrow key, when a CommandButton has focus, will set focus to the previous control in the TabIndex sequence, pressing the Down Arrow key will set focus to the next control in the Tabindex sequence)
Thus the KeyUp event is triggered in the control that gets the focus. If you have a simple Form with a CommandButton and a Textbox you can see what I mean by running this:
Code:Private Sub Command1_Click()
SendKeys "{UP}"
End Sub
Private Sub Text1_Keyup(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyUp Then
MsgBox "Up arrow pressed in Command1"
End If
End Sub
If you are in Vista or Win7 then SendKeys has an issue, you're better of using keybd_event.
Code:Option Explicit
Private Declare Sub keybd_event Lib "user32" ( _
ByVal bVk As Byte, _
ByVal bScan As Byte, _
ByVal dwFlags As Long, _
ByVal dwExtraInfo As Long)
Private Const VK_DOWN As Long = &H28
Private Const KEYEVENTF_KEYUP As Long = &H2
Public Sub PressKeyDown()
keybd_event VK_DOWN, 0, 0, 0 ' press
keybd_event VK_DOWN, 0, KEYEVENTF_KEYUP, 0 ' release
End Sub
Private Sub Command1_Click()
Text1.SetFocus
PressKeyDown
End Sub
Thanks all but non of the above solution work for me. There is accordion ctl in another application where the it has focus on first item. I have the Hwnd and all i want programitcally press arrow down button in keyboard so the focus goes to next item in accodion ctl list.. could any one help me achieve this task ?Thanks
edit: i was able to send keydown to it:
Code:Private Declare Function AttachThreadInput Lib "user32.dll" (ByVal idAttach As Long, ByVal idAttachTo As Long, ByVal fAttach As Long) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32.dll" (ByVal hwnd As Long, ByRef lpdwProcessId As Long) As Long
Private Declare Function SetFocusAPI Lib "user32.dll" Alias "SetFocus" (ByVal hwnd As Long) As Long
Private Sub Command1_Click()
Dim lThreadID As Long, lChildhWnd As Long
lChildhWnd = &H1505BC ' << provide correct value
lThreadID = GetWindowThreadProcessId(lChildhWnd, ByVal 0&)
AttachThreadInput lThreadID, App.ThreadID, True
SetFocusAPI lChildhWnd
SendKeys "{DOWN}"
AttachThreadInput lThreadID, App.ThreadID, False
End Sub
It isn't a problem. The sendkeys will sends the keys you typed to it. But first, you must set focus to it.
You should use 'SendMessage' API function with the constant:
OK, after it got focus, you can sendkey to it.vb Code:
Const WM_ACTIVATE As Long = &H6
SendKeys "ABC"
Can you try using SetActiveWindow API on the hwnd of that control before pressing the arrow key?