class Rubygame::EventTriggers::KeyPressTrigger
KeyPressTrigger is an event trigger which fires when a key on the keyboard is pressed down (i.e. KeyPressed). See also KeyReleaseTrigger.
This trigger can be configured to fire for any key, or a specific key. It can also fire depending on which modifier keys are held (ctrl, shift, alt, etc.).
NOTE: This trigger only works with the new-style KeyPressed event class, not with the older KeyDownEvent. See Rubygame::EventQueue#enable_new_style_events
Public Class Methods
Initialize a new instance of KeyPressTrigger with the given key and modifier keys.
- key
-
the key symbol to detect, or :any (default) to detect any key. (Symbol, optional)
- mods
-
an Array of one or more modifier key symbols, or :none to detect key presses with exactly no modifiers, or :any (default) to detect any key modifiers.
Valid modifiers are:
-
:alt, :left_alt, :right_alt,
-
:ctrl, :left_ctrl, :right_ctrl,
-
:shift, :left_shift, :right_shift,
-
:meta, :left_meta, :right_meta,
-
:numlock
-
:capslock
-
:mode
:alt, :ctrl, :shift, and :meta will match either the left version or right version (e.g. :left_alt or :right_alt).
-
Example:
# Matches any key press, regardless of the key or modifiers. KeyPressTrigger.new # Matches the 'A' key with any (or no) modifiers. KeyPressTrigger.new( :a ) # Matches the 'A' with both Ctrl and Shift modifiers. KeyPressTrigger.new( :a, [:ctrl, :shift] ) # Matches the 'A' with both Left Ctrl and Left Shift modifiers. KeyPressTrigger.new( :a, [:left_ctrl, :left_shift] )
# File lib/rubygame/event_triggers.rb, line 357 def initialize( key=:any, mods=:any ) @key = key @mods = mods end
Public Instance Methods
Returns true if the event is a KeyPressed event and the event's key and mods BOTH match the trigger's expectations.
Key matches if either of these is true:
-
the trigger's key is the symbol :any
-
the event's key is the same as the trigger's key
Modifiers matches if any of these is true:
-
the trigger's @mods is the symbol :any
-
the event has no modifiers and the trigger's @mods is the symbol :none
-
every one of the trigger's @mods matches one of the event's modifiers. “Matches” means either it is the same symbol, or it is a more general version. For example, :alt will match either :left_alt or :right_alt.
# File lib/rubygame/event_triggers.rb, line 378 def match?( event ) if event.kind_of?( Rubygame::Events::KeyPressed ) ((@key == :any) or (event.key == @key)) and ((@mods == :any) or (@mods == :none and event.modifiers == []) or (_mods_match?(event.modifiers))) end end