LeftMouseButtonUP() windows

Share your advanced PureBasic knowledge/code with the community.
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

LeftMouseButtonUP() windows

Post by Rook Zimbabwe »

OK I have been beating my head against a wall for a simple API way to check for LEFT mouse button UP... RIGHT mouse button UP etc.

I didn't want to add a huge library of really well written procedures to my program.

I have just finished this and about 2 weeks reading and rereading the API references on the MOUSE... (I am now cross-eyed and devoutly insane BTW!)

My solution was simple.

Code: Select all

        If MouseButton(#PB_MouseButton_Left)
            LBUTSTATE = GetAsyncKeyState_(#VK_LBUTTON) ; this = 32768 if PRESSED 
            ; Debug "MOUSE STATE: "+Str(GetAsyncKeyState_(#VK_LBUTTON)) ; uncomment to see 
            If LBUTSTATE = -32767 ; LEFT mouse button UP = -32767 : )
                ; INSERT YOUR CODE HERE
            EndIf
           ; LBUTSTATE = 0 ; not needed as I found out
        EndIf
All in all it is pretty simple
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
User avatar
eddy
Addict
Addict
Posts: 1479
Joined: Mon May 26, 2003 3:07 pm
Location: Nantes

Post by eddy »

it could be usefull for my drag panelgadget tabs snippet :)
Imagewin10 x64 5.72 | IDE | PB plugin | Tools | Sprite | JSON | visual tool
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

All in all it is pretty simple
... and almost wrong.

when you use PB's "MouseButton" Function, you should not use additionally GetAsyncKeystate.

the way you use it causes a problem:
you only check for UP via API when you previously had a DOWN by PB's MouseButton.

if you want to use API:
check for Focus additionally, then you can use it on every Window, no screen needed.
(and you should be aware that GetAsyncKeystate always checks for physical buttons, a lefthander mouse will not be rocognized.)

If you want to use PB's MouseLib (what I would prefer for a Fullscreen Game) you can flag it with 4 values:

Code: Select all

#MButtonFree = 0
#MButtonDown = 1
#MButtonHold = 2
#MButtonUp   = 4

Define LeftMouseButtonState.l

;...


If MouseButton( #PB_MouseButton_Left )
  If LeftMouseButtonState = #MButtonDown Or LeftMouseButtonState = #MButtonHold
    LeftMouseButtonState = #MButtonHold
  Else
    LeftMouseButtonState = #MButtonDown
  EndIf 
Else
  If LeftMouseButtonState = #MButtonDown Or LeftMouseButtonState = #MButtonHold
    LeftMouseButtonState = #MButtonUp
  Else
    LeftMouseButtonState = #MButtonFree
  EndIf 
EndIf
oh... and have a nice day.
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Post by Rook Zimbabwe »

Kaeru... your IF/THEN logic loop has me almost cross eyed again... I think I see what you are getting at, but that is a bigger decision tree than mine...

I simply want to check keystate if the LEFT button is pressed... otherwise the button continues firing which can be annoying in some instances...

Which is WHY we need this to be built IN to PB 4.3! (HINT Fred! Timmo! Guys!?)
... and almost wrong.
Yep, but it werks! 8)
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

Under Windows you can detect mouse button events, there's one for up, for down, for each button...

But yes, a function that returns the current state of the mouse button would be nice and same some thinking :-)
( 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... )
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

MouseButton() uses DirectX(), its not for normal programs.

Code: Select all

OpenWindow(0, 0, 0, 512, 384, "", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
CreateGadgetList(WindowID(0))


Repeat
  Select WaitWindowEvent()
    Case #WM_LBUTTONUP
      Debug "Up!"
    Case #PB_Event_CloseWindow
      Break
  EndSelect
ForEver


User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

but that is a bigger decision tree than mine...
the SIZE of a decision tree does not matter.
if it is straightforward, if will evaluate fast, this is what counts.

sure you can use the Keystate API-functions, but then better use them alone and not mixed up.

and the return values of GetAsyncKeystate are not THAT simple:
the first click is -32767 in normal cases, -32768 if the click sets focus on the window, 1 if you click on the exit-button, etc.
and 32768 id HOLDING the button down, not the first click.
oh... and have a nice day.
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Post by Rook Zimbabwe »

Kaeru: but does the number of operations in a decision tree make a difference?

Maybe not since it is usually a simple 1 or 0 decision...

Hmmm...

Your code is more accurate!

BUT:

Why wasn't there mention of this: Case #WM_LBUTTONUP before?

I just need to get the coordinates of the mouse when the button is released! That would avoid the 1000 times a second update from my mouse to the OS!

:D
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

Rook Zimbabwe wrote:Why wasn't there mention of this: Case #WM_LBUTTONUP before?
as I see it, there will be NO eventhandling at all in a fullscreen-game, so it's impossible to use #WM_LBUTTONUP.

on the other Hand, on a "normal" Window there is no DX, no support for the MouseLib, so then the solution I posted will not work. (wich is why Trond posted this)

only on a windowedscreen you could access both, but then again you have to be careful when and how to mix them together.
oh... and have a nice day.
User avatar
tinman
PureBasic Expert
PureBasic Expert
Posts: 1102
Joined: Sat Apr 26, 2003 4:56 pm
Location: Level 5 of Robot Hell
Contact:

Post by tinman »

If you intend to use the #WM_LBUTTONUP method then you can get the co-ordinates like this:

Code: Select all

Case #WM_LBUTTONUP
  pos.l = GetMessagePos_()
  x.l = pos & $FFFF
  y.l = (pos >> 16) & $FFFF
  Debug Str(x) + " " + Str(y)
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

With position:

Code: Select all

OpenWindow(0, 0, 0, 512, 384, "", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
CreateGadgetList(WindowID(0))

Repeat
  Select WaitWindowEvent()
    Case #WM_LBUTTONUP
      Debug "Up at "
      Debug EventlParam() >> 16
      Debug EventlParam() << 16 >> 16
      Debug "---"
    Case #PB_Event_CloseWindow
      Break
  EndSelect
ForEver
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Post by Rook Zimbabwe »

Now here is the fun part... I have a windowed screen and controls outside the window... :D

But all of this is very interesting... maybe have to whip up a small PBI for both functions!

If one of you gurus of Mouse Coolness fells like chunking that together I won't stop you!

As an idea, you could add in all the other missing windows mouse features, including change the cursor and wait cursor etc. ;)

That way I coud be lazy and sit on my butt and not do anything! :mrgreen:
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
Post Reply