### Home Classes Methods

### In Files

- lib/rubygame/event_triggers.rb

### Parent

Object

### Methods

- ::new
- #match?

### Files

- CREDITS
- LICENSE
- NEWS
- README
- ROADMAP
- custom_sdl_load_paths.rdoc
- getting_started.rdoc
- keyboard_symbols.rdoc
- macosx_install.rdoc
- managing_framerate.rdoc
- surface_palettes.rdoc
- windows_install.rdoc

### Class/Module Index

Quicksearch

- Rubygame
- Rubygame::ActiveEvent
- Rubygame::Clock
- Rubygame::Color
- Rubygame::Color::ColorBase
- Rubygame::Color::ColorHSL
- Rubygame::Color::ColorHSV
- Rubygame::Color::ColorRGB
- Rubygame::Color::Palette
- Rubygame::Event
- Rubygame::EventActions
- Rubygame::EventActions::BlockAction
- Rubygame::EventActions::MethodAction
- Rubygame::EventActions::MultiAction
- Rubygame::EventHandler
- Rubygame::EventHandler::HasEventHandler
- Rubygame::EventHook
- Rubygame::EventQueue
- Rubygame::EventTriggers
- Rubygame::EventTriggers::AndTrigger
- Rubygame::EventTriggers::AttrTrigger
- Rubygame::EventTriggers::BlockTrigger
- Rubygame::EventTriggers::InstanceOfTrigger
- Rubygame::EventTriggers::KeyPressTrigger
- Rubygame::EventTriggers::KeyReleaseTrigger
- Rubygame::EventTriggers::KindOfTrigger
- Rubygame::EventTriggers::MouseMoveTrigger
- Rubygame::EventTriggers::MousePressTrigger
- Rubygame::EventTriggers::MouseReleaseTrigger
- Rubygame::EventTriggers::OrTrigger
- Rubygame::EventTriggers::TickTrigger
- Rubygame::EventTriggers::YesTrigger
- Rubygame::Events
- Rubygame::Events::ClockTicked
- Rubygame::Events::InputFocusGained
- Rubygame::Events::InputFocusLost
- Rubygame::Events::JoystickAxisMoved
- Rubygame::Events::JoystickBallMoved
- Rubygame::Events::JoystickButtonEvent
- Rubygame::Events::JoystickButtonPressed
- Rubygame::Events::JoystickButtonReleased
- Rubygame::Events::JoystickHatMoved
- Rubygame::Events::KeyPressed
- Rubygame::Events::KeyReleased
- Rubygame::Events::KeyboardEvent
- Rubygame::Events::MouseButtonEvent
- Rubygame::Events::MouseFocusGained
- Rubygame::Events::MouseFocusLost
- Rubygame::Events::MouseMoved
- Rubygame::Events::MousePressed
- Rubygame::Events::MouseReleased
- Rubygame::Events::QuitRequested
- Rubygame::Events::WindowExposed
- Rubygame::Events::WindowMinimized
- Rubygame::Events::WindowResized
- Rubygame::Events::WindowUnminimized
- Rubygame::ExposeEvent
- Rubygame::Ftor
- Rubygame::GL
- Rubygame::Hotspot
- Rubygame::JoyAxisEvent
- Rubygame::JoyBallEvent
- Rubygame::JoyDownEvent
- Rubygame::JoyHatEvent
- Rubygame::JoyUpEvent
- Rubygame::Joystick
- Rubygame::Key
- Rubygame::KeyDownEvent
- Rubygame::KeyUpEvent
- Rubygame::MailQueue
- Rubygame::MediaBag
- Rubygame::Mixer
- Rubygame::Mixer::Music
- Rubygame::Mixer::Sample
- Rubygame::Mouse
- Rubygame::MouseDownEvent
- Rubygame::MouseMotionEvent
- Rubygame::MouseUpEvent
- Rubygame::Music
- Rubygame::NamedResource
- Rubygame::NamedResource::NamedResourceClassMethods
- Rubygame::QuitEvent
- Rubygame::Rect
- Rubygame::ResizeEvent
- Rubygame::SDLError
- Rubygame::SFont
- Rubygame::Screen
- Rubygame::Sound
- Rubygame::Sprites
- Rubygame::Sprites::DepthSortGroup
- Rubygame::Sprites::Group
- Rubygame::Sprites::LimitGroup
- Rubygame::Sprites::Sprite
- Rubygame::Sprites::UpdateGroup
- Rubygame::Surface
- Rubygame::TTF

No matching classes.

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

### Public Class Methods

new( key=:any, mods=:any ) click to toggle source

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

match?( event ) click to toggle source

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

[Validate]

Generated with the Darkfish Rdoc Generator 2.