simulate mouse events?

Just starting out? Need help? Post your questions and find answers here.
lambor734
User
User
Posts: 40
Joined: Sun Dec 19, 2010 7:46 pm
Location: ovanaro

simulate mouse events?

Post by lambor734 »

Hi All,
How can API functions, we simulate mouse events? None of the commands without the PB.
Only api functions. :shock:
User avatar
greyhoundcode
Enthusiast
Enthusiast
Posts: 108
Joined: Sun Dec 30, 2007 7:24 pm

Re: simulate mouse events?

Post by greyhoundcode »

What sort of events do you wish to simulate and in what context?
C64
Enthusiast
Enthusiast
Posts: 151
Joined: Sat Dec 18, 2010 4:40 am

Re: simulate mouse events?

Post by C64 »

I don't know if you want to use API commands or not... but here:

Code: Select all

GetCursorPos_(mouse.POINT)
Debug mouse\x
Debug mouse\y

SetCursorPos_(x,y)

mouse_event_(#MOUSEEVENTF_LEFTDOWN,0,0,0,0)
mouse_event_(#MOUSEEVENTF_LEFTUP,0,0,0,0)

mouse_event_(#MOUSEEVENTF_RIGHTDOWN,0,0,0,0)
mouse_event_(#MOUSEEVENTF_RIGHTUP,0,0,0,0)

mouse_event_(#MOUSEEVENTF_MIDDLEDOWN,0,0,0,0)
mouse_event_(#MOUSEEVENTF_MIDDLEUP,0,0,0,0)
lambor734
User
User
Posts: 40
Joined: Sun Dec 19, 2010 7:46 pm
Location: ovanaro

Re: simulate mouse events?

Post by lambor734 »

For example, if I want to do the following program defining event, what to write?

Code: Select all

OpenWindow(0,0,0,800,600,"My",#PB_Window_MinimizeGadget|#PB_Window_ScreenCentered)

;For example, the closing event for the window.  (only Api Function)

or

ButtonGadget(33,450,140,60,90,"")

;Event for double-click on the button. (only Api Function)
;Ie when we double-click on the button , display message.
How are the objects in Pb by API event be defined? I need these functions. :wink:
lambor734
User
User
Posts: 40
Joined: Sun Dec 19, 2010 7:46 pm
Location: ovanaro

Re: simulate mouse events?

Post by lambor734 »

Who knows how the function WaitWindowEvent() have produced?
Whether these codes have been used?

Code: Select all

  While GetMessage_(msg.MSG, #Null, 0, 0 )
  TranslateMessage_(msg)
  DispatchMessage_(msg)
  Wend 
Please if you have information provided in this regard let me.
C64
Enthusiast
Enthusiast
Posts: 151
Joined: Sat Dec 18, 2010 4:40 am

Re: simulate mouse events?

Post by C64 »

What are you trying to do? Learn how PureBasic converts it native commands to API?
lambor734
User
User
Posts: 40
Joined: Sun Dec 19, 2010 7:46 pm
Location: ovanaro

Re: simulate mouse events?

Post by lambor734 »

I love to learn how the Pb, the job is done. :D
I asked the question whether hard? :shock:
Ie one can not give answers to these questions? :?
buddymatkona
Enthusiast
Enthusiast
Posts: 252
Joined: Mon Aug 16, 2010 4:29 am

Re: simulate mouse events?

Post by buddymatkona »

You can override the normal event data like this: (I only show the ClickType portion of the override here. You will also need sim override data for PBEventGadget, EventGadget, whatever your app uses.)

Code: Select all

SimEvent = #PB_EventType_LeftDoubleClick
Gosub MouseEvent
;
;
;
MouseEvent:
If Simevent <> 0 
  Event = Simevent
Else
  Event = WaitWindowEvent()
EndIf
       
       Select Event
       
         Case #PB_Event_Gadget
           Select EventGadget()
             Case 1 
               Select EventType()
                 Case #PB_EventType_LeftClick        : Debug "Click with left mouse button"
                 Case #PB_EventType_RightClick       : Debug "Click with right mouse button"
                 Case #PB_EventType_LeftDoubleClick  : Debug "Double-click with left mouse button"
                 Case #PB_EventType_RightDoubleClick : Debug "Double-click with right mouse button"
               EndSelect
           EndSelect
       
       EndSelect
Simevent = 0
Return
User avatar
Michael Vogel
Addict
Addict
Posts: 2666
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: simulate mouse events?

Post by Michael Vogel »

I am trying to send mouse events to an external windows program, not sure if it can be done "seemless" - without moving the mouse around and no matter if the window is in the background (and overlapped by other windows), etc.

Here's a sample code, I'd like to push button "1" and "2" of the left window by activating them by the right window. Button 2 works but with a lot of restrictions. The method for button 1 fails completely.

Are there any chances for improvements?

Code: Select all


Macro MakeLong(low,high)
	(((high&$FFFF)<<16) | (low&$FFFF))
EndMacro

Procedure Main()

	Protected x,y
	Protected point.Point
	Protected cursor.Point
	Protected IqHandle

	IqHandle=OpenWindow(0,100,100,200,200,"")

	ButtonGadget(1,20,20,40,20,"1")
	ButtonGadget(2,20,50,40,20,"2")

	OpenWindow(1,400,100,200,200,"Control")
	ButtonGadget(666,20,20,40,20,"Go 1")
	ButtonGadget(999,20,40,40,20,"Go 2")

	Repeat

		Select WaitWindowEvent()

		Case #PB_Event_Gadget
			Select EventGadget()
			Case 666
				PostMessage_(IqHandle,#WM_LBUTTONDOWN,0, MakeLong(25,25))
				PostMessage_(IqHandle,#WM_LBUTTONUP,0, MakeLong(25,25))

			Case 999
				If IsWindowVisible_(IqHandle)
					point\x=25
					point\y=50
					ClientToScreen_(IqHandle,point)

					GetCursorPos_(cursor)
					SetCursorPos_(point\x,point\y)
					mouse_event_(#MOUSEEVENTF_LEFTDOWN,0,0,0,0)
					mouse_event_(#MOUSEEVENTF_LEFTUP,0,0,0,0)
					SetCursorPos_(cursor\x,cursor\y)
				EndIf

			Case 1
				MessageRequester("","1")
			Case 2
				MessageRequester("","2")
			EndSelect

		Case #PB_Event_CloseWindow
			End

		EndSelect

	ForEver

EndProcedure

Main()
User avatar
Otrebor
Enthusiast
Enthusiast
Posts: 198
Joined: Mon Mar 17, 2014 1:42 pm
Location: São Paulo, Brasil
Contact:

Re: simulate mouse events?

Post by Otrebor »

Hi
I don't know if i understood correctly :wink:

Code: Select all

Global A,B$,IqHandle,BUTTON1,BUTTON2
Macro MakeLong(low,high)
   (((high&$FFFF)<<16) | (low&$FFFF))
EndMacro

Procedure EnumChildProc(hwnd, lParam)

  
  VALUE=AllocateMemory(128)
  ; For any hwnd, you can 'nest' calls to EnumChildWindows and to other EnumChildProc
  result = #True ; continue searching
  If GetWindowText_(hwnd, VALUE, 128)
    If PeekS(VALUE)=B$
      A=hwnd
      result = #False ; search succesful, stop callback, do whatever
    EndIf
  EndIf
  ProcedureReturn result
EndProcedure

Procedure gethandle()
  Shared statusbar
  ;automatically get the handle 2 buttons
  HANDLE=IqHandle
  If HANDLE
    GetWindowThreadProcessId_(HANDLE, @ProcID_)
    Global hProc_ = OpenProcess_(#PROCESS_ALL_ACCESS, 0, ProcID_)
    B$="1"
    EnumChildWindows_(HANDLE, @EnumChildProc(), 0)
    BUTTON1=A
    B$="2"
    EnumChildWindows_(HANDLE, @EnumChildProc(), 0)
    BUTTON2=A
  EndIf
EndProcedure




Procedure Main()

   Protected x,y
   Protected point.Point
   Protected cursor.Point
   ;Protected IqHandle

   IqHandle=OpenWindow(0,100,100,200,200,"")

   ButtonGadget(1,20,20,40,20,"1")
   ButtonGadget(2,20,50,40,20,"2")

   OpenWindow(1,400,100,200,200,"Control")
   ButtonGadget(666,20,20,40,20,"Go 1")
   ButtonGadget(999,20,40,40,20,"Go 2")
   gethandle()
   Repeat

      Select WaitWindowEvent()

      Case #PB_Event_Gadget
         Select EventGadget()
         Case 666
            SendMessage_(BUTTON1,#WM_LBUTTONDOWN,0,0)
            SendMessage_(BUTTON1,#WM_LBUTTONUP,0,0)
            
            SendMessage_(BUTTON2,#WM_LBUTTONDOWN,0,0)
            SendMessage_(BUTTON2,#WM_LBUTTONUP,0,0)

         Case 999
            If IsWindowVisible_(IqHandle)
               point\x=25
               point\y=50
               ClientToScreen_(IqHandle,point)

               GetCursorPos_(cursor)
               SetCursorPos_(point\x,point\y)
               mouse_event_(#MOUSEEVENTF_LEFTDOWN,0,0,0,0)
               mouse_event_(#MOUSEEVENTF_LEFTUP,0,0,0,0)
               SetCursorPos_(cursor\x,cursor\y)
            EndIf

         Case 1
            MessageRequester("","1")
         Case 2
            MessageRequester("","2")
         EndSelect

      Case #PB_Event_CloseWindow
         End

      EndSelect

   ForEver

EndProcedure


Main()
User avatar
Michael Vogel
Addict
Addict
Posts: 2666
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: simulate mouse events?

Post by Michael Vogel »

Thanks Otrebor, but the problem for me is, that the target program (Garmin IQ Simulator) does not have specific buttons but only a bitmap where a mouseclick on different areas do some actions (so my example was not the best to explain what should be done :oops: ).
Id' like to control this software by keyboard shortcuts instead of moving around with the mouse. Not sure if there would be another aprroach than the one seen above...
Post Reply