This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2007-08-29 13:36:33

EdnaC
Scratcher
Registered: 2007-08-28
Posts: 100+

Support for Sensing the "Any" key

If you want to monitor multiple keys on the keyboard to see if sprites should move or be turned (or whatever), you have a couple of options. 

1)For a given sprite, you could have a forever loop that contains "If" <KeyPressed> statements to check the keys that you care about.

2)You could have multiple scripts that start with "Wait Until" <KeyPressed> that would check each key separately and would set a variable or send a broadcast.

How about:
Sensing for "Any Key" would be a nice way to trigger looking at several keys in one script.  The script could start with "Wait Until" <Any Key Pressed>, then go on to find out which one(s).

Offline

 

#2 2007-08-29 15:34:20

beny
Scratcher
Registered: 2007-07-24
Posts: 100+

Re: Support for Sensing the "Any" key

i don't really get what you mean by that


more people like the letter B than the letter A!
i'm going for 250 posts!

Offline

 

#3 2007-08-29 16:48:29

Sawman3
Scratcher
Registered: 2007-07-19
Posts: 97

Re: Support for Sensing the "Any" key

Interesting idea...I think it could work well, although it would also need a way of ckecking for only previously referred to keys, so that if keys were accidentally pressed, it wouldn't scan for them too and thus slow down the game interface, if that makes sense... big_smile


"These are the old days, the glory days...they're back."

Offline

 

#4 2007-08-31 02:12:28

kevin_karplus
Scratcher
Registered: 2007-04-27
Posts: 1000+

Re: Support for Sensing the "Any" key

If you do a wait until any-key, then you need some way of recording which key that was for subsequent tests, since the key might no longer be pressed by the time the test is made.  This complicates scripts, rather than simplifying them.

Offline

 

#5 2007-08-31 08:57:58

EdnaC
Scratcher
Registered: 2007-08-28
Posts: 100+

Re: Support for Sensing the "Any" key

The key wouldn't go "unpressed" quicker than the program could catch it unless the script had wait statements or was really, really  long.  You could verify this if in doubt, by setting up a Forever If < "n" Keypressed> followed by an If <"n" Keypressed> (Show something), Else (Show some other thing) statement.  See if you can press your chosen key and let go so quickly that you don't get "caught". 

In any case, what I originally described was a way to avoid constantly checking keys if nothing was being pressed.  The thought was for something like a game control function where the arrow keys might be used to turn a character, in this case, the user would be holding the key to execute a turn.

In addition to the use originally described, I can also imagine using the proposed function to ask the user for input:

Press "1" (player) or "2" (player)

If they press any other key, you could now briefly display an error message: "Please Select 1 or 2 player".  This would be a nice "professional" touch.

The function would also be nice for the old "Press Any Key to Continue" thing.

Offline

 

#6 2007-08-31 20:31:24

kevin_karplus
Scratcher
Registered: 2007-04-27
Posts: 1000+

Re: Support for Sensing the "Any" key

If you have to compare the pressed key against several choices and have several other scripts running, it is actually fairly easy for a scheduling break to occur between the key detection and seeing what key was pressed.  This sor tof timing error is very difficult to debug, and scratch should not be set up to make such race conditions common.

Offline

 

Board footer