getting bits from an integer (Mouse.State)

Post your Gambas programming questions here.
Post Reply
User avatar
BruceSteers
Posts: 1578
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

getting bits from an integer (Mouse.State)

Post by BruceSteers »

Anyone know how to use the Mouse.State property?

From the wiki...

Mouse.State (gb.qt4)
Static Property Read State As Integer
Return the state of the mouse buttons when the mouse event was generated.
The state is an integer whose each bit represents the state of one button.
The bit 0 is set if the left button is pressed.
The bit 1 is set if the middle button is pressed.
The bit 2 is set if the right button is pressed.


But i have no idea how to inspect the bits.
I've tried using Lsl Lsr Shl Shr but it's all very confusing :(

Cheers
If at first you don't succeed , try doing something differently.
BruceS
vuott
Posts: 263
Joined: Wednesday 5th April 2017 6:07pm
Location: European Union

Re: getting bits from an integer (Mouse.State)

Post by vuott »

...by using Bin() function:
Public Sub Form_MouseDown()

  Print Bin(Mouse.State, 8)
  
End

https://gambaswiki.org/wiki/lang/bin
Europaeus sum !

Amare memorentes atque deflentes ad mortem silenter labimur.
User avatar
thatbruce
Posts: 168
Joined: Saturday 4th September 2021 11:29pm

Re: getting bits from an integer (Mouse.State)

Post by thatbruce »

Use the AND

If (Mouse.State AND 1) then the left button was pressed.
If (Mouse.State AND 2) then the middle button was pressed.
If (Mouse.State AND 4) then the right button was pressed.
If (Mouse.State AND 3) then the left and middle buttons were pressed.
If (Mouse.State AND 5) then the middle and right buttons were pressed.
If (Mouse.State AND 7) then all the buttons were pressed.
If (Mouse.State AND 0) then no button was pressed.

If you go any further then you must have one of those weirdo gaming mouses that look like ski bindings.

This is called "bit masking", its been around since Ada Lovelace was playing with dollies. I am quite staggered that you dont know it.
For the sake of simplicity lets use a four bit machine. It is capable of counting up to 15!
0000 = 0
0001 = 1
0010 = 2
...
1111 = 15
So the code
LDA @some memory address which is our state variable to be tested.
AND 2
tests if the 2nd bit from the right is set.

bit like tabulature really,
Have you ever noticed that software is never advertised using the adjective "spreadable".
User avatar
BruceSteers
Posts: 1578
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: getting bits from an integer (Mouse.State)

Post by BruceSteers »

Thank you guys ,
I was clueless there.

Good to know :)
If at first you don't succeed , try doing something differently.
BruceS
Post Reply