CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Mar 2010
    Posts
    4

    Question VB6 - Simulate Shift+Click and Ctrl+Click

    I looked everywhere but can not find, how to simulate shift+left mouse click and ctrl+left mouse click?

    anyone know?

    thanks!

  2. #2
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: VB6 - Simulate Shift+Click and Ctrl+Click

    Usually you'd use SendMessage() API to send the WM_LBUTTONDOWN message to a hWnd.
    Code:
    SendMessage myhWnd, WM_LBUTTONDOWN, MK_SHIFT, 0&
    The wParam (MK_SHIFT) should reflect the state of the shift, alt or ctrl key.
    For some reason I was not able to SendMessage the MK_SHIFT to a VB control.
    Try to research more on SendMessage WM_LBUTTONDOWN and th efollowing wParam parameter.

  3. #3
    Join Date
    Mar 2010
    Posts
    4

    Re: VB6 - Simulate Shift+Click and Ctrl+Click

    Thank you WoF, MK_SHIFT can even report the status of the keys shifit, ctrl and alt, but what the exact time of sendkeys one of these buttons to simulate a left mouse click and result shift + click or ctrl + click, I can not find these in place, I hope to be able to in VB6, I know that Delphi is possible, I do not have to change the programming language here I'll get it

  4. #4
    Join Date
    Feb 2002
    Location
    Makati City, Philippines
    Posts
    1,054

    Re: VB6 - Simulate Shift+Click and Ctrl+Click

    Can you elaborate more on this? ....

    Quote:
    "... but what the exact time of sendkeys one of these buttons to simulate a left mouse click and result shift + click or ctrl + click, I can not find these in place...."

    Are you trying to measure the length of time the key has been pressed at a time mouse was also pressed?

    Anyway, here is another possible option.

    Code:
    Option Explicit
    
    'shift state placeholder
        Dim kstatus As Integer
    
    '   -- save the keyboard/shift state when you press or release them
    Private Sub UserForm_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
        kstatus = Shift
    End Sub
    
    Private Sub UserForm_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
        kstatus = Shift
    End Sub
    
    
    '   Then check the kstatus when you click the mouse. You can also use mouse_down
    Private Sub UserForm_Click()
        Select Case kstatus
        Case 1
            MsgBox "Shift pressed"
        Case 2
            MsgBox "Ctrl pressed"
        Case 4
            MsgBox "Alt pressed"
        Case Else
            MsgBox "No shift"
        End Select
    End Sub
    I dont have VB in the pc I am using right now and have not programmed in VB for over 5 years now. But it should be something like that (I used Excel VBA form which is slightly different) in this sample code.
    Marketing our skills - please participate in the survey and share your insights
    -

  5. #5
    Join Date
    Mar 2010
    Posts
    4

    Re: VB6 - Simulate Shift+Click and Ctrl+Click

    I understand, the problem is not knowing when a key (shift or ctrl) was pressed, is to simulate pressing shift or control and left click the mouse while

  6. #6
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: VB6 - Simulate Shift+Click and Ctrl+Click

    To send a MouseDown to a control or form you use SendMessage like this:
    SendMessage hWnd, WM_LBUTTONDOWN, 0&, 0&
    where hWnd is the window handle of the control or form.
    If you would subclass a windowproc you will find that that the incoming message WM_LBUTTONDOWN is accompanied by two more parameters (wParam and lParam).
    wParam would - in case of additional shift keys held down - contain the bits for them
    MK_SHIFT (&H4), MK_ALT (&H10) and MK_CONTROL (&H8)
    For SendMessage() wParam is the third parameter. So it is to be expected that if you would set the apropriate bits it would cause a shift-click:
    SendMessage hWnd, WM_LBUTTONDOWN, MK_SHIFT, 0&
    But obviously the shift information is not recognized by the receiving window.
    The remaining question is: Why is this?
    I did not have time for deeper research yet, sorry. Maybe somebody comes up with an idea now that the facts are explained again.

  7. #7
    Join Date
    Mar 2010
    Posts
    4

    Re: VB6 - Simulate Shift+Click and Ctrl+Click

    Thank you for trying to help, we all know how to move the mouse cursor in any direction, giving left click, middle click and right click, but could not find anywhere how to simulate the shift + left click and ctrl + left click, are two types of clicks important, without them a function to simulate mouse is not complete. I hope someone has the answer

  8. #8
    Join Date
    Mar 2010
    Posts
    26

    Re: VB6 - Simulate Shift+Click and Ctrl+Click

    I guess you are trying to select a rang of items in a list like in windows explorer or the likes.
    **** lets you select between two points ctrl lets you select additional items.

    This is some code that does what I think you want to do:

    1. It sends a mouse click to position 100,100 on your screen - regardless of the app
    2. it simulates a down shift on the "Shift" Key
    3. It mouse clicks on position 100,400
    4. it releases the **** key

    The effect is it selects whatever is between these two points.

    I think this does what you want.

    Hope it helps
    Attached Files Attached Files

  9. #9
    Join Date
    Mar 2010
    Posts
    26

    Re: VB6 - Simulate Shift+Click and Ctrl+Click

    LOL the "****" say "Shift"

    I am not swearing!!!

  10. #10
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: VB6 - Simulate Shift+Click and Ctrl+Click

    Well, that's clever to use the keybd_event() API. Very good.
    I tried to use the SendMessage() with WM_KEYDOWN, which did not give the proper results.
    Well done.

  11. #11
    Join Date
    Mar 2010
    Posts
    26

    Re: VB6 - Simulate Shift+Click and Ctrl+Click

    Thks WoF

    Using the keybd_event approach with SendMessage() also seems to work.ie:

    Call keybd_event(16, 0, 0, 0)
    Sleep 100oEvents
    Call SendMessage(hwnd, &HF5, 0&, 0&) ' Button Click
    Call keybd_event(16, 0, 2, 0)

    I' ve tried this in the attached and it seems to work for "shift" at least. It need a sleep or DoEvents call to let the keypress event occur.

    I cannot really see any practical way the OP would want to use it unless he was trying to click specific co-ordinates on the desktop rather than to raise click events on a specified control. He
    doesn't really make it clear.

    Anyway he can do it both ways using this kind of approach.
    Attached Files Attached Files

  12. #12
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: VB6 - Simulate Shift+Click and Ctrl+Click

    Yes. Very good.
    I really wonder why the SendMessage() with WM_KEYDOWN and the code for the shift key does not produce the same result as a keybd_event() call. I would have expected the same result, but obviously it doesn't do.

  13. #13
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: VB6 - Simulate Shift+Click and Ctrl+Click

    Very nice!

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

Featured