I've cooked up a hack that pretty much works. It's implemented in terms of 
keypress events, relying on the fact that when you hold a key down, keypress 
events are sent every 0.1s or so (at most), except the keypress after the 
initial event/press, which takes 0.5 seconds to be sent (these figures are on 
my machine anyways, for a barebones shoes app, you can adjust values according 
to yours).

The code takes account of the pause after the initial event, with the 
disadvantage being that if you just press the button, it will assume it has 
been held for a full 0.505 seconds. Which may be totally acceptable for a lot 
of use cases. The other choice in this hack would be to wait for the rapid 
events, but that would make it feel very sluggish.

Also, I'm sure this could be abstracted out to look more like what you wanted, 
a function where you passed in the key, but I leave that as an exercise to 
someone who needs it. Here's the code:

Shoes.app do
  @info = para ''
  @s = stack
  @t = Time.now
  animate( 10 ) do
    if up_pressed?
      @info.replace "You pressed UP!!!"
      @s.append { para "up - ", Time.now.to_f%100, " - ", @t.to_f%100 } # Check 
these paras to see the reason for problems
    else
      @info.replace "C'mon, you know you wanna press the up button, just do it 
already"
    end
  end
  keypress do |key|
    if key.inspect == ':up' and Time.now - @t < 0.15
      # Continued press, where keypress events are fired off every ~0.1s
      @t = Time.now
    elsif key.inspect == ':up' and Time.now - @t >= 0.15
      # Initial press, which will followed by a second keypress event after 
~0.5s pause, if held down, hence the delay
      @t = Time.now + 0.505
    end
  end
  def up_pressed?
    # If the following statement is true, the up button is probably being held 
down, since the last even fired less than 0.15s ago
    Time.now - @t < 0.15
  end
end

And this code should provide the timing of keypress events, in case they're 
different for your hardware (also the events are slower if lots of things are 
going on I guess, so you'd have to adjust the 0.15 values):

Shoes.app do
  @t = Time.now
  keypress do |key|
    para "\n" if Time.now - @t > 0.15
    para Time.now - @t
    @t = Time.now
  end
end


> Subject: keydown?
> From: [email protected]
> To: [email protected]
> Date: Sat, 4 Jul 2009 10:16:27 -0700
> 
> I want to be able to do this:
> 
> kd = false
> animate 24 do
>     kd = keydown?(:up)
> end
> 
> Can we have keydown in some form or another?
> 
> -- 
>     ~devyn

_________________________________________________________________
Insert movie times and more without leaving HotmailĀ®. 
http://windowslive.com/Tutorial/Hotmail/QuickAdd?ocid=TXT_TAGLM_WL_HM_Tutorial_QuickAdd_062009

Reply via email to