Skip to content
This repository
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 127 lines (89 sloc) 3.795 kb

keypress, mouse and clipboard

A Shoes proggram can react to mouse events and interact with the system's clipboard.

# sample20.rb
Shoes.app :title => 'Sorter', :width => 180, :height => 80 do
  background gradient powderblue, royalblue
  msg = para '', :size => 8

  yar = image('../images/yar.png', :left => 60, :top => 18).click do
    self.clipboard = self.clipboard.sort unless self.clipboard.nil?
    yar.transform :center
    a = animate(24) do |i|
      yar.rotate -15
      a.stop if i > 22
    end
  end
  yar.hover{msg.text = strong('Click Yar. She sorts clipboard text!')}
  yar.leave{msg.text = ''}
end

sample20.png

sample20.png

Let's hee see how this program manipulates the clipboard

Before:
Creatures name list is:
loogink
cy
kamome
yar
shaha

Copy the above list into the system clipboard.
Click Yar and she will rotate (*1).
Paste the clipboard text into the place you want.

*1: With Shoes-0.r925, Yar rotates as expected, but with Shoes-0.r970, Yar rotates when the mouse moves out of the Shoes window. This behavior is a bug. It has been fixed in Shoes-0.r1057.

After:
Creatures name list is:
cy
kamome
loogink
shaha
yar

How to work with keypress events:

# sample21.rb
Shoes.app :width => 250, :height => 40 do
  @info = para 'NO KEY is PRESSED.'
  keypress{|key| @info.text = "#{key.inspect} was PRESSED."}
end

sample21.png

sample21.png

ATTENTION

Please be aware of the following behavior which may be cause by a bug in Shoes:

In the following code snippet, cases 1, 3 and 4 will crash. Case 2 works because it creates a new empty string object ('') which it assigns to self.clipboard instead of p2.text in case the latter is empty.

This problem was discovered in the code of rotten.rb created by Michael Kohl. It does not seem to affect Shoes on Mac OS X where the program is developed.

# sample65.rb
Shoes.app :width => 400, :height => 50 do
  p1 = para
  p2 = para
  p3 = para
  p4 = para
  button('case 1'){self.clipboard = p1.text}                                #=> crash
  button('case 2'){self.clipboard = p2.text.eql?('') ? '' : p2.text }       #=> not crash
  button('case 3'){self.clipboard = p3.text.eql?('') ? p3.text : p3.text }  #=> crash
  button('case 4'){self.clipboard = p4.text.equal?('') ? '' : p4.text }     #=> crash
end

sample65.png

sample65.png

Policeman

  • The sample21.rb worked well.
  • The sample65.rb worked well. All buttons has no crash!!
  • The sample20.rb didn't work well and showed the following error.

sample20-1.png

sample20-1.png

So, I revised the code like this:

# sample20-1.rb
Shoes.app :title => 'Sorter', :width => 180, :height => 80 do
  background gradient powderblue, royalblue
  msg = para '', :size => 8

  yar = image('../images/yar.png', :left => 60, :top => 18).click do
    self.clipboard = self.clipboard.split.sort.join("\n") unless self.clipboard.nil?
    yar.transform :center
    a = animate(24) do |i|
      yar.rotate -15
      a.stop if i > 22
    end
  end
  yar.hover{msg.text = strong('Click Yar. She sorts clipboard text!')}
  yar.leave{msg.text = ''}
end

This works with both Raisins and Policeman.

Something went wrong with that request. Please try again.