Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » Detect which key is pressed
- -WhoAsked-
-
Scratcher
79 posts
Detect which key is pressed
So, I want to make a writing engine, but the thing is that after it checks that you pressed a key, it doesn't know what letter to write down, does anyone have a clue of to do this? please I don't want to make 758493654 scripts to check for each key on the keyboard
- Vadik1
-
Scratcher
500+ posts
Detect which key is pressed
Detect which key is pressed //has "run without screen refresh" enabled
define Detect which key is pressed
set [keys v] to [qwertyuiopasdfghjklzxcvbnm1234567890 -=_+!@#$%^&*(){};:'"\\|/?.,><`\[\]]
set [iterator v] to [0]
repeat (length of (keys))
change [iterator v] by (1)
if <key (letter (iterator) of (keys)) pressed?> then
set [pressed key v] to (letter (iterator) of (keys))
stop [this script v]
end
end
set [pressed key v] to []
- -WhoAsked-
-
Scratcher
79 posts
Detect which key is pressed
THANK YOU SO MUCHDetect which key is pressed //has "run without screen refresh" enabled
define Detect which key is pressed
set [keys v] to [qwertyuiopasdfghjklzxcvbnm1234567890 -=_+!@#$%^&*(){};:'"\\|/?.,><`\[\]]
set [iterator v] to [0]
repeat (length of (keys))
change [iterator v] by (1)
if <key (letter (iterator) of (keys)) pressed?> then
set [pressed key v] to (letter (iterator) of (keys))
stop [this script v]
end
end
set [pressed key v] to []
- -WhoAsked-
-
Scratcher
79 posts
Detect which key is pressed
It didn't workDetect which key is pressed //has "run without screen refresh" enabled
define Detect which key is pressed
set [keys v] to [qwertyuiopasdfghjklzxcvbnm1234567890 -=_+!@#$%^&*(){};:'"\\|/?.,><`\[\]]
set [iterator v] to [0]
repeat (length of (keys))
change [iterator v] by (1)
if <key (letter (iterator) of (keys)) pressed?> then
set [pressed key v] to (letter (iterator) of (keys))
stop [this script v]
end
end
set [pressed key v] to []
- Vadik1
-
Scratcher
500+ posts
Detect which key is pressed
It didn't workCan you share the link to the project so I can find where the mistake is?
- sabko
-
Scratcher
41 posts
Detect which key is pressed
Let me tell you.
If you are making a Text editor by stamping costume then the code is simple.
Firstly if you had made those costumes so name all those to the keys like if you have made the letter a then name it a and do same with all costume.
Create a new list called keys for this sprite only.
Fill all the list with your costume names.
Now do this code—>
Hope it helps
If you are making a Text editor by stamping costume then the code is simple.
Firstly if you had made those costumes so name all those to the keys like if you have made the letter a then name it a and do same with all costume.
Create a new list called keys for this sprite only.
Fill all the list with your costume names.
Now do this code—>
when green flag clickedScript for clones–>
create key clones
define create key clones
set [clone id v] to [0]//This clone id variable should be for this sprite only.
repeat (length of [keys v] :: list)
change [clone id v] by [1]
create clone of [myself v]
end
when I start as a clone
forever
if <key (item (clone id) of [key v] :: list) pressed?> then
wait until <not<key (item (clone id) of [key v] :: list) pressed?>>
Text (item (clone id) of [key v] :: list)//Your stamping script.
end
end
define Text (Letter)
switch costume to (Letter)
stamp
Hope it helps
- shopperkeeps
-
Scratcher
22 posts
Detect which key is pressed
It didn't workDetect which key is pressed //has "run without screen refresh" enabled
define Detect which key is pressed
set [keys v] to [qwertyuiopasdfghjklzxcvbnm1234567890 -=_+!@#$%^&*(){};:'"\\|/?.,><`\[\]]
set [iterator v] to [0]
repeat (length of (keys))
change [iterator v] by (1)
if <key (letter (iterator) of (keys)) pressed?> then
set [pressed key v] to (letter (iterator) of (keys))
stop [this script v]
end
end
set [pressed key v] to []
remove the stop script
- Vadik1
-
Scratcher
500+ posts
Detect which key is pressed
remove the stop scriptDo not.
Then it will definitely stop working.
Stop script is required for it to exit out of the custom block when the key is found. If it doesn't exit out of it early, then no key was pressed and it resets “pressed key” variable. Removing that thing will prevent it from exiting out of the loop early and it will always behave as if no key was pressed.
- LostLittleChick738
-
Scratcher
500+ posts
Detect which key is pressed
Let me tell you.but then you would have to do 7937423 scripts for each key for the
If you are making a Text editor by stamping costume then the code is simple.
Firstly if you had made those costumes so name all those to the keys like if you have made the letter a then name it a and do same with all costume.
Create a new list called keys for this sprite only.
Fill all the list with your costume names.
Now do this code—>when green flag clickedScript for clones–>
create key clones
define create key clones
set [clone id v] to [0]//This clone id variable should be for this sprite only.
repeat (length of [keys v] :: list)
change [clone id v] by [1]
create clone of [myself v]
endwhen I start as a clone
forever
if <key (item (clone id) of [key v] :: list) pressed?> then
wait until <not<key (item (clone id) of [key v] :: list) pressed?>>
Text (item (clone id) of [key v] :: list)//Your stamping script.
end
end
define Text (Letter)
switch costume to (Letter)
stamp
Hope it helps
(key :: list)list
- Discussion Forums
- » Help with Scripts
-
» Detect which key is pressed