Results 1 to 9 of 9

Thread: [RESOLVED] How to press arrow down programitcally ?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    Resolved [RESOLVED] How to press arrow down programitcally ?

    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

  2. #2

  3. #3
    New Member VuVanHoanh's Avatar
    Join Date
    May 2011
    Location
    Viet Nam
    Posts
    9

    Re: How to press arrow down programitcally ?

    The function: 'SendKeys' sends keys you typed in its parameter
    vb Code:
    1. SendKeys(keys As String)
    For example:
    vb Code:
    1. SendKeys "ABC"
    It will send the keys: A, B, C to the active window on your desktop
    Last edited by si_the_geek; May 20th, 2011 at 03:03 AM. Reason: removed 'advertising' link

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    Re: How to press arrow down programitcally ?

    Thanks for your reply. i tried that but it does not press the arrow key down at all !

  5. #5
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: How to press arrow down programitcally ?

    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

  6. #6
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: How to press arrow down programitcally ?

    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
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    Re: How to press arrow down programitcally ?

    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
    Last edited by tony007; May 20th, 2011 at 09:18 PM.

  8. #8
    New Member VuVanHoanh's Avatar
    Join Date
    May 2011
    Location
    Viet Nam
    Posts
    9

    Re: How to press arrow down programitcally ?

    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:
    vb Code:
    1. Const WM_ACTIVATE As Long = &H6
    OK, after it got focus, you can sendkey to it.
    SendKeys "ABC"

  9. #9
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: [RESOLVED] How to press arrow down programitcally ?

    Can you try using SetActiveWindow API on the hwnd of that control before pressing the arrow key?
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

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