Capturing mouse events

Just starting out? Need help? Post your questions and find answers here.
fenyctis
User
User
Posts: 21
Joined: Sat Sep 10, 2005 9:08 pm

Capturing mouse events

Post by fenyctis »

I would like to capture mouse events such as clicking in general, not only inside my window. I have tried the API command GetAsyncKeyState(2) (rightmouse down) but it would only work inside my own window as well.
Thanks in advance!

fenyctis
Jellybean
User
User
Posts: 95
Joined: Wed Aug 24, 2005 7:33 pm

Post by Jellybean »

Let me have a guess: You're checking it inside an event loop using WaitWindowEvent()?
fenyctis
User
User
Posts: 21
Joined: Sat Sep 10, 2005 9:08 pm

Post by fenyctis »

Err.. yes. Sounds like that's wrong. :P Should I make a thread?
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

I'd avoid threads if you don't need them. All you need to really do here is drop the wait from your windowevent check.

Code: Select all

.
.
    Event = WindowEvent() ; not WaitWindowEvent() !
    If Event
       TurnPurple
    Else
       If GetAsyncKeyState(#PB_VerySpecificKey) ;numbers alone are less readable later
          BlinkTwiceRapidly
          ClearYourThroatLoudly
       Else
          Delay (10)
       Endif
    Endif

    ; Note: Some syntax in this program will not be available until PB 4.x
      
BERESHEIT
fenyctis
User
User
Posts: 21
Joined: Sat Sep 10, 2005 9:08 pm

Post by fenyctis »

I seem to have got it to work now. Thanks a bundle. But now I've got a 50% usage of my program. Is this because of the GetAsyncKeyState? Is there a way to prevent this?
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

GetAsyncKeyState takes nanoseconds. Just make sure that if you don't have an event of some description to respond to that you put a decent-size delay in the code. 10 should be about right.
BERESHEIT
fenyctis
User
User
Posts: 21
Joined: Sat Sep 10, 2005 9:08 pm

Post by fenyctis »

Code: Select all

    eid.l = WindowEvent()
    If eid
      If eid = #PB_Event_CLosewindow
          quit = 1
      EndIf
    Else

      If GetAsyncKeyState_(#VK_LBUTTON)
          leftmousepressed = 1
      Else
        If leftmousepressed = 1
          TriggerMouseReleaseAndBounceAround
          leftmousepressed = 0
        EndIf
      EndIf

    EndIf
fenyctis
User
User
Posts: 21
Joined: Sat Sep 10, 2005 9:08 pm

Post by fenyctis »

Okay.
--
Got it working, thanks a bundle.
Post Reply