The Green Shoes Manual 1.1.362

Events

Wondering how to catch stray mouse clicks or keyboard typing? Events are sent to a slot whenever a mouse moves inside the slot. Or whenever a key is pressed. Even when the slot is created or destroyed. You can attach a block to each of these events.

Mouse events include motion, click, release, hover and leave. Keyboard typing is represented by the keypress event. And the start and finish events indicate when a canvas comes into play or is discarded.

So, let's say you want to change the background of a slot whenever the mouse floats over it. We can use the hover event to change the background when the mouse comes inside the slot. And leave to change back when the mouse floats away.

 Shoes.app do
   s = stack width: 200, height: 200 do
     background red
   end
   s.hover do
     s.clear { background blue }
   end
   s.leave do
     s.clear { background red }
   end
 end

The click block is called when a mouse button is clicked. The button is the number of the mouse button which has been pressed. The left and top are the mouse coordinates at which the click happened.

To catch the moment when the mouse is unclicked, see the release event.

When a slot is removed, it's finish event occurs. The finish block is immediately handed self, the slot object which has been removed.

Note: Green Shoes doesn't support finish method.

The hover event happens when the mouse enters the slot. The block gets self, meaning the object which was hovered over.

To catch the mouse exiting the slot, check out the leave event.

Whenever a key (or combination of keys) is pressed, the block gets called. The block is sent a key which is a string representing the character (such as the letter or number) on the key.

So, for example, if Shift-a is pressed, the block will get the string "Shift_L" and "A".

And if the F1 key is pressed, the "F1" string is received.

The left side modifier keys are "Control_L", "Shift_L" and "Alt_L". They appear in that order. If Shift-Control-Alt-PgUp is pressed, the strings will be "Shift_L", "Control_L", "Alt_L" and "Page_Up".

One thing about the shift key. On US keyboards, Shift-7 is an ampersand. So you'll get the string "Shift_L" and "ampersand" rather than "Shift_L" and "7". And, if you press Shift-Alt-7 on such a keyboard, you'll get the strings: "Shift_L", "Alt_L" and "ampersand".

 Shoes.app do
   info = para "NO KEY is PRESSED."
   keypress do |k|
     info.replace "#{k.inspect} was PRESSED."
   end
 end

The leave event takes place when the mouse cursor exits a slot. The moment it no longer is inside the slot's edges. When that takes place, the block is called with self, the slot object which is being left.

Also see hover if you'd like to detect the mouse entering a slot.

The motion block gets called every time the mouse moves around inside the slot. The block is handed the cursor's left and top coordinates.

 Shoes.app width: 200, height: 200 do
   background black
   fill white
   circ = oval 0, 0, 100, 100
   motion do |top, left|
     circ.move top - 50, left - 50
   end
 end

The release block runs whenever the mouse is unclicked (on mouse up). When the finger is lifted. The button is the number of the button that was depressed. The left and top are the coordinates of the mouse at the time the button was released.

To catch the actual mouse click, use the click event.

The first time the slot is drawn, the start event fires. The block is handed self, the slot object which has just been drawn.

Note: Green Shoes doesn't support start method.

Next: Manipulation Blocks