Send a mouse click?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • BadOmen

    Send a mouse click?

    I want my program to send a mouse click to the window at the current mouse
    position, how do I do that?

    Example:
    I have my mouse over a button in Word and then my program is sending the
    left mouse click and the button under the mouse is clicked.

    Yours, Jonas


  • BadOmen

    #2
    Re: Send a mouse click?

    The I am using the "keybd_even t" but the mouse buttons does not work,
    VK_LBUTTON and VK_RBUTTON does not work, way???
    The rest seams to work fine :-)


    This is how i am faking a pressed key:

    In a .bas file:

    Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal _
    bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)



    Public Enum enumKeys
    VK_LBUTTON = 1
    VK_RBUTTON = 2
    VK_MBUTTON = 4 '/* NOT contiguous with L & RBUTTON */

    VK_VOLUME_MUTE = 173
    'Windows 2000/XP: Volume Mute key

    VK_VOLUME_DOWN = &HAE
    'Windows 2000/XP: Volume Down key

    VK_VOLUME_UP = &HAF
    'Windows 2000/XP: Volume Up key

    VK_MEDIA_NEXT_T RACK = 176
    'Windows 2000/XP: Next Track key

    VK_MEDIA_Prev_T RACK = 177
    'Windows 2000/XP: Previous Track key

    VK_MEDIA_STOP = 178
    'Windows 2000/XP: Stop Media key

    VK_MEDIA_PLAY_P AUSE = 179
    'Windows 2000/XP: Stop Media key

    End Enum

    Public Sub PressKeyVK(keyP ress As enumKeys, Optional bHold As Boolean, _
    Optional bRelease As Boolean, Optional bCompatible As Boolean)

    keybd_event keyPress, 0, 0, 0

    keybd_event keyPress, 0, 2, 0

    End Sub

    In a form File:

    'To make Media Player Stop
    PressKeyVK VK_MEDIA_STOP '***Works Fine

    'Left Mouse botton
    PressKeyVK VK_LBUTTON '*** Do NOT work at all!

    'Right Mouse Button
    PressKeyVK VK_RBUTTON '*** Do NOT work at all!


    Yours, Jonas


    "BadOmen" <badomen02@hotm ail.com> skrev i meddelandet
    news:poPXb.4912 6$mU6.193264@ne wsb.telia.net.. .[color=blue]
    > I want my program to send a mouse click to the window at the current mouse
    > position, how do I do that?
    >
    > Example:
    > I have my mouse over a button in Word and then my program is sending the
    > left mouse click and the button under the mouse is clicked.
    >
    > Yours, Jonas
    >
    >[/color]


    Comment

    • J French

      #3
      Re: Send a mouse click?

      On Mon, 16 Feb 2004 14:37:00 GMT, "BadOmen" <badomen02@hotm ail.com>
      wrote:
      [color=blue]
      >The I am using the "keybd_even t" but the mouse buttons does not work,
      >VK_LBUTTON and VK_RBUTTON does not work, way???
      >The rest seams to work fine :-)[/color]

      Mouse_event is for mouse clicks

      Keybd_event is for key presses

      Comment

      • BadOmen

        #4
        Re: Send a mouse click?


        "J French" <erewhon@nowher e.com> skrev i meddelandet
        news:4031e204.8 354645@news.btc lick.com...[color=blue]
        > On Mon, 16 Feb 2004 14:37:00 GMT, "BadOmen" <badomen02@hotm ail.com>
        > wrote:
        >[color=green]
        > >The I am using the "keybd_even t" but the mouse buttons does not work,
        > >VK_LBUTTON and VK_RBUTTON does not work, way???
        > >The rest seams to work fine :-)[/color]
        >
        > Mouse_event is for mouse clicks
        >
        > Keybd_event is for key presses[/color]

        Ops, thanx

        Now I so that Keybd_event and Mouse_event is superseded. I don't really
        know what it means, but I think It's best avoiding it.
        I am using the SendInput function instead, but I don't really understand how
        it works. The msdn is not that simple I think, plus everything I found on
        SendInput was written in C/C++.

        This is how I am doing it (I found an example and changed it a bit)


        Option Explicit

        Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMem ory" (pDst As
        Any, pSrc As Any, ByVal ByteLen As Long)
        Private Declare Function SendInput Lib "user32.dll " (ByVal nInputs As Long,
        pInputs As INPUT_TYPE, ByVal cbSize As Long) As Long

        Public Enum MouseClick 'Set the constans of the mouse buttons
        MOUSEEVENTF_LEF TDOWN = &H2
        MOUSEEVENTF_LEF TUP = &H4
        MOUSEEVENTF_RIG HTDOWN = &H8
        MOUSEEVENTF_RIG HTUP = &H10
        MOUSEEVENTF_MID DLEDOWN = &H20
        MOUSEEVENTF_MID DLEUP = &H40
        End Enum

        Private Const INPUT_MOUSE = 0
        Private Const INPUT_KEYBOARD = 1
        Private Const INPUT_HARDWARE = 2

        Type MOUSEINPUT
        dx As Long
        dy As Long
        mouseData As Long
        dwFlags As Long
        dwtime As Long
        dwExtraInfo As Long
        End Type

        '***This I would be niding to make a KeyPress, But I dont know how...
        Type KEYBDINPUT
        wVk As Integer
        wScan As Integer
        dwFlags As Long
        time As Long
        dwExtraInfo As Long
        End Type

        Type INPUT_TYPE
        dwType As Long
        xi(0 To 23) As Byte
        End Type



        Public Sub ClickDaMouse(Bu ttonPressed As MouseClick, ButtonRelise As
        MouseClick)

        Dim intX As Integer
        Dim inputEvents(0 To 1) As INPUT_TYPE ' holds information about each
        event
        Dim mouseEvent As MOUSEINPUT ' temporarily hold mouse input info

        ' Load the information needed to synthesize pressing the mouse button.
        mouseEvent.dx = 0 ' no horizontal movement
        mouseEvent.dy = 0 ' no vertical movement
        mouseEvent.mous eData = 0 ' not needed
        mouseEvent.dwFl ags = ButtonPressed ' button down
        mouseEvent.dwti me = 0 ' use the default
        mouseEvent.dwEx traInfo = 0 ' not needed
        ' Copy the structure into the input array's buffer.
        inputEvents(0). dwType = INPUT_MOUSE ' mouse input
        CopyMemory inputEvents(0). xi(0), mouseEvent, Len(mouseEvent)

        ' Do the same as above, but for releasing the mouse button.
        mouseEvent.dx = 0 ' no horizontal movement
        mouseEvent.dy = 0 ' no vertical movement
        mouseEvent.mous eData = 0 ' not needed
        mouseEvent.dwFl ags = ButtonRelise ' button up
        mouseEvent.dwti me = 0 ' use the default
        mouseEvent.dwEx traInfo = 0 ' not needed
        ' Copy the structure into the input array's buffer.
        inputEvents(1). dwType = INPUT_MOUSE ' mouse input
        CopyMemory inputEvents(1). xi(0), mouseEvent, Len(mouseEvent)

        ' Now that all the information for the 2 input events has been placed
        ' into the array, finally send it into the input stream.
        intX = SendInput(2, inputEvents(0), Len(inputEvents (0))) ' place the
        events into the stream

        End Sub

        ++++++++

        I think I can use this keys because with them I don't need to specified a
        window to sent the information to:

        VK_VOLUME_MUTE = 173
        'Windows 2000/XP: Volume Mute key
        VK_VOLUME_DOWN = &HAE

        'Windows 2000/XP: Volume Down key

        VK_VOLUME_UP = &HAF
        'Windows 2000/XP: Volume Up key


        VK_MEDIA_NEXT_T RACK = 176
        'Windows 2000/XP: Next Track key

        VK_MEDIA_Prev_T RACK = 177
        'Windows 2000/XP: Previous Track key

        VK_MEDIA_STOP = 178
        'Windows 2000/XP: Stop Media key

        VK_MEDIA_PLAY_P AUSE = 179
        'Windows 2000/XP: Stop Media key


        But I want to be able to send Alt+X or CTRL+4 and Shift+a or something like
        that to a specific window not the one that have the focus.
        maybe I can use this:

        Declare Function FindWindow Lib "user32" Alias "FindWindow A" (ByVal
        lpClassName As String, ByVal lpWindowName As String) As Long

        WinAMPhWnd = FindWindow("Win amp v1.x", vbNullString)

        Than I think if I have understand it correct that WinAMPhWnd is holding the
        unique ID for WinAmp in this case, which is what hWnd stands for in some
        code???

        No I don't know how to use that ID to send a key to that window...???

        Yours, Jonas


        Comment

        • J French

          #5
          Re: Send a mouse click?

          On Thu, 19 Feb 2004 16:25:34 GMT, "BadOmen" <badomen02@hotm ail.com>
          wrote:

          <snip>
          [color=blue]
          >
          >Than I think if I have understand it correct that WinAMPhWnd is holding the
          >unique ID for WinAmp in this case, which is what hWnd stands for in some
          >code???
          >
          >No I don't know how to use that ID to send a key to that window...???[/color]

          I don't use the SendInput API as it is not supported on Win95
          - I like to write for older platforms

          However you will find an example in the downloadable API Guide from


          I assume that it sends the data to the current Window with focus
          - so you will need to find your target App's hWnd and use
          SetForegroundWi ndow to it

          I can't work out how much you know about Windows Handles, but assuming
          it is very little, a hWnd is simply a unique number that Windows uses
          to identify each 'window'
          - that number is pretty random and can even (sometimes) change at
          runtime
          - to locate a specific window you need to know something about it,
          which can be a painful process

          This snooper may give you some hints:

          Option Explicit

          ' Add one Timer

          Private Type POINTAPI
          X As Long
          Y As Long
          End Type
          Private Declare Function GetCursorPos _
          Lib "user32" _
          (lpPoint As POINTAPI) As Long

          Private Declare Function WindowFromPoint _
          Lib "user32" _
          (ByVal xPoint As Long, _
          ByVal yPoint As Long) As Long

          Private Declare Function GetClassName _
          Lib "user32" _
          Alias "GetClassNa meA" _
          (ByVal hwnd As Long, _
          ByVal lpClassName As String, _
          ByVal nMaxCount As Long) As Long

          Private Declare Function SendMessage Lib "user32" Alias "SendMessag eA"
          ( _
          ByVal hwnd As Long, _
          ByVal wMsg As Long, _
          ByVal wParam As Long, _
          lParam As Any) As Long

          Private Const WM_GETTEXT = &HD


          Private Sub Form_Load()
          Timer1.Enabled = True
          Timer1.Interval = 500
          End Sub

          Private Function WindowInf() As String
          Dim Hnd As Long, Buff$, Q&
          Dim PT As POINTAPI

          GetCursorPos PT
          Hnd = WindowFromPoint (PT.X, PT.Y)

          WindowInf = Str$(Hnd)
          ' ---
          Buff$ = Space$(255)
          Q& = GetClassName(Hn d, Buff$, Len(Buff$))
          WindowInf = WindowInf + ":" + Left$(Buff$, Q)
          ' ---
          Q& = SendMessage(Hnd , WM_GETTEXT, Len(Buff$), ByVal Buff$)
          WindowInf = WindowInf + ":" + Left$(Buff$, Q)

          End Function


          Private Sub Timer1_Timer()
          Me.Caption = WindowInf
          End Sub




          Comment

          • BadOmen

            #6
            Re: Send a mouse click?

            Thanx that helped :)

            I have posting a new message:
            "Can I see if an app is bieng closed if I have it's hWnd?"
            :-)

            But I will use your method if I don't have the hWnd already, by searching
            for the title I now know the program have by using your little program :)

            Yours, Jonas
            "J French" <erewhon@nowher e.com> skrev i meddelandet
            news:4035d1dc.2 46537536@news.b tclick.com...[color=blue]
            > On Thu, 19 Feb 2004 16:25:34 GMT, "BadOmen" <badomen02@hotm ail.com>
            > wrote:
            >
            > <snip>
            >[color=green]
            > >
            > >Than I think if I have understand it correct that WinAMPhWnd is holding[/color][/color]
            the[color=blue][color=green]
            > >unique ID for WinAmp in this case, which is what hWnd stands for in some
            > >code???
            > >
            > >No I don't know how to use that ID to send a key to that window...???[/color]
            >
            > I don't use the SendInput API as it is not supported on Win95
            > - I like to write for older platforms
            >
            > However you will find an example in the downloadable API Guide from
            > www.AllAPI.net
            >
            > I assume that it sends the data to the current Window with focus
            > - so you will need to find your target App's hWnd and use
            > SetForegroundWi ndow to it
            >
            > I can't work out how much you know about Windows Handles, but assuming
            > it is very little, a hWnd is simply a unique number that Windows uses
            > to identify each 'window'
            > - that number is pretty random and can even (sometimes) change at
            > runtime
            > - to locate a specific window you need to know something about it,
            > which can be a painful process
            >
            > This snooper may give you some hints:
            >
            > Option Explicit
            >
            > ' Add one Timer
            >
            > Private Type POINTAPI
            > X As Long
            > Y As Long
            > End Type
            > Private Declare Function GetCursorPos _
            > Lib "user32" _
            > (lpPoint As POINTAPI) As Long
            >
            > Private Declare Function WindowFromPoint _
            > Lib "user32" _
            > (ByVal xPoint As Long, _
            > ByVal yPoint As Long) As Long
            >
            > Private Declare Function GetClassName _
            > Lib "user32" _
            > Alias "GetClassNa meA" _
            > (ByVal hwnd As Long, _
            > ByVal lpClassName As String, _
            > ByVal nMaxCount As Long) As Long
            >
            > Private Declare Function SendMessage Lib "user32" Alias "SendMessag eA"
            > ( _
            > ByVal hwnd As Long, _
            > ByVal wMsg As Long, _
            > ByVal wParam As Long, _
            > lParam As Any) As Long
            >
            > Private Const WM_GETTEXT = &HD
            >
            >
            > Private Sub Form_Load()
            > Timer1.Enabled = True
            > Timer1.Interval = 500
            > End Sub
            >
            > Private Function WindowInf() As String
            > Dim Hnd As Long, Buff$, Q&
            > Dim PT As POINTAPI
            >
            > GetCursorPos PT
            > Hnd = WindowFromPoint (PT.X, PT.Y)
            >
            > WindowInf = Str$(Hnd)
            > ' ---
            > Buff$ = Space$(255)
            > Q& = GetClassName(Hn d, Buff$, Len(Buff$))
            > WindowInf = WindowInf + ":" + Left$(Buff$, Q)
            > ' ---
            > Q& = SendMessage(Hnd , WM_GETTEXT, Len(Buff$), ByVal Buff$)
            > WindowInf = WindowInf + ":" + Left$(Buff$, Q)
            >
            > End Function
            >
            >
            > Private Sub Timer1_Timer()
            > Me.Caption = WindowInf
            > End Sub
            >
            >
            >
            >[/color]


            Comment

            • J French

              #7
              Re: Send a mouse click?

              On Fri, 20 Feb 2004 14:11:17 GMT, "BadOmen" <badomen02@hotm ail.com>
              wrote:
              [color=blue]
              >Thanx that helped :)
              >
              >I have posting a new message:
              >"Can I see if an app is bieng closed if I have it's hWnd?"
              >:-)
              >
              >But I will use your method if I don't have the hWnd already, by searching
              >for the title I now know the program have by using your little program :)[/color]

              You will certainly not get a notification from the hWnd or 'Hooking'
              that an app is being closed

              ( well you sort of can, but it is dangerous )

              I'm not entirely sure what you want to do
              - and it is likely that others will know more

              So I suggest that you post again,
              stating where you are and what you want


              Comment

              • BadOmen

                #8
                Re: Send a mouse click?

                I have posted a new topic but i think I will have to do it like this than:


                "J French" <erewhon@nowher e.com> skrev i meddelandet
                news:403643d1.2 75714965@news.b tclick.com...[color=blue]
                > On Fri, 20 Feb 2004 14:11:17 GMT, "BadOmen" <badomen02@hotm ail.com>
                > wrote:
                >[color=green]
                > >Thanx that helped :)
                > >
                > >I have posting a new message:
                > >"Can I see if an app is bieng closed if I have it's hWnd?"
                > >:-)
                > >
                > >But I will use your method if I don't have the hWnd already, by searching
                > >for the title I now know the program have by using your little program :)[/color]
                >
                > You will certainly not get a notification from the hWnd or 'Hooking'
                > that an app is being closed
                >
                > ( well you sort of can, but it is dangerous )
                >
                > I'm not entirely sure what you want to do
                > - and it is likely that others will know more
                >
                > So I suggest that you post again,
                > stating where you are and what you want
                >
                >[/color]


                Comment

                • BadOmen

                  #9
                  Re: Send a mouse click?

                  I accidently hit a combination on my keyboard that just posted my message...
                  :-P
                  I was just about to past this:


                  WinAMPhWnd = FindWindow("Win amp v1.x", vbNullString)
                  If WinAMPhWnd = 0 Then
                  MsgBox "WinAMP doesn't appear to be open!", vbExclamation, "Ack!"

                  End If

                  But i w0uld like to have some thing that could use the hWnd instead of the
                  tilted...

                  "BadOmen" <badomen02@hotm ail.com> skrev i meddelandet
                  news:2lKZb.4981 7$mU6.197942@ne wsb.telia.net.. .[color=blue]
                  > I have posted a new topic but i think I will have to do it like this than:
                  >
                  >
                  > "J French" <erewhon@nowher e.com> skrev i meddelandet
                  > news:403643d1.2 75714965@news.b tclick.com...[color=green]
                  > > On Fri, 20 Feb 2004 14:11:17 GMT, "BadOmen" <badomen02@hotm ail.com>
                  > > wrote:
                  > >[color=darkred]
                  > > >Thanx that helped :)
                  > > >
                  > > >I have posting a new message:
                  > > >"Can I see if an app is bieng closed if I have it's hWnd?"
                  > > >:-)
                  > > >
                  > > >But I will use your method if I don't have the hWnd already, by[/color][/color][/color]
                  searching[color=blue][color=green][color=darkred]
                  > > >for the title I now know the program have by using your little program[/color][/color][/color]
                  :)[color=blue][color=green]
                  > >
                  > > You will certainly not get a notification from the hWnd or 'Hooking'
                  > > that an app is being closed
                  > >
                  > > ( well you sort of can, but it is dangerous )
                  > >
                  > > I'm not entirely sure what you want to do
                  > > - and it is likely that others will know more
                  > >
                  > > So I suggest that you post again,
                  > > stating where you are and what you want
                  > >
                  > >[/color]
                  >
                  >[/color]


                  Comment

                  • J French

                    #10
                    Re: Send a mouse click?

                    On Sat, 21 Feb 2004 15:02:53 GMT, "BadOmen" <badomen02@hotm ail.com>
                    wrote:
                    [color=blue]
                    >I accidently hit a combination on my keyboard that just posted my message...
                    >:-P
                    >I was just about to past this:
                    >
                    >
                    > WinAMPhWnd = FindWindow("Win amp v1.x", vbNullString)
                    > If WinAMPhWnd = 0 Then
                    > MsgBox "WinAMP doesn't appear to be open!", vbExclamation, "Ack!"
                    >
                    > End If
                    >
                    >But i w0uld like to have some thing that could use the hWnd instead of the
                    >tilted...[/color]

                    I do not understand 'tilted'
                    .... Do you mean 'Title' ?

                    Look, let us sort one thing out /clearly/
                    - the hWnd is just an arbitrary number assigned by the OS


                    Comment

                    • BadOmen

                      #11
                      Re: Send a mouse click?


                      "J French" <erewhon@nowher e.com> skrev i meddelandet
                      news:4037a2ef.3 0666659@news.bt click.com...[color=blue]
                      > On Sat, 21 Feb 2004 15:02:53 GMT, "BadOmen" <badomen02@hotm ail.com>
                      > wrote:
                      >[color=green]
                      > >I accidently hit a combination on my keyboard that just posted my[/color][/color]
                      message...[color=blue][color=green]
                      > >:-P
                      > >I was just about to past this:
                      > >
                      > >
                      > > WinAMPhWnd = FindWindow("Win amp v1.x", vbNullString)
                      > > If WinAMPhWnd = 0 Then
                      > > MsgBox "WinAMP doesn't appear to be open!", vbExclamation, "Ack!"
                      > >
                      > > End If
                      > >
                      > >But i w0uld like to have some thing that could use the hWnd instead of[/color][/color]
                      the[color=blue][color=green]
                      > >tilted...[/color]
                      >
                      > I do not understand 'tilted'
                      > ... Do you mean 'Title' ?
                      >
                      > Look, let us sort one thing out /clearly/
                      > - the hWnd is just an arbitrary number assigned by the OS
                      >
                      >[/color]

                      I meant title but the spell checking program ducked(fucked) it up :P

                      I know that hWnd is a unique Id that specifie a program or a window. So if I
                      had a programs unique ID (assign by the OS) than I hoped I could get the
                      messages from that program when the program is tolled to close by the user
                      so I know when I can't send commands to that program...
                      like a close command if I want.

                      Ok... Now I have reconfigured it and made it much simpler... I just make a
                      check if the program is still running before I send any commands of any
                      kind...

                      I make the check like this:

                      WinAMPhWnd = FindWindow("Win amp v1.x", vbNullString)
                      If WinAMPhWnd = 0 Then
                      MsgBox "WinAMP doesn't appear to be open!", vbExclamation, "Ack!"

                      End If

                      Some background info on what I am doing:

                      I have made a program that works with a Infra Read Remote I can as an
                      example move the mouse, click the mouse buttons or start WinAmp5 and
                      play/pause, next, back Fast Forward and rew and more.

                      Now if I start WinAmp with the remote control then I get WinAmps hWin if
                      WinAmp is started "by hand" then I use WinAMPhWnd = FindWindow("Win amp
                      v1.x", vbNullString) to get it's hWind.

                      Then I check before I send a command to WinAmp If WinAMPhWnd is True (or
                      rather not 0, False) then I send the command..

                      Now I want to be able to take the hWnd from the active window using the
                      GetActiveWindow API but it just returns 0 if I have WinAmp5 as the active
                      window. I don't know way???
                      Then I want to use this to quite the active program.

                      Dim ReturnState As Long

                      ReturnState = PostMessage(hWn dHandler, WM_QUIT, 0&, 0&)

                      If I use this WinAMPhWnd = FindWindow("Win amp v1.x", vbNullString) to get
                      it's hWind an than use PostMessage it works fine and quits WinAmp. But I
                      want it to work on all active programs...

                      I have just used VB for one or maybe two month so I am a bit new to this...
                      I don't know "all the good things and all the bad thing, that may be"...
                      :-P

                      I have posted a new topic acing about under the subject "Quitting a window,
                      using GetActiveWindow () API, returns 0???" because I think it not about
                      sending a mouse click that this subject was in the beginning... :)

                      Yours, Jonas



                      Comment

                      Working...