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

#1 2011-04-27 06:17:34

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

A (key pressed) block that works online!

The (Key pressed) block would be extremely, extremely helpful for projects like word editors. Unfortunately, it doesn't exist. Only the <key [space] pressed> boolean exists. A new block can be coded, but that means it won't work online. So, most Scratchers end up manually making a long script, with an if block for every key. That it a long job.

Solution? Well, you could get away with only one if block: here's how:

Code:

forever:
    set n to 1
    repeat 41 times:
        if <key (item (n) of [keys]) pressed>
            set [keyPressed] to (item (n) of [keys])
    change n by 1

You would need a list called keys, which had each key as an item. This would basically run through the list, testing for a key that's pressed. Great!

The catch? I cleverly assumed that you can drag reporters (round blocks like ((2) + (2))) into the <key [space] presses> block, like the [switch to costume [costume]] block. It doesn't.

Well, let's make it!

Open up the System Browser (learn how here), and navigate:
Scratch-Blocks » CommandBlockMorph » private » uncoloredArgMorphFor:

Replace all the coding with:

Code:

uncoloredArgMorphFor: t1 
    | t2 |
    t2 _ t1 at: 2.
    $a = t2 ifTrue: [^ AttributeArgMorph new choice: 'volume'].
    $b = t2 ifTrue: [^ BooleanArgMorph new].
    $c = t2 ifTrue: [^ ColorArgMorph new showPalette: true].
    $C = t2 ifTrue: [^ ColorArgMorph new showPalette: false].
    $d = t2 ifTrue: [^ ExpressionArgMorphWithMenu new numExpression: '0';
         menuSelector: #directionMenu].
    $D = t2 ifTrue: [^ ExpressionArgMorphWithMenu new numExpression: '48';
         menuSelector: #midiDrumMenu].
    $e = t2 ifTrue: [^ EventTitleMorph new].
    $f = t2 ifTrue: [^ ChoiceArgMorph new getOptionsSelector: #mathFunctionNames;
         choice: 'sqrt'].
    $g = t2 ifTrue: [^ ChoiceArgMorph new getOptionsSelector: #graphicEffectNames;
         choice: 'color'].
    $H = t2 ifTrue: [^ ChoiceArgMorph new getOptionsSelector: #hookupSensorNames].
    $h = t2 ifTrue: [^ ChoiceArgMorph new getOptionsSelector: #hookupBooleanSensorNames].
    $I = t2 ifTrue: [^ ExpressionArgMorphWithMenu new numExpression: '1';
         menuSelector: #midiInstrumentMenu].
    $i = t2 ifTrue: [^ ExpressionArgMorphWithMenu new numExpression: '1';
         menuSelector: #listIndexMenu].
    $k = t2 ifTrue: [^ ChoiceOrExpressionArgMorph new getOptionsSelector: #keyNames;
         choice: 'space'].
    $L = t2 ifTrue: [^ ChoiceArgMorph new getOptionsSelector: #listVarMenu].
    $l = t2 ifTrue: [^ ChoiceOrExpressionArgMorph new getOptionsSelector: #costumeNames;
         choice: 'costume1'].
    $m = t2 ifTrue: [^ SpriteArgMorph new].
    $M = t2 ifTrue: [^ ChoiceArgMorph new getOptionsSelector: #motorNames].
    $n = t2 ifTrue: [^ ExpressionArgMorph new numExpression: '10'].
    $N = t2 ifTrue: [^ ExpressionArgMorphWithMenu new numExpression: '60';
         menuSelector: #noteSelector].
    $s = t2 ifTrue: [^ ExpressionArgMorph new stringExpression: ''].
    $S = t2 ifTrue: [^ ChoiceOrExpressionArgMorph new getOptionsSelector: #soundNames;
         choice: 'pop'].
    $v = t2 ifTrue: [^ ChoiceArgMorph new getOptionsSelector: #varNamesMenu;
         choice: ''].
    $W = t2 ifTrue: [^ ChoiceArgMorph new getOptionsSelector: #motorDirection].
    $x = t2 ifTrue: [^ ChoiceOrExpressionArgMorph new getOptionsSelector: #sceneNames;
         choice: ''].
    $y = t2 ifTrue: [^ ExpressionArgMorphWithMenu new numExpression: '1';
         menuSelector: #listIndexForDeleteMenu].
    ^ ExpressionArgMorph new numExpression: '10'

Alt-click, and choose accept. Close the Browser. Shift-click the R again, and click 'turn fill screen on'.

Do NOT quit Scratch until I say so later!

Drag out the <key [space] pressed?> block. Take any reporter, and stuff it into the [space] dropdown list. Voila! It fits!

Now, make and run the following script (recognize it?). You will need to make 2 variables called 'n' and keyPressed. You also need to make a list called 'keys' and put every key's name in it.

Code:

forever:
    set n to 1
    repeat 41 times:
        if <key (item (n) of [keys]) pressed>
            set [keyPressed] to (item (n) of [keys])
    change n by 1

Run the script—variable keyPressed will show the key you're pressing!  big_smile

Now, you can save and quit Scratch.

Note that
1) Once you reopen Scratch, while these scripts will remain there, the key [] pressed's modification will get deleted.
2) This will work online!

Last edited by Hardmath123 (2012-05-04 04:29:33)


Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

#2 2011-04-27 07:57:16

LS97
Scratcher
Registered: 2009-06-14
Posts: 1000+

Re: A (key pressed) block that works online!

Are you absolutely sure that the scripts will remain once the image is saved?

Offline

 

#3 2011-04-27 08:02:14

scimonster
Community Moderator
Registered: 2010-06-13
Posts: 1000+

Re: A (key pressed) block that works online!

I tried this already.  You can't drag a reporter into the hat.

Offline

 

#4 2011-04-27 08:17:36

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Re: A (key pressed) block that works online!

LS97 wrote:

Are you absolutely sure that the scripts will remain once the image is saved?

Yep! I've tried it.

sci wrote:

I tried this already.  You can't drag a reporter into the hat.

Yes, you can.


Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

#5 2011-04-27 08:19:31

scimonster
Community Moderator
Registered: 2010-06-13
Posts: 1000+

Re: A (key pressed) block that works online!

Hardmath123 wrote:

LS97 wrote:

Are you absolutely sure that the scripts will remain once the image is saved?

Yep! I've tried it.

Scimonster wrote:

I tried this already.  You can't drag a reporter into the hat.

Yes, you can.

Not with what I did.
Also, you really don't have to shorten my name for a quote. ^^

Offline

 

#6 2011-04-27 08:20:38

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Re: A (key pressed) block that works online!

scimonster wrote:

Hardmath123 wrote:

LS97 wrote:

Are you absolutely sure that the scripts will remain once the image is saved?

Yep! I've tried it.

Scimonster wrote:

I tried this already.  You can't drag a reporter into the hat.

Yes, you can.

Not with what I did.
Also, you really don't have to shorten my name for a quote. ^^

I'm lazy  tongue


Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

#7 2011-04-27 08:25:01

scimonster
Community Moderator
Registered: 2010-06-13
Posts: 1000+

Re: A (key pressed) block that works online!

Hardmath123 wrote:

scimonster wrote:

Hardmath123 wrote:


Yep! I've tried it.


Yes, you can.

Not with what I did.
Also, you really don't have to shorten my name for a quote. ^^

I'm lazy  tongue

What if you click Quotetongue

Try editing Scratch-Blocks -> KeyEventHatMorph -> --all-- initialize.

Offline

 

#8 2011-04-27 08:35:44

sparks
Community Moderator
Registered: 2008-11-05
Posts: 1000+

Re: A (key pressed) block that works online!

you could always change the block argument to a string for the key boolean, then add

"t1 as UTF8" (I think) at the top of the blockspecs.


http://img541.imageshack.us/img541/7563/scratchbetabanner.png

Offline

 

#9 2011-04-27 11:02:52

Magnie
Scratcher
Registered: 2007-12-12
Posts: 1000+

Re: A (key pressed) block that works online!

This would do so much better in the Suggestions section of the forums.  tongue  But I'm sure they'll add a lot of things ( hopefully ) in Scratch 2.0 and I hope this is in it.

Sparks: Add this to Panther, or at least add it to the Block Library.  tongue

Offline

 

#10 2011-04-27 11:22:10

scimonster
Community Moderator
Registered: 2010-06-13
Posts: 1000+

Re: A (key pressed) block that works online!

I thought it is in the Library.  hmm

Offline

 

#11 2011-04-27 17:03:35

Pecola1
Scratcher
Registered: 2010-09-06
Posts: 1000+

Re: A (key pressed) block that works online!

Hardmath123 wrote:

The (Key pressed) block would be extremely, extremely helpful for projects like word editors. Unfortunately, it doesn't exist. Only the <key [space] pressed> boolean exists. A new block can be coded, but that means it won't work online. So, most Scratchers end up manually making a long script, with an if block for every key. That it a long job.

Solution? Well, you could get away with only one if block: here's how:

Code:

forever:
    set n to 1
    repeat 41 times:
        if <key (item (n) of [keys]) pressed>
            set [keyPressed] to (item (n) of [keys])
    change n by 1

You would need a list called keys, which had each key as an item. This would basically run through the list, testing for a key that's pressed. Great!

The catch? I cleverly assumed that you can drag reporters (round blocks like ((2) + (2))) into the <key [space] presses> block, like the [switch to costume [costume]] block. It doesn't.

Well, let's make it!

Open up the System Browser (learn how here), and navigate:
Scratch-Blocks » CommandBlockMorph » private » uncoloredArgMorphFor:

Replace all the coding with:

Code:

uncoloredArgMorphFor: t1 
    | t2 |
    t2 _ t1 at: 2.
    $a = t2 ifTrue: [^ AttributeArgMorph new choice: 'volume'].
    $b = t2 ifTrue: [^ BooleanArgMorph new].
    $c = t2 ifTrue: [^ ColorArgMorph new showPalette: true].
    $C = t2 ifTrue: [^ ColorArgMorph new showPalette: false].
    $d = t2 ifTrue: [^ ExpressionArgMorphWithMenu new numExpression: '0';
         menuSelector: #directionMenu].
    $D = t2 ifTrue: [^ ExpressionArgMorphWithMenu new numExpression: '48';
         menuSelector: #midiDrumMenu].
    $e = t2 ifTrue: [^ EventTitleMorph new].
    $f = t2 ifTrue: [^ ChoiceArgMorph new getOptionsSelector: #mathFunctionNames;
         choice: 'sqrt'].
    $g = t2 ifTrue: [^ ChoiceArgMorph new getOptionsSelector: #graphicEffectNames;
         choice: 'color'].
    $H = t2 ifTrue: [^ ChoiceArgMorph new getOptionsSelector: #hookupSensorNames].
    $h = t2 ifTrue: [^ ChoiceArgMorph new getOptionsSelector: #hookupBooleanSensorNames].
    $I = t2 ifTrue: [^ ExpressionArgMorphWithMenu new numExpression: '1';
         menuSelector: #midiInstrumentMenu].
    $i = t2 ifTrue: [^ ExpressionArgMorphWithMenu new numExpression: '1';
         menuSelector: #listIndexMenu].
    $k = t2 ifTrue: [^ ChoiceOrExpressionArgMorph new getOptionsSelector: #keyNames;
         choice: 'space'].
    $L = t2 ifTrue: [^ ChoiceArgMorph new getOptionsSelector: #listVarMenu].
    $l = t2 ifTrue: [^ ChoiceOrExpressionArgMorph new getOptionsSelector: #costumeNames;
         choice: 'costume1'].
    $m = t2 ifTrue: [^ SpriteArgMorph new].
    $M = t2 ifTrue: [^ ChoiceArgMorph new getOptionsSelector: #motorNames].
    $n = t2 ifTrue: [^ ExpressionArgMorph new numExpression: '10'].
    $N = t2 ifTrue: [^ ExpressionArgMorphWithMenu new numExpression: '60';
         menuSelector: #noteSelector].
    $s = t2 ifTrue: [^ ExpressionArgMorph new stringExpression: ''].
    $S = t2 ifTrue: [^ ChoiceOrExpressionArgMorph new getOptionsSelector: #soundNames;
         choice: 'pop'].
    $v = t2 ifTrue: [^ ChoiceArgMorph new getOptionsSelector: #varNamesMenu;
         choice: ''].
    $W = t2 ifTrue: [^ ChoiceArgMorph new getOptionsSelector: #motorDirection].
    $x = t2 ifTrue: [^ ChoiceOrExpressionArgMorph new getOptionsSelector: #sceneNames;
         choice: ''].
    $y = t2 ifTrue: [^ ExpressionArgMorphWithMenu new numExpression: '1';
         menuSelector: #listIndexForDeleteMenu].
    ^ ExpressionArgMorph new numExpression: '10'

Alt-click, and choose accept. Close the Browser. Shift-click the R again, and click 'turn fill screen on'.

Do NOT quit Scratch until I say so later!

Drag out the <key [space] pressed?> block. Take any reporter, and stuff it into the [space] dropdown list. Voila! It fits!

Now, make and run the following script (recognize it?). You will need to make 2 variables called 'n' and keyPressed. You also need to make a list called 'keys' and put every key's name in it.

Code:

forever:
    set n to 1
    repeat 41 times:
        if <key (item (n) of [keys]) pressed>
            set [keyPressed] to (item (n) of [keys])
    change n by 1

Run the script—variable keyPressed will show the key you're pressing!  big_smile

If you're holding down many keys together, it'll glitch, but that's easy to fix. Here's how:

With the same variables, make these scripts:

Code:

When key (item (n) of (keys)) pressed
set [keyPressed] to (item ((n) mod (41)) of (keys))

When green flag pressed
forever:
    change (n) by 1

Congratulations! Variable keyPressed is your (key pressed) block!

Now, you can save and quit Scratch.

Note that
1) Once you reopen Scratch, while these scripts will remain there, the key [] pressed's modification will get deleted.
2) This will work online!

Yes I have noticed this, though you made it sound like the reporter did, only a reporter which fits into it will stay there.


If you are reading this, please read to the end, because if you don't you won't know what's at the end. Don't just skip to the end though otherwise you won't be able to read the middle, which is most important. Now you must be wondering why you just read all that, the reason is you may have not noticed something, read it again and see if you notice it this time  smile

Offline

 

#12 2011-04-28 06:06:20

floppy_gunk
Scratcher
Registered: 2008-11-14
Posts: 500+

Re: A (key pressed) block that works online!

Pecola1 wrote:

Hardmath123 wrote:

long post

Yes I have noticed this, though you made it sound like the reporter did, only a reporter which fits into it will stay there.

Next time you quote a long post, could you replace it with 'long post' or something?


http://img163.imageshack.us/img163/1917/2856lg.jpg Get it now!  smile

Offline

 

#13 2011-04-28 06:07:57

ssss
Scratcher
Registered: 2007-07-29
Posts: 1000+

Re: A (key pressed) block that works online!

floppy_gunk wrote:

Pecola1 wrote:

Hardmath123 wrote:

long post

Yes I have noticed this, though you made it sound like the reporter did, only a reporter which fits into it will stay there.

Next time you quote a long post, could you replace it with 'long post' or something?

what's the point?  Then we have no idea what they are referencing  tongue


Hey.  It's me SSSS, back from the dead!  smile

Offline

 

#14 2011-04-28 06:37:16

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Re: A (key pressed) block that works online!

ssss wrote:

floppy_gunk wrote:

Pecola1 wrote:


Yes I have noticed this, though you made it sound like the reporter did, only a reporter which fits into it will stay there.

Next time you quote a long post, could you replace it with 'long post' or something?

what's the point?  Then we have no idea what they are referencing  tongue

You could say

Hardmath123 wrote:

The kay pressed block would be extremely, extremely helpful
...
shortened
...
2) This will work online!


Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

#15 2011-04-28 06:39:15

ssss
Scratcher
Registered: 2007-07-29
Posts: 1000+

Re: A (key pressed) block that works online!

Hardmath123 wrote:

ssss wrote:

floppy_gunk wrote:


Next time you quote a long post, could you replace it with 'long post' or something?

what's the point?  Then we have no idea what they are referencing  tongue

You could say

Hardmath123 wrote:

The kay pressed block would be extremely, extremely helpful
...
shortened
...
2) This will work online!

yeah....

YAYZ!  I will add this to my SynOS 7 patch  tongue   LESS LAG  big_smile


Hey.  It's me SSSS, back from the dead!  smile

Offline

 

#16 2011-04-28 07:05:08

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Re: A (key pressed) block that works online!

ssss wrote:

Hardmath123 wrote:

ssss wrote:


what's the point?  Then we have no idea what they are referencing  tongue

You could say

Hardmath123 wrote:

The kay pressed block would be extremely, extremely helpful
...
shortened
...
2) This will work online!

yeah....

YAYZ!  I will add this to my SynOS 7 patch  tongue   LESS LAG  big_smile

Ok, please give me some credit.


Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

#17 2011-04-28 07:09:19

ssss
Scratcher
Registered: 2007-07-29
Posts: 1000+

Re: A (key pressed) block that works online!

Hardmath123 wrote:

ssss wrote:

Hardmath123 wrote:

ssss wrote:

what's the point?  Then we have no idea what they are referencing  tongue

You could say

yeah....

YAYZ!  I will add this to my SynOS 7 patch  tongue   LESS LAG  big_smile

Ok, please give me some credit.

Yeah...
It will be in somewhere....

Hidden...
In the comments...
Way beneath all the code...

Lol.  jk - i will find a place to put it  tongue


Hey.  It's me SSSS, back from the dead!  smile

Offline

 

#18 2011-04-28 12:58:51

Pecola1
Scratcher
Registered: 2010-09-06
Posts: 1000+

Re: A (key pressed) block that works online!

Hardmath123 wrote:

ssss wrote:

floppy_gunk wrote:

Next time you quote a long post, could you replace it with 'long post' or something?

what's the point?  Then we have no idea what they are referencing  tongue

You could say

Hardmath123 wrote:

The kay pressed block would be extremely, extremely helpful
...
shortened
...
2) This will work online!

Yes, I believe I could, but I would rather just do:

Hardmath123 wrote:

The (Key pressed) block would be extremely, extremely helpful...
...2) This will work online!

Last edited by Pecola1 (2011-04-28 12:59:09)


If you are reading this, please read to the end, because if you don't you won't know what's at the end. Don't just skip to the end though otherwise you won't be able to read the middle, which is most important. Now you must be wondering why you just read all that, the reason is you may have not noticed something, read it again and see if you notice it this time  smile

Offline

 

#19 2012-05-03 22:43:05

shefehs
New Scratcher
Registered: 2012-05-02
Posts: 2

Re: A (key pressed) block that works online!

I know this post is a year old, but I need to do exactly this and I would love it if this code example actually worked, but it doesn't seem to. I followed all the steps carefully, but when I got to the line "Drag out the <key [space] pressed?> block. Take any reporter, and stuff it into the [space] dropdown list. Voila! It fits!" I found that it didn't accept any reporter. I reopened the code for that block and verified that it was your version. (btw, it would have been very helpful to point out exactly what line had changed--I ended up having to paste your version and the original into different text files and use a diff tool!)

I tried all the steps several times to make sure I hadn't missed a step, but I had the same problem as scimonster--it just wouldn't accept the reporter.  Is there a step missing or a precondition that I don't have?  Thanks very much!

Offline

 

#20 2012-05-03 23:09:27

shefehs
New Scratcher
Registered: 2012-05-02
Posts: 2

Re: A (key pressed) block that works online!

Correcting myself--it works in the if keypressed, but not in the hat.  I tried modifying the initialize method of the KeyEventHatMorph to use a ChoiceOrExpressionArgMorph instead of just a ChoiceArgMorph, but that didn't do it.  Anyone successfully modify the Key press hat?

Offline

 

#21 2012-05-03 23:27:04

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Re: A (key pressed) block that works online!

Well, the hat is hard to modify, but you could do this:

when gf clicked
forever
if <key (someKey) pressed?>
broadcast [somekeywaspress v]
end

when I receive [somekeywaspressed v]
say [Hi!]//Do something!
Enjoy!

Last edited by Hardmath123 (2012-05-04 01:02:49)


Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

#22 2012-05-04 01:25:38

LEGOengineer261
Scratcher
Registered: 2011-11-16
Posts: 100+

Re: A (key pressed) block that works online!

Alt-click, and choose accept. Close the Browser. Shift-click the R again, and click 'turn fill screen on'.

I alt-clicked, and there was no accept option. ?
there was only buttons for grow me, debug me, duplicate me, drag me, grab me, menu for me, dismiss me, and collapse me. Please help, anybody, I can't exit until its fixed!  yikes



So, I ended up just clicking somewhere else, and it asked me if I wanted to save changes, I clicked yes. But I still can't drag reporters into the key-pressed thing! > sad
Could someone help me out?

Last edited by LEGOengineer261 (2012-05-04 01:31:01)


http://i1156.photobucket.com/albums/p562/LEGOengineer261/IronmanSERIOUSBOSS.jpg

Offline

 

#23 2012-05-04 01:37:16

jji7skyline
Scratcher
Registered: 2010-03-08
Posts: 1000+

Re: A (key pressed) block that works online!

This is similar to the one in Processing right? The variable that sets to the button pressed.


I don't know why you say goodbye, I say hello!  big_smile

Offline

 

#24 2012-05-04 04:28:16

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Re: A (key pressed) block that works online!

LEGOengineer261 wrote:

Alt-click, and choose accept. Close the Browser. Shift-click the R again, and click 'turn fill screen on'.

I alt-clicked, and there was no accept option. ?
there was only buttons for grow me, debug me, duplicate me, drag me, grab me, menu for me, dismiss me, and collapse me. Please help, anybody, I can't exit until its fixed!  yikes



So, I ended up just clicking somewhere else, and it asked me if I wanted to save changes, I clicked yes. But I still can't drag reporters into the key-pressed thing! > sad
Could someone help me out?

Alt-click on the text area where you pasted my code.

@jji7skyline: Yeah.


Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

Board footer