1

I'm helping someone with a project on Scratch, and it has a block <if _key_ is pressed>, and I can tell if a key is pressed, (a, b, c, ...) or when ANY key is pressed.

Using these booleans, any and (a, b, c ...), can I tell if one and only one key is pressed without checking every single key individually? (e.g. a and ONLY a is pressed)

Note: I can only use logical operators, not variables or other things.

Davedude
  • 160
  • 10

2 Answers2

1

All I can think of is if you put if key a pressed not key a pressed and key b pressed or key a pressed and key c pressed etc., then it could eventually work. The only problem is what if you pressed key a, b, and c, what would happen. I guess though you are pressing a and b as well as the c so it wouldn't work. You know what? The other answer above is better. Ignore me.

SmartBaby
  • 11
  • 4
0

Well it would be very hard to find a short piece of code to do it without using variables...
Your best shot if you don't want to do without variables is to make sure if a key is pressed, the other keys are not as shown below...

When GF clicked
if < <key [a] pressed> and <not <key [b] pressed> >
do stuff
if < <key [b] pressed> and <not <key [a] pressed> >
do stuff

But this is only for keys a and b, so if you want to do it for all 26 alphabetical letters, plus 10 numerals and a space bar, it would be very impractical. I'm not saying it is impossible, it is just very lengthy.

If you want to use variables, make a variable called keysPressed which would be used as a counter of number of keys pressed. You would need to make 38 seperate simple scripts as shown below...
When GF clicked
forever
if <key [a] pressed
change keysPressed by (1)
wait until <not <key [a] pressed> >
change keysPressed by (-1)

Then you would need a seperate script after the 38 scripts of mostly copy-paste:

When GF Clicked
Forever
if <keysPressed = (1)>
do stuff

I'm not too sure what you want to do, so I just labelled your code after you pressed only 1 key as do stuff

Conclusion? Unless you are the impractical dude, use variables!