Discuss Scratch

NeonG4
Scratcher
1000+ posts

How to detect what key is pressed?

How do I detect what key is pressed, when any key is pressed? (I know this is a repeated conversation, couldn't find old one though)
gdxfor
Scratcher
100+ posts

How to detect what key is pressed?

There's not much you can do apart from adding every single possible key to a list, then looping through them.
Jolte0n
Scratcher
48 posts

How to detect what key is pressed?

You mean like this?
when [Space] key pressed
say [hi]
NeonG4
Scratcher
1000+ posts

How to detect what key is pressed?

Jolte0n wrote:

You mean like this?
when [Space] key pressed
say [hi]
Sort of, but I don't want 36 different events.

ametz807 wrote:

There's not much you can do apart from adding every single possible key to a list, then looping through them.
I don't want to use data storage though, because my project is already at 2,400 blocks and 2 mb
NeonG4
Scratcher
1000+ posts

How to detect what key is pressed?

Bumping… (unsolved
abcde1234qwe
Scratcher
500+ posts

How to detect what key is pressed?

You'll need to create a list that stores all the keys that you want pressed. You'll also need to create a few variables.

(Keybindings :: list) // For this sprite only or for all sprites.
(index)//For this sprite only.

when [any v] key pressed
Check Key Pressed :: more blocks


//Without screen refresh.
define Check Key Pressed
set [index v] to [0]
repeat (length of [Keybindings v] :: list)
change [index v] by (1)
if <key (item (index) of [Keybindings v] :: list) pressed?> then
if <(item (index) of [Keybindings v]) = [space]> then // Example.
run script :: grey
end
if <(item (index) of [Keybindings v]) = [c]> then // Example.
run script :: grey
end
. . .
end
end
NeonG4
Scratcher
1000+ posts

How to detect what key is pressed?

abcde1234qwe wrote:

clipped
The problem with that is I will have 78 different if statements, and I can't really spare another list. Could I use a variable?
abcde1234qwe
Scratcher
500+ posts

How to detect what key is pressed?

NeonG4 wrote:

The problem with that is I will have 78 different if statements, and I can't really spare another list. Could I use a variable?
You could use a variable, but a list would just be more simpler. Since you'd like to use a variable, you'll have to separate each key with a special symbol or character. Maybe with unicode or a character that you aren't using.

(Chars)//For all sprites or for this sprite only.
(Key)//For this sprite.

when green flag clicked
set [Chars v] to [a:b:c: . . .]//May change depending on the characters you are using.

define Check Key Pressed
set [index v] to [0]
repeat until <(letter (index) of (Chars)) = []>//Blank.
set [Key v] to []//Blank.
repeat until <(letter (index) of (Chars)) = [:]>//Or your special char.
change [index v] by (1)
set [Key v] to (join (Key) (letter (index) of (Chars)))
end
change [index v] by (1)
if <key (Key) pressed?> then
if <(Key) = [space]> then // Example.
run script :: grey
end
if <(Key) = [c]> then // Example.
run script :: grey
end
. . .
end
end

Last edited by abcde1234qwe (Aug. 4, 2022 18:28:47)

cIoudyness
Scratcher
500+ posts

How to detect what key is pressed?

ooh I have an idea!
set [letters v] to [abcdef…]

forever
set [count v] to (((count) mod (26)) + (1))
end

when (letter (count) of (letters)) pressed::hat pen // makey makey extension!
say (letter (count) of (letters)) for (2) secs

Last edited by cIoudyness (Aug. 4, 2022 19:32:01)

NeonG4
Scratcher
1000+ posts

How to detect what key is pressed?

cIoudyness wrote:

clipped
OMG!!! TY!!!!
cIoudyness
Scratcher
500+ posts

How to detect what key is pressed?

won’t work for enter or shift (one of those doesn’t work anyway) but for everything else that’s just one symbol, it’ll do well
NeonG4
Scratcher
1000+ posts

How to detect what key is pressed?

cIoudyness wrote:

clipped
I tried it out, and it works fine, but it cycles too slowly. as I mentioned earlier, I have a 2 MB project in code, and it lags. I need to sense what key is pressed, and once that is done, add it to a list. Is there a better way?
cIoudyness
Scratcher
500+ posts

How to detect what key is pressed?

NeonG4 wrote:

cIoudyness wrote:

clipped
I tried it out, and it works fine, but it cycles too slowly. as I mentioned earlier, I have a 2 MB project in code, and it lags. I need to sense what key is pressed, and once that is done, add it to a list. Is there a better way?
I’m not sure. you might be able to throw the letter () of () into a key pressed boolean and put that under a w/o screen refresh
this is probably the most list efficient method tho
NeonG4
Scratcher
1000+ posts

How to detect what key is pressed?

cIoudyness wrote:

clipped
okay, I'll try it sometime.
ThatStrategyGuy
Scratcher
3 posts

How to detect what key is pressed?

NeonG4 wrote:

Jolte0n wrote:

You mean like this?
when [Space] key pressed
say [hi]
Sort of, but I don't want 36 different events.

ametz807 wrote:

There's not much you can do apart from adding every single possible key to a list, then looping through them.
I don't want to use data storage though, because my project is already at 2,400 blocks and 2 mb
when green flag clicked
forever
if <key [space v] pressed?> then



else
if <key [v v] pressed?> then



else
if <key [k v] pressed?> then



else

end
end
You could Do this
legendary34678
Scratcher
1000+ posts

How to detect what key is pressed?

Hey there, ThatStrategyGuy! cloudyness has provided a more efficient solution to NeonG4's problem. Try to read the thread before suggesting the exact same thing as other people. Thanks!
cIoudyness
Scratcher
500+ posts

How to detect what key is pressed?

the premise of this topic was efficiency and avoiding lots of lists or conditional c blocks. scratch has its limits and you might want to look into other languages to code what you want?
Alberknyis
Scratcher
1000+ posts

How to detect what key is pressed?

NeonG4 wrote:

and I can't really spare another list.

No such thing. A project will use as many lists and variables as it needs.

I assume you are looking for a way to lessen the number of scripts in your “Scratch as a language” project. If this is the case, I suggest abcde1234qwe's solution, but instead of running a separate if statement for every key (which undermines the entire purpose of the solution, to not make a separate case for every key), have a second list named “keyheld” which has a corresponding item for every key in the first list, and in the second list replace that item with 1 if that key is pressed, and with 0 if that key is not pressed.

This way you will have a list that lets you know which keys are pressed at every frame, which, assuming you will use to type commands directly into the project, you can loop through and run the “Update line” block on every key that has a keyheld value of 1

This method can be used under the “when any key pressed” hat block, but if you do you must use the “Update line” custom block immediately after and nowhere else, because the list will only update if a key is pressed, so if you check the list while no keys are pressed you will get a wrong result. To be able to use this method in a forever loop you will make a third list “keypressed”, which only returns 1 for a particular key if the key was pressed on that frame, but returns 0 if the key continues to be held.
Arth_TCas
Scratcher
7 posts

How to detect what key is pressed?

i need use ? in chroome, but key not back, is pressed forever, please help.
Norse7
Scratcher
100+ posts

How to detect what key is pressed?

arthurteixeiradecas wrote:

i need use ? in chroome, but key not back, is pressed forever, please help.
please explain more. I can't really understand what you're trying to say.

Powered by DjangoBB