From Test-Scratch-Wiki

< Eng:Key () Pressed? (block)

Archive.png This article or section documents a feature not included in the current version of Scratch (2.0). It is only useful from a historical perspective.

To enable drag and dropping reporter block in the drop-down menu on the Key () Pressed? block, you need to change the %K argument's function in the System Browser by editing the underlying Squeak code which controls inputs.

Enabling

First, open the System Browser. Navigate to Scratch-Blocks → CommandBlockMorph → private → uncoloredArgMorphFor:

Replace the code with this:

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'

Alternatively, scroll down to the line:

	$k = t2 ifTrue: [^ ChoiceArgMorph new getOptionsSelector: #keyNames;
		 choice: 'space'].

and replace ChoiceArgMorph with ChoiceOrExpressionArgMorph:

$k = t2 ifTrue: [^ ChoiceOrExpressionArgMorph new getOptionsSelector: #keyNames;
		 choice: 'space'].

Finally, Shift-Click R and select "save image for end-user" to save your changes.

Warning Warning: Saving your changes may cause irreversible damage to your copy of Scratch. If this happens, simply re-install Scratch.

Explanation

This patch functions by changing the input type. The basic input types include text boxes, text boxes with drop-down menus, drop-down menus which accept drop-ins, and drop-down menus which do not. By setting the input type to ChoceOrExpressionArgMorph, we are setting it a drop-down menu which accept drop-ins.

Using the modified block

There are many uses for this change. One use, namely emulating the (key pressed) block, is outlined here:

when gf clicked
set [n v] to [0]
forever
set [n v] to ((n) mod ((length of [keys v])+(1)))
change [n v] by (1)
if <key (item (n) of [keys v]) pressed?>
set [key v] to (item [n v] of [keys v])
end

when key [space v] pressed
say (key)// space

The list "keys" contains all the keys, namely:

1
2
3
4
5
6
7
8
9
0
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
space
up arrow
down arrow
left arrow
right arrow

This script makes projects like word processors very easy.

Cookies help us deliver our services. By using our services, you agree to our use of cookies.