Discuss Scratch

Sebatho
Scratcher
30 posts

How to check if ONLY a certain key is pressed

Is there a simple way to check if the space key is pressed and nothing else is pressed? I want the player to open their mini inventory when they are not pressing any other button (to do things such as moving, jumping, attacking, etc.) except for the space button.

Last edited by Sebatho (April 7, 2024 00:59:17)

malicondii
Scratcher
100+ posts

How to check if ONLY a certain key is pressed

You can with this block:
define is only space pressed?  // run without screen refresh
set [i v] to (1)
repeat (length of [abcdefghijklmnopqrstuvwxyz1234567890
if <key [space v] pressed?> then
set [only space pressed v] to [true]
if <key (letter (i) of [abcdefghijklmnopqrstuvwxyz1234567890]) pressed?> then
set [only space pressed v] to [false]
end
if <key [up arrow v] pressed?> then
set [only space pressed v] to [false]
end
if <key [down arrow v] pressed?> then
set [only space pressed v] to [false]
end
if <key [left arrow v] pressed?> then
set [only space pressed v] to [false]
end
if <key [right arrow v] pressed?> then
set [only space pressed v] to [false]
end
change [i v] by (1)
Also, if you want it to work for any key (including like .,/;' etc) you can modify the string of “abcde…” in the block, and add any characters you want.

Edit: 0 second ninja is wild

Last edited by malicondii (April 7, 2024 02:26:13)

MultiTasker801
Scratcher
100+ posts

How to check if ONLY a certain key is pressed

something like
define only space pressed
// run without screen refresh
if <key [space v] pressed?> then
set [only space pressed v] to [1]
set [i v] to [0]
repeat (length of [`1234567890-=qwertyuiop\[\]\\asdfghjkl;'zxcvbnm,./])
change [i v] by [1]
if <key (letter (i) of [`1234567890-=qwertyuiop\[\]\\asdfghjkl;'zxcvbnm,./]) pressed?> then
set [only space pressed v] to [0]
stop [this script v]
end
would work. there might be a better way to do this though
you can also definitely remove some of the characters from those blocks since i doubt you really need to check if, for instance, the user is holding the ` key when they hit space. removing characters will make it run slightly faster


edit: i got ninja'd by zero seconds. with a better response too. feels badman

Last edited by MultiTasker801 (April 7, 2024 02:25:23)

Sebatho
Scratcher
30 posts

How to check if ONLY a certain key is pressed

malicondii wrote:

You can with this block:
define is only space pressed?  // run without screen refresh
set [i v] to (1)
repeat (length of [abcdefghijklmnopqrstuvwxyz1234567890
if <key [space v] pressed?> then
set [only space pressed v] to [true]
if <key (letter (i) of [abcdefghijklmnopqrstuvwxyz1234567890]) pressed?> then
set [only space pressed v] to [false]
end
if <key [up arrow v] pressed?> then
set [only space pressed v] to [false]
end
if <key [down arrow v] pressed?> then
set [only space pressed v] to [false]
end
if <key [left arrow v] pressed?> then
set [only space pressed v] to [false]
end
if <key [right arrow v] pressed?> then
set [only space pressed v] to [false]
end
change [i v] by (1)
Also, if you want it to work for any key (including like .,/;' etc) you can modify the string of “abcde…” in the block, and add any characters you want.

Edit: 0 second ninja is wild

MultiTasker801 wrote:

something like
define only space pressed
// run without screen refresh
if <key [space v] pressed?> then
set [only space pressed v] to [1]
set [i v] to [0]
repeat (length of [`1234567890-=qwertyuiop\[\]\\asdfghjkl;'zxcvbnm,./])
change [i v] by [1]
if <key (letter (i) of [`1234567890-=qwertyuiop\[\]\\asdfghjkl;'zxcvbnm,./]) pressed?> then
set [only space pressed v] to [0]
stop [this script v]
end
would work. there might be a better way to do this though
you can also definitely remove some of the characters from those blocks since i doubt you really need to check if, for instance, the user is holding the ` key when they hit space. removing characters will make it run slightly faster


edit: i got ninja'd by zero seconds. with a better response too. feels badman
Thank you @malicondii and @MultiTasker801 for helping me solve this problem! I appreciate your help

Powered by DjangoBB