Im trying to learn python but struggling on this problem. I will be thankfull if anyone give me some help.
while button1_pressed and button2_pressed: #no matter which one is pressed first to me
do some
while button3_pressed: #only if it is pressed after button1 and button2 are pressed
do some2
if button2_is_not_pressed:
break
in practice "do some2" activates even when I press button 3 before buttons 1 and 2. Anyone can help me, please?
button1_pressed
and button2_pressed
must both be True before you press button 3.
(May-31-2020, 03:00 PM)Yoriz Wrote: [ -> ]button1_pressed
and button2_pressed
must both be True before you press button 3.
Thank you for your answer but i do not fully understand. English isnt my first language.
The
button1_pressed
,
button2_pressed
and
button3_pressed
are functions. I forgot to mention that.
def button1_pressed(): # Returns true if the left mouse button is pressed
button1_state = win32api.GetKeyState(0x01)
return button1_state < 0
def button2_pressed(): # Returns true if the right mouse button is pressed
button2_state = win32api.GetKeyState(0x02)
return button2_state < 0
def button3_pressed(): # Returns true if the M button is pressed
button3_state = win32api.GetKeyState(0x4D)
return button3_state < 0
while button1_pressed() and button2_pressed(): # No matter which one is pressed first to me
do some
while button3_pressed(): # Only if it is pressed after button1 and button2 are pressed
do some2
if button2_pressed() == 0:
break
The problem is: if I press
button3_pressed
even before
button1_pressed
and
button2_pressed
do some2 activate. I do not want this..
button1_pressed = True
button2_pressed = True
button3_pressed = True
button2_is_not_pressed = True
while button1_pressed and button2_pressed:
print('do some') # this only happens if both the above are True
while button3_pressed:
print('do some2') # theis only happens if button3_pressed is True
if button2_is_not_pressed:
break # this only breaks the inner while loop
break # added an extra break else it would just be stuck in an endless outer loop
Output:
do some
do some2
The following conditions nothing will happen
button1_pressed = False
button2_pressed = True
button3_pressed = True
button2_is_not_pressed = True
while button1_pressed and button2_pressed:
print('do some') # this only happens if both the above are True
while button3_pressed:
print('do some2') # theis only happens if button3_pressed is True
if button2_is_not_pressed:
break # this only breaks the inner while loop
break # added an extra break else it would just be stuck in an endless loop
Output:
button1_pressed
and
button2_pressed
must be returning True even though you think they should not be.
(May-31-2020, 03:35 PM)Yoriz Wrote: [ -> ]button1_pressed = True
button2_pressed = True
button3_pressed = True
button2_is_not_pressed = True
while button1_pressed and button2_pressed:
print('do some') # this only happens if both the above are True
while button3_pressed:
print('do some2') # theis only happens if button3_pressed is True
if button2_is_not_pressed:
break # this only breaks the inner while loop
break # added an extra break else it would just be stuck in an endless outer loop
Output:
do some
do some2
The following conditions nothing will happen
button1_pressed = False
button2_pressed = True
button3_pressed = True
button2_is_not_pressed = True
while button1_pressed and button2_pressed:
print('do some') # this only happens if both the above are True
while button3_pressed:
print('do some2') # theis only happens if button3_pressed is True
if button2_is_not_pressed:
break # this only breaks the inner while loop
break # added an extra break else it would just be stuck in an endless loop
Output:
button1_pressed
and button2_pressed
must be returning True even though you think they should not be.
I really apreciate your help... but imagine this situation:
1 - user press
button1
and
button2
then press
button3
Its ok to me "do some 2"
2 - user press
button2
and
button1
then press
button3
Its ok to me "do some 2"
but
3 - user press
button3
before
button1
and
button2
Its not ok to "do some2", I need just to "do some"
Mate, I should have mentioned that this whole code is inside a Main loop. Im sorry!!
So.. I Thinks to test should be like this:
button1_pressed = True
button2_pressed = True
button3_pressed = True
button2_is_not_pressed = True
while True:
while button1_pressed and button2_pressed:
print('do some') # this only happens if both the above are True
while button3_pressed:
print('do some2') # theis only happens if button3_pressed is True
if button2_is_not_pressed:
break # this only breaks the inner while loop
break # added an extra break else it would just be stuck in an endless outer loop
In your function, you get the state of the button press. It can be an integer between 1 and 0 (pressed or not pressed).
When you return it, to get a True or False output you check if it is less than 0.
When the key
isn't pressed, button_state is going to be 0:
0 < 0 = True.
Meaning when the button isn't pressed the while loop can still run. Surely you would want something like this:
return bool(button1_state)
Since bool(0) returns False, and bool(1) returns True.
Am I missing something here?
(May-31-2020, 04:22 PM)DreamingInsanity Wrote: [ -> ]In your function, you get the state of the button press. It can be an integer between 1 and 0 (pressed or not pressed).
When you return it, to get a True or False output you check if it is less than 0.
When the key isn't pressed, button_state is going to be 0:
0 < 0 = True.
Meaning when the button isn't pressed the while loop can still run. Surely you would want something like this:
return bool(button1_state)
Since bool(0) returns False, and bool(1) returns True.
Am I missing something here?
Thank you for your answer.
I made some mistakes editing the posts..
Could you give a look a it:
def button1_pressed(): # Returns true if the left mouse button is pressed
button1_state = win32api.GetKeyState(0x01)
return button1_state < 0
def button2_pressed(): # Returns true if the right mouse button is pressed
button2_state = win32api.GetKeyState(0x02)
return button2_state < 0
def button3_pressed(): # Returns true if the M button is pressed
button3_state = win32api.GetKeyState(0x4D)
return button3_state < 0
while True:
while button1_pressed() and button2_pressed(): # No matter which one is pressed first to me
print("do some")
while button3_pressed(): # Only if it is pressed after button1 and button2 are pressed
print("do some2")
if button2_pressed() == 0:
break # this only breaks the inner while loop
break # added an extra break else it would just be stuck in an endless loop
I forgot to mention this whole code is inside a main loop.
@
penahuse
Try this:
def button1_pressed(): # Returns true if the left mouse button is pressed
button1_state = win32api.GetKeyState(0x01)
return bool(button1_state)
def button2_pressed(): # Returns true if the right mouse button is pressed
button2_state = win32api.GetKeyState(0x02)
return bool(button2_state)
def button3_pressed(): # Returns true if the M button is pressed
button3_state = win32api.GetKeyState(0x4D)
return bool(button3_state)
while True:
while button1_pressed() and button2_pressed(): # No matter which one is pressed first to me
print("do some")
while button3_pressed(): # Only if it is pressed after button1 and button2 are pressed
print("do some2")
if button2_pressed() == 0:
break # this only breaks the inner while loop
break # added an extra break else it would just be stuck in an endless loop
(May-31-2020, 04:52 PM)DreamingInsanity Wrote: [ -> ]@penahuse
Try this:
def button1_pressed(): # Returns true if the left mouse button is pressed
button1_state = win32api.GetKeyState(0x01)
return bool(button1_state)
def button2_pressed(): # Returns true if the right mouse button is pressed
button2_state = win32api.GetKeyState(0x02)
return bool(button2_state)
def button3_pressed(): # Returns true if the M button is pressed
button3_state = win32api.GetKeyState(0x4D)
return bool(button3_state)
while True:
while button1_pressed() and button2_pressed(): # No matter which one is pressed first to me
print("do some")
while button3_pressed(): # Only if it is pressed after button1 and button2 are pressed
print("do some2")
if button2_pressed() == 0:
break # this only breaks the inner while loop
break # added an extra break else it would just be stuck in an endless loop
Did not work well. Printed infinite "do some" until I click again any mouse button again (to return False) but I need it to work only if the buttons are pressed at same time.
I apreciate your try anyway!
I did some testing. If you use '
GetAsyncKeyState' it should work. 'GetAsyncKeyState' is basically 'isKeyPressed' so the moment you let go of a button, it will no longer return true.
def button1_pressed(): # Returns true if the left mouse button is pressed
button1_state = win32api.GetAsyncKeyState(0x01)
return bool(button1_state)
def button2_pressed(): # Returns true if the right mouse button is pressed
button2_state = win32api.GetAsyncKeyState(0x02)
return bool(button2_state)
def button3_pressed(): # Returns true if the M button is pressed
button3_state = win32api.GetAsyncKeyState(0x4D)
return bool(button3_state)
while True:
while button1_pressed() and button2_pressed(): # No matter which one is pressed first to me
print("do some")
while button3_pressed(): # Only if it is pressed after button1 and button2 are pressed
print("do some2")
if button2_pressed() == 0:
break # this only breaks the inner while loop
break # added an extra break else it would just be stuck in an endless loop