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:
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 1You 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:
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.
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 1Run the script—variable keyPressed will show the key you're pressing!
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)
Offline
Are you absolutely sure that the scripts will remain once the image is saved?
Offline
I tried this already. You can't drag a reporter into the hat.
Offline
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.
Offline
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
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
Offline
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
![]()
What if you click Quote?
Try editing Scratch-Blocks -> KeyEventHatMorph -> --all-- initialize.
Offline
This would do so much better in the Suggestions section of the forums.
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.
Offline
I thought it is in the Library.
Offline
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 1You 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 1Run the script—variable keyPressed will show the key you're pressing!
![]()
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 1Congratulations! 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.
Offline
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?
Offline
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
Offline
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
![]()
You could say
Hardmath123 wrote:
The kay pressed block would be extremely, extremely helpful
...
shortened
...
2) This will work online!
Offline
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
![]()
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
LESS LAG
Offline
ssss wrote:
Hardmath123 wrote:
ssss wrote:
what's the point? Then we have no idea what they are referencing![]()
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 patchLESS LAG
![]()
Ok, please give me some credit.
Offline
Hardmath123 wrote:
ssss wrote:
Hardmath123 wrote:
ssss wrote:
what's the point? Then we have no idea what they are referencing
![]()
You could say
yeah....
YAYZ! I will add this to my SynOS 7 patchLESS LAG
![]()
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
Offline
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
![]()
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)
Offline
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
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
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)
Offline
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!
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! >
Could someone help me out?
Last edited by LEGOengineer261 (2012-05-04 01:31:01)
Offline
This is similar to the one in Processing right? The variable that sets to the button pressed.
Offline
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!![]()
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! >![]()
Could someone help me out?
Alt-click on the text area where you pasted my code.
@jji7skyline: Yeah.
Offline