WindowMouseButton - (Windows/Linux)

Share your advanced PureBasic knowledge/code with the community.
DarkDragon
Addict
Addict
Posts: 2344
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

WindowMouseButton - (Windows/Linux)

Post 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 ;) .
bye,
Daniel
Straker
Enthusiast
Enthusiast
Posts: 701
Joined: Wed Apr 13, 2005 10:45 pm
Location: Idaho, USA

Post by Straker »

Nice work and Thank you!

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

Thanks again!
:D
DarkDragon
Addict
Addict
Posts: 2344
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Post by DarkDragon »

:wink: Thanks for testing.
bye,
Daniel
josku_x
Addict
Addict
Posts: 997
Joined: Sat Sep 24, 2005 2:08 pm

Post 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 )
dracflamloc
Addict
Addict
Posts: 1648
Joined: Mon Sep 20, 2004 3:52 pm
Contact:

Post by dracflamloc »

Cool one DarkDragon
Polo
Addict
Addict
Posts: 2422
Joined: Tue May 06, 2003 5:07 pm
Location: UK

Post 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 ?
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

Polo wrote: Where's the middle button ?
between the other two? at least, there was mine last time i looked...
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
thefool
Always Here
Always Here
Posts: 5875
Joined: Sat Aug 30, 2003 5:58 pm
Location: Denmark

Post 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?
DarkDragon
Addict
Addict
Posts: 2344
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Post by DarkDragon »

I don't know what a trackball is. Well, maybe it works, haven't tried it.

Thanks for testing.
bye,
Daniel
thefool
Always Here
Always Here
Posts: 5875
Joined: Sat Aug 30, 2003 5:58 pm
Location: Denmark

Post by thefool »

DarkDragon wrote:I don't know what a trackball is.
Thanks for testing.
haha! are you stupid???
DarkDragon
Addict
Addict
Posts: 2344
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Post 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().
Last edited by DarkDragon on Sat Dec 08, 2007 9:56 am, edited 1 time in total.
bye,
Daniel
User avatar
DoubleDutch
Addict
Addict
Posts: 3220
Joined: Thu Aug 07, 2003 7:01 pm
Location: United Kingdom
Contact:

Post 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
https://deluxepixel.com <- My Business website
https://reportcomplete.com <- School end of term reports system
Post Reply