Page 1 of 1

WindowMouseButton - (Windows/Linux)

Posted: Mon Nov 28, 2005 4:38 pm
by DarkDragon
Code updated For 5.20+

Hello,

I've done something for you:

Code: Select all

Procedure WindowMouseButton(Wnd, ButtonNr)
  
  CompilerIf #PB_Compiler_OS = #PB_OS_Linux
    ;Linux Version
    
    Protected gdkWnd.l, x.l, y.l, mask.l
    
    If Wnd
      *Window.GTKWindow = Wnd
      gdkWnd = *Window\bin\child\window
      gdk_window_get_pointer_(gdkWnd, @x, @y, @mask)
      
      Select ButtonNr
        Case 0
          If (mask & #GDK_BUTTON1_MASK)
            ProcedureReturn 1
          EndIf
        Case 1
          If (mask & #GDK_BUTTON3_MASK)
            ProcedureReturn 1
          EndIf
        Case 2
          If (mask & #GDK_BUTTON2_MASK)
            ProcedureReturn 1
          EndIf
      EndSelect
    EndIf
    
  CompilerElse
    ;Windows Version
    
    If Wnd And GetForegroundWindow_() = Wnd
      Select ButtonNr
        Case 0
          If GetAsyncKeyState_(#VK_LBUTTON) > 0
            ProcedureReturn 1
          EndIf
        Case 1
          If GetAsyncKeyState_(#VK_RBUTTON) > 0
            ProcedureReturn 1
          EndIf
        Case 2
          If GetAsyncKeyState_(#VK_MBUTTON) > 0
            ProcedureReturn 1
          EndIf
      EndSelect
    EndIf
    
  CompilerEndIf
  
  ProcedureReturn 0
EndProcedure

;-ExampleCode:

OpenWindow(0, 0, 0, 300, 200, "Test", #PB_Window_SystemMenu)

hWnd = WindowID(0)
If hWnd <> 0
  
  
  TextGadget(0, 10, 10, 280, 20, "Status")
  
  Repeat
    Event = WindowEvent()
    
    If WindowMouseButton(hWnd, 0)
      SetGadgetText(0, "Left MouseButton pressed"  )
    ElseIf WindowMouseButton(hWnd, 1)
      SetGadgetText(0, "Right MouseButton pressed" )
    ElseIf WindowMouseButton(hWnd, 2)
      SetGadgetText(0, "Middle MouseButton pressed")
    EndIf
    
    Delay(15)
  Until Event = #PB_Event_CloseWindow
EndIf
End
Should work on Windows/Linux.

[EDIT]
Feedback please ;) .

Posted: Tue Nov 29, 2005 9:37 pm
by Straker
Nice work and Thank you!

Yes, works fine on Windows 2000 and Ubuntu Linux 5.04 (Hoary).

Thanks again!
:D

Posted: Wed Nov 30, 2005 6:31 pm
by DarkDragon
:wink: Thanks for testing.

Posted: Wed Nov 30, 2005 6:34 pm
by josku_x
works perfect here on these machines:

Windows Server 2003
Windows XP Professional

Red Hat Linux
Mandrake Linux


(PS: I own only the windows systems, the others are my office's :D )

Posted: Wed Nov 30, 2005 7:51 pm
by dracflamloc
Cool one DarkDragon

Posted: Wed Nov 30, 2005 9:38 pm
by Polo
Sounds good, even if I think that doing the same with events is good too.
BTW, why isn't there support for the middle button of the mouse in the events ?
I see in the help that :

Code: Select all

  #PB_EventType_LeftClick       : Left mouse button click
  #PB_EventType_RightClick      : right mouse button click
  #PB_EventType_LeftDoubleClick : Left mouse button double click
  #PB_EventType_RightDoubleClick: Right mouse button double click
Where's the middle button ?

Posted: Wed Nov 30, 2005 11:15 pm
by blueznl
Polo wrote: Where's the middle button ?
between the other two? at least, there was mine last time i looked...

Posted: Wed Nov 30, 2005 11:17 pm
by thefool
Does this work with a trackball too? Well it isnt exactly a mouse!!
Where are the left mouse button..? Is it the one most far away from the keyboard?

Posted: Fri Dec 02, 2005 6:19 am
by DarkDragon
I don't know what a trackball is. Well, maybe it works, haven't tried it.

Thanks for testing.

Posted: Fri Dec 02, 2005 4:59 pm
by thefool
DarkDragon wrote:I don't know what a trackball is.
Thanks for testing.
haha! are you stupid???

Posted: Fri Dec 02, 2005 6:12 pm
by DarkDragon
thefool wrote:
DarkDragon wrote:I don't know what a trackball is.
Thanks for testing.
haha! are you stupid???
Yes. Why are you asking me about that? You know it!

[EDIT]
For having swapped mouse buttons you could use GetKeyState() instead of GetAsyncKeyState().

Posted: Fri Dec 02, 2005 10:59 pm
by DoubleDutch
Don't forget to check for swapped mouse buttons in the windows version, this is what I use:

Code: Select all

Procedure IsAltPressed()
	ProcedureReturn GetAsyncKeyState_(#VK_MENU)&$8000
EndProcedure

Procedure IsShiftPressed()
	ProcedureReturn GetAsyncKeyState_(#VK_SHIFT)&$8000
EndProcedure

Procedure IsCtrlPressed()
	ProcedureReturn GetAsyncKeyState_(#VK_CONTROL)&$8000
EndProcedure

Procedure IsLeftPressed()
	result=GetAsyncKeyState_(#VK_LBUTTON)&$8000
	If GetSystemMetrics_(#SM_SWAPBUTTON)
		If result
			result=#False
		Else
			result=$8000
		EndIf
	EndIf
	ProcedureReturn result
EndProcedure

Procedure IsRightPressed()
	result=GetAsyncKeyState_(#VK_RBUTTON)&$8000
	If GetSystemMetrics_(#SM_SWAPBUTTON)
		If result
			result=#False
		Else
			result=$8000
		EndIf
	EndIf
	ProcedureReturn result
EndProcedure

Procedure IsMiddlePressed()
	ProcedureReturn GetAsyncKeyState_(#VK_MBUTTON)&$8000
EndProcedure
[EDIT]

Just looked again the the IsLeftPressed() and IsRightPressed() and realised how much better this would be:

Code: Select all

Procedure IsLeftPressed()
	If GetSystemMetrics_(#SM_SWAPBUTTON)
		key=#VK_RBUTTON
	Else
		key=#VK_LBUTTON
	EndIf
	ProcedureReturn GetAsyncKeyState_(key)&$8000
EndProcedure

Procedure IsRightPressed()
	If GetSystemMetrics_(#SM_SWAPBUTTON)
		key=#VK_LBUTTON
	Else
		key=#VK_RBUTTON
	EndIf
	ProcedureReturn GetAsyncKeyState_(key)&$8000
EndProcedure
:D