0
if event.key == K_s:
    if event.mod & KMOD_SHIFT:
        player.move_ip((+5, 0))
    else:
        player.move_ip((+1, 0))

does the following code work the same way?

if event.key == K_s:
    if event.mod == 1:
        player.move_ip((+5, 0))
    else:
        player.move_ip((+1, 0))

i mean is it a good way to understand if i'm pressing the shift key?

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • Is there someone who can help me? it's urgent – User9dazzi91 Dec 03 '20 at 13:55
  • 3
    No, it doesn't work the same way. Even if `KMOD_SHIFT` had a value of 1 (I have no idea if that's true, and even if its true in some case it's certainly not *guaranteed* to always be true), testing a bit via bitwise AND is not the same as testing equality of the entire `mod` value. Why do you think you need to make this change? – jasonharper Dec 03 '20 at 13:57
  • because i can't understand bitwise operators and i don't think i will ever use them so i would like to find another method to understand if i am pressing the s key together with the shift key – User9dazzi91 Dec 03 '20 at 14:06
  • 1
    To your question - no you can't use this method instead. Assuming `event.mod = 5 ` and `KMOD_SHIFT = 1` then doing `event.mod & KMOD_SHIFT` will indeed be true, while of course `event.mod != KMOD_SHIFT` – Tomerikoo Dec 03 '20 at 14:12
  • I didn't quite understand your last answer..i don't understand much english and that doesn't help me anyway..he says: you can use either the unicode (if strictly printable characters) or mod attribute to tell the difference.why doesn't my method work? a practical example? – User9dazzi91 Dec 03 '20 at 14:27
  • Not to put too blunt a point on it, but you will need to learn to understand bitwise operations and masking to do this correctly. Any sort of attempts to avoid them in this situation (or others) will simply produce fragile, sloppy code. Full stop. – g.d.d.c Dec 03 '20 at 14:35
  • *"because i can't understand bitwise operators and i don't think i will ever use them [...]"* - There is no other method. In a computer, the values and numbers are stored in bits, which are usually organized in bytes. You should not write a program unless you have a basic understanding of binary operations (AND, OR, NOT). – Rabbid76 Dec 03 '20 at 15:43
  • I think the source of the confusion is actually that @User9dazzi91 understands AND, OR, NOT perfectly, but does not know what their bitwise counterparts are, which is perfectly plausible for a beginner python user. Instead of suggesting him not to write programs, I'd redirect him to an easy explanation with examples, such as https://www.tutorialspoint.com/python/bitwise_operators_example.htm – harisont Dec 09 '20 at 12:13

0 Answers0