Class | Rubygame::EventTriggers::KeyPressTrigger
In: | lib/rubygame/event_triggers.rb
Parent: | Object

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 EventQueue#enable_new_style_events

### Methods

match? new

### Public Class methods

new( key=:any, mods=:any )

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] )

### Public Instance methods

match?( event )

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.

[Validate]