Left mouse click detection

Just starting out? Need help? Post your questions and find answers here.
User avatar
Distorted Pixel
Enthusiast
Enthusiast
Posts: 233
Joined: Sun Aug 29, 2021 4:34 am

Left mouse click detection

Post by Distorted Pixel »

Hi,

I have been trying to get my program to detect a left mouse click within a window timer of 4000. I'm not sure if the timer is working, but I'll figure that out later. Currently the way I have my code, it seems like it thinks the left mouse button has already been clicked and moves on in the program. I don't want it to move on unless the left mouse button has been clicked before the timer is finished.

Code: Select all

; Splash Screen Include
UsePNGImageDecoder()

Procedure MouseClick()
  MClick = Event
  Select MClick
    Case 0
      If MouseButton(#PB_MouseButton_Left) And EventTimer() < 4000
        FreeImage(ImgFile)
        FreeGadget(0)
      EndIf  
    Case 1
      End
  EndSelect
EndProcedure

Procedure SplashScreen()
  
  InitSprite()
  InitMouse()
  
      OpenWindow(0, 0, 0, 1024, 576, "Ultimate Football Manager", #PB_Window_ScreenCentered)
        OpenScreen(1024, 576, 32, "Ultimate Football Manager", 0, 60)
          CanvasGadget(0, 0, 0, 1024, 576)
        ImgFile = LoadImage(#PB_Any, "C:\Users\Brian\Documents\Pure Basic Projects\Ultimate Football Manager\Media\UFM1.png")
      SetGadgetAttribute(0, #PB_EventType_LeftClick | #PB_Canvas_Image, ImageID(ImgFile))
              
       AddWindowTimer(0, 1, 4000)
  
       BindEvent(#PB_EventType_LeftClick, @MouseClick(),0)
       
  Repeat
    
    Event = WaitWindowEvent()
    ExamineMouse()
    MouseClick()
    
  Until Event = #PB_EventType_LeftClick
     
EndProcedure
To be popular is way to much work. I just want to be me, myself and I. Oh no, does that mean I'm bipolar? :shock:

No one cares how much you know until they know how much you care
User avatar
jacdelad
Addict
Addict
Posts: 1432
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: Left mouse click detection

Post by jacdelad »

EventTimer() Returns the timer that was triggered, not a timecode or something.
You can't process the timer and the click in one procedure, each creates its own event.
Leave out ExamineMouse().
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
User avatar
Distorted Pixel
Enthusiast
Enthusiast
Posts: 233
Joined: Sun Aug 29, 2021 4:34 am

Re: Left mouse click detection

Post by Distorted Pixel »

jacdelad wrote: Sun Jan 23, 2022 9:57 pm EventTimer() Returns the timer that was triggered, not a timecode or something.
You can't process the timer and the click in one procedure, each creates its own event.
Leave out ExamineMouse().
I have taken out the Examine() command and the EventTimer() check in the MouseClick() procedure. It still doesn't wait for a left mouse click, it just opens the window and screen and immediately moves on to the next #IncludeFile after the window disappears. Even with it being a If/Endif statement in the MouseClick() procedure. No matter what I have tried it just acts like it is already pressed and moves on to the next #IncludeFile. This is the last code I have tried and still the same.

Code: Select all

; Splash Screen Include
UsePNGImageDecoder()
Procedure MouseClick()
  ;MClick = Event
  Select MClick
    Case 0
      MouseButton(#PB_MouseButton_Left)              
    Case 1
      End
  EndSelect
EndProcedure

Procedure SplashScreen()
  
  InitSprite()
  InitMouse()
  
      OpenWindow(0, 0, 0, 1024, 576, "Ultimate Football Manager", #PB_Window_ScreenCentered)
        OpenScreen(1024, 576, 32, "Ultimate Football Manager", 0, 60)
          CanvasGadget(0, 0, 0, 1024, 576)
        ImgFile = LoadImage(#PB_Any, "C:\Users\Brian\Documents\Pure Basic Projects\Ultimate Football Manager\Media\UFM1.png")
      SetGadgetAttribute(0, #PB_EventType_LeftClick | #PB_Canvas_Image, ImageID(ImgFile))
       
      AddWindowTimer(0, 1, 4000)
      
      BindEvent(#PB_EventType_LeftClick, @MouseClick(),0)
       
  Repeat
    
    Event = WaitWindowEvent()
    EventTimer = EventTimer()
    
    MouseClick()
    
  Until Event = #PB_EventType_LeftClick
     
EndProcedure
To be popular is way to much work. I just want to be me, myself and I. Oh no, does that mean I'm bipolar? :shock:

No one cares how much you know until they know how much you care
User avatar
Demivec
Addict
Addict
Posts: 4086
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Left mouse click detection

Post by Demivec »

Here are a few suggestions that may help.

Use EnableExplicit at the top of your code and deflate your variables.

There are several issues with your code. One is that you are using a screen and a window at the same time. Typically you would use only one of these or use a window and a windowed screen. An exception to this is to open a window for the user of things such as a window timer and use the screen for display and such but even this can be done with simply a window and a windowed screen.

Second, if you are using a canvas gadget, why open a screen? Just leave it the code for the screen.

Third, your MouseClick() procedure doesn't need to do anything except set a global variable to atonal the end of the splash screen display and/or close it.

Fourth, the attributes you set in the Canvas gadget shouldn't include #PB_EventType_LeftClick. You should only set one attribute at a time and that isn't even a canvas gadget attribute but is instead an event type used to process window events.

Fifth, one approach you can use to manage the needed events to close the splash screen is by first checking the Event() value. If it equals #PB_EventTimer and EventTimer() = 4000 you are done. The other possible condition check is if the Event() value is #PB_Event_Gadget and EventGadget() = 0 and if EvenaType() = #PB_EventType_LeftClick.
User avatar
Distorted Pixel
Enthusiast
Enthusiast
Posts: 233
Joined: Sun Aug 29, 2021 4:34 am

Re: Left mouse click detection

Post by Distorted Pixel »

Thanks Demivec, I will take some time to go through what you have said here and work on these things. I put some of the commands in there because every time I added commands like InitMouse() it needed InitSprite() before it to work among others that did the same thing. So that is how I accumulated some commands. I will post back on fail or success
To be popular is way to much work. I just want to be me, myself and I. Oh no, does that mean I'm bipolar? :shock:

No one cares how much you know until they know how much you care
User avatar
Demivec
Addict
Addict
Posts: 4086
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Left mouse click detection

Post by Demivec »

Distorted Pixel wrote: Mon Jan 24, 2022 12:21 am Thanks Demivec, I will take some time to go through what you have said here and work on these things. I put some of the commands in there because every time I added commands like InitMouse() it needed InitSprite() before it to work among others that did the same thing. So that is how I accumulated some commands. I will post back on fail or success
I apologize (:wink:) for doing this but I wrote up some sample code to demonstrate one way of doing things. This may save you some experimentation. This sample code uses just a window and Canvas Gadget.

Code: Select all

; Splash Screen Include
EnableExplicit

UsePNGImageDecoder()

;these enumerations are just to help make things more readable later
Enumeration gadgets
  #g_Canvas
EndEnumeration

Enumeration windows
  #w_Main
EndEnumeration

Enumeration timers
  #t_splashscreen
EndEnumeration


Define CloseSplashScreen = #False

;Triggered by Event() = #PB_Event_Gadget, EventGadget() = #g_Canvas, EventType() = #PB_EventType_LeftClick
Procedure MouseClick()
  Shared CloseSplashScreen
  CloseSplashScreen = #True
EndProcedure

;Triggered by Event() = #PB_Event_Timer,  EventTimer() = #t_splashscreen
Procedure SplashScreenTimeout()
  Shared CloseSplashScreen
  CloseSplashScreen = #True
EndProcedure

Procedure SplashScreen()
  Define ImgFile, Event
  Shared CloseSplashScreen
  
  OpenWindow(#w_Main, 0, 0, 1024, 576, "Ultimate Football Manager", #PB_Window_ScreenCentered)
  ;OpenScreen(1024, 576, 32, "Ultimate Football Manager", 0, 60)
  CanvasGadget(#g_Canvas, 0, 0, 1024, 576)
  ImgFile = LoadImage(#PB_Any, "C:\Users\Brian\Documents\Pure Basic Projects\Ultimate Football Manager\Media\UFM1.png"); "test UFM1.png"
  SetGadgetAttribute(#g_Canvas, #PB_Canvas_Image, ImageID(ImgFile))
  
  AddWindowTimer(#w_Main, #t_splashscreen, 4000)
  
  BindEvent(#PB_Event_Gadget, @MouseClick(), #w_Main, #g_Canvas, #PB_EventType_LeftClick)
  BindEvent(#PB_Event_Timer, @SplashScreenTimeout(), #w_Main, #t_splashscreen)
  
  Repeat
    Event = WaitWindowEvent()
  Until CloseSplashScreen = #True Or Event = #PB_Event_CloseWindow
  
  FreeImage(ImgFile)
  UnbindEvent(#PB_Event_Timer, @SplashScreenTimeout(), #w_Main, #t_splashscreen)
  UnbindEvent(#PB_Event_Gadget, @MouseClick(), #w_Main, #g_Canvas, #PB_EventType_LeftClick)
  CloseWindow(#w_Main)
EndProcedure
;-----------------------------------------------------------------
CompilerIf #PB_Compiler_IsMainFile
  SplashScreen()
  MessageRequester("Tada!", "All Done!")
CompilerEndIf

User avatar
Distorted Pixel
Enthusiast
Enthusiast
Posts: 233
Joined: Sun Aug 29, 2021 4:34 am

Re: Left mouse click detection

Post by Distorted Pixel »

I apologize (:wink:) for doing this but I wrote up some sample code to demonstrate one way of doing things. This may save you some experimentation. This sample code uses just a window and Canvas Gadget.
It's ok, yes, I am trying to learn by doing it myself, but getting some help after I have been stuck on this for over 2 weeks is nice.
I'll give your example a look after work today and work with it

Thank you
To be popular is way to much work. I just want to be me, myself and I. Oh no, does that mean I'm bipolar? :shock:

No one cares how much you know until they know how much you care
User avatar
jacdelad
Addict
Addict
Posts: 1432
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: Left mouse click detection

Post by jacdelad »

Distorted Pixel wrote: Sun Jan 23, 2022 10:45 pm
jacdelad wrote: Sun Jan 23, 2022 9:57 pm EventTimer() Returns the timer that was triggered, not a timecode or something.
You can't process the timer and the click in one procedure, each creates its own event.
Leave out ExamineMouse().
I have taken out the Examine() command and the EventTimer() check in the MouseClick() procedure. It still doesn't wait for a left mouse click, it just opens the window and screen and immediately moves on to the next #IncludeFile after the window disappears. Even with it being a If/Endif statement in the MouseClick() procedure. No matter what I have tried it just acts like it is already pressed and moves on to the next #IncludeFile. This is the last code I have tried and still the same.

Code: Select all

; Splash Screen Include
UsePNGImageDecoder()
Procedure MouseClick()
  ;MClick = Event
  Select MClick
    Case 0
      MouseButton(#PB_MouseButton_Left)              
    Case 1
      End
  EndSelect
EndProcedure

Procedure SplashScreen()
  
  InitSprite()
  InitMouse()
  
      OpenWindow(0, 0, 0, 1024, 576, "Ultimate Football Manager", #PB_Window_ScreenCentered)
        OpenScreen(1024, 576, 32, "Ultimate Football Manager", 0, 60)
          CanvasGadget(0, 0, 0, 1024, 576)
        ImgFile = LoadImage(#PB_Any, "C:\Users\Brian\Documents\Pure Basic Projects\Ultimate Football Manager\Media\UFM1.png")
      SetGadgetAttribute(0, #PB_EventType_LeftClick | #PB_Canvas_Image, ImageID(ImgFile))
       
      AddWindowTimer(0, 1, 4000)
      
      BindEvent(#PB_EventType_LeftClick, @MouseClick(),0)
       
  Repeat
    
    Event = WaitWindowEvent()
    EventTimer = EventTimer()
    
    MouseClick()
    
  Until Event = #PB_EventType_LeftClick
     
EndProcedure
I'm sorry I can't work it out right now, my computer is in a package due to me moving and also I'm sick. But I'm sure others here will help you.
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
User avatar
Distorted Pixel
Enthusiast
Enthusiast
Posts: 233
Joined: Sun Aug 29, 2021 4:34 am

Re: Left mouse click detection

Post by Distorted Pixel »

In Demivec's example code as exactly as it was posted, it didn't work straight off. It kept telling me with EnableExplicit have to be declared: x.

[18:57:48] [COMPILER] Line 29: With 'EnableExplicit', variables have to be declared: x.
[18:59:00] [COMPILER] Line 29: With 'EnableExplicit', variables have to be declared: x.
[19:00:38] [COMPILER] Line 29: With 'EnableExplicit', variables have to be declared: x.
[19:01:30] [COMPILER] Line 29: With 'EnableExplicit', variables have to be declared: x.
[19:02:08] [COMPILER] Line 29: With 'EnableExplicit', variables have to be declared: x.
[19:02:32] [COMPILER] Line 29: With 'EnableExplicit', variables have to be declared: x.
[19:06:21] [COMPILER] Line 30: With 'EnableExplicit', variables have to be declared: x.

I'm not familar with certain commands in the code example, including "EnableExplicit". I still have a lot to learn. Sometimes I have to have a simplified explanation to comprehend something. I had to Rem out the EnableExplicit command and it worked. That is the only thing I had to do to get it to work
I will continue to study people's examples to learn and refer to the help files. The help files are even confusing sometimes, but sometimes I can find something online that explains things in simplified terms and I understand it better. I hope In a couple of years I'll be decently good in PureBasic. It has me addicted to it lol.

Code: Select all

; Splash Screen Include
;EnableExplicit

UsePNGImageDecoder()

;these enumerations are just to help make things more readable later
Enumeration gadgets
  #g_Canvas
EndEnumeration

Enumeration windows
  #w_Main
EndEnumeration

Enumeration timers
  #t_splashscreen
EndEnumeration


Define CloseSplashScreen = #False

;Triggered by Event() = #PB_Event_Gadget, EventGadget() = #g_Canvas, EventType() = #PB_EventType_LeftClick
Procedure MouseClick()
  Shared CloseSplashScreen
  CloseSplashScreen = #True
EndProcedure

;Triggered by Event() = #PB_Event_Timer,  EventTimer() = #t_splashscreen
Procedure SplashScreenTimeout()
  Shared CloseSplashScreen
  CloseSplashScreen = #True
EndProcedure

Procedure SplashScreen()
  Define ImgFile, Event
  Shared CloseSplashScreen
  
  OpenWindow(#w_Main, 0, 0, 1024, 576, "Ultimate Football Manager", #PB_Window_ScreenCentered)
  ;OpenScreen(1024, 576, 32, "Ultimate Football Manager", 0, 60)
  CanvasGadget(#g_Canvas, 0, 0, 1024, 576)
  ImgFile = LoadImage(#PB_Any, "C:\Users\Brian\Documents\Pure Basic Projects\Ultimate Football Manager\Media\UFM1.png"); "test UFM1.png"
  SetGadgetAttribute(#g_Canvas, #PB_Canvas_Image, ImageID(ImgFile))
  
  AddWindowTimer(#w_Main, #t_splashscreen, 4000)
  
  BindEvent(#PB_Event_Gadget, @MouseClick(), #w_Main, #g_Canvas, #PB_EventType_LeftClick)
  BindEvent(#PB_Event_Timer, @SplashScreenTimeout(), #w_Main, #t_splashscreen)
  
  Repeat
    Event = WaitWindowEvent()
  Until CloseSplashScreen = #True Or Event = #PB_Event_CloseWindow
  
  FreeImage(ImgFile)
  UnbindEvent(#PB_Event_Timer, @SplashScreenTimeout(), #w_Main, #t_splashscreen)
  UnbindEvent(#PB_Event_Gadget, @MouseClick(), #w_Main, #g_Canvas, #PB_EventType_LeftClick)
  CloseWindow(#w_Main)
EndProcedure
;-----------------------------------------------------------------
CompilerIf #PB_Compiler_IsMainFile
  SplashScreen()
  MessageRequester("Tada!", "All Done!")
CompilerEndIf
To be popular is way to much work. I just want to be me, myself and I. Oh no, does that mean I'm bipolar? :shock:

No one cares how much you know until they know how much you care
User avatar
Demivec
Addict
Addict
Posts: 4086
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Left mouse click detection

Post by Demivec »

Distorted Pixel wrote: Tue Jan 25, 2022 2:05 am In Demivec's example code as exactly as it was posted, it didn't work straight off. It kept telling me with EnableExplicit have to be declared: x.
Using EnableExplicit makes it necessary to declare a variable before it's used. Without this PureBasic will self-declare a variable when it is first used. If a typo is made then different variations of the same variable could result that are all unique variables. The variables will also be local to wherever they appear, whether in a procedure or the main scope.

What is strange regarding the errors you posted is that the only 'x' that appears anywhere in the code sample is in the word 'EnableExplicit'. Perhaps you typed a stray 'x' in either case the code passed testing with EnableExplicit and so should function equally well without it.

To explore variations I also did a version of test code using a Windowed Screen and it worked fine. I did a version with only a Screen but ran into a strange problem because it wouldn't display anything and I wasn't able to resolve the problem in the time before I posted. I'll investigate the issue further when I find some time.
User avatar
Distorted Pixel
Enthusiast
Enthusiast
Posts: 233
Joined: Sun Aug 29, 2021 4:34 am

Re: Left mouse click detection

Post by Distorted Pixel »

What is strange regarding the errors you posted is that the only 'x' that appears anywhere in the code sample is in the word 'EnableExplicit'. Perhaps you typed a stray 'x' in either case the code passed testing with EnableExplicit and so should function equally well without it.
Please forgive me, yes your code did work as you originally wrote it. I wasn't paying attention at what #IncludeFile the error was on. Your code was fine. The error happened on the ImgFile variable in the next #IncludeFile after SplashScreen.pb
To be popular is way to much work. I just want to be me, myself and I. Oh no, does that mean I'm bipolar? :shock:

No one cares how much you know until they know how much you care
Post Reply