### Home Classes Methods

### In Files

- lib/rubygame/joystick.rb

### Parent

Object

### Methods

- ::activate_all
- ::deactivate_all
- ::get_name
- ::new
- ::num_joysticks
- #axes
- #balls
- #buttons
- #hats
- #index
- #name

### 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::Joystick

The Joystick class interfaces with joysticks, gamepads, and other similar hardware devices used to play games. Each joystick may have zero or more axes, balls, hats, and/or buttons.

After a Joystick object is successfully created, events for that Joystick will begin appearing on the EventQueue when a button is pressed or released, a control stick is moved, etc.

You can use Joystick.activate_all to start receiving events for all joysticks (equivalent to creating them all individually with Joystick.new). You can use Joystick.deactivate_all to stop receiving events for all joysticks.

As of Rubygame 2.4, these are the current, "new-style" Joystick event classes:

- Events::JoystickAxisMoved

- Events::JoystickButtonPressed

- Events::JoystickButtonReleased

- Events::JoystickBallMoved

- Events::JoystickHatMoved

These old Joystick-related events are deprecated and will be removed in Rubygame 3.0:

- JoyAxisEvent

- JoyBallEvent

- JoyHatEvent

- JoyDownEvent

- JoyUpEvent

For more information about "new-style" events, see EventQueue.enable_new_style_events.

### Public Class Methods

activate_all() click to toggle source

Activate all joysticks on the system, equivalent to calling Joystick.new for every joystick available. This will allow joystick-related events to be sent to the EventQueue for all joysticks.

Returns | Array of zero or more Joysticks.
May raise | SDLError, if the joystick system could not be initialized.

    # File lib/rubygame/joystick.rb, line 82
    def self.activate_all
      # Initialize if it isn't already.
      if( SDL.WasInit(SDL::INIT_JOYSTICK) == 0 )
        if( SDL.Init(SDL::INIT_JOYSTICK) != 0 )
          raise Rubygame::SDLError, "Could not initialize SDL joysticks."
        end
      end

      # Collect Joystick instances in an Array
      joysticks = []

      num_joysticks.times do |i|
        joysticks << new( i )
      end

      return joysticks
    end

deactivate_all() click to toggle source

Deactivate all joysticks on the system. This will stop all joystick-related events from being sent to the EventQueue.

    # File lib/rubygame/joystick.rb, line 104
    def self.deactivate_all
      # Return right away if it isn't active
      return if( SDL.WasInit(SDL::INIT_JOYSTICK) == 0 )

      num_joysticks.times do |i|
        joy = SDL.JoystickOpen(i)
        unless( joy.pointer.nil? )
          SDL.JoystickClose( joy )
        end
      end

      return nil
    end

get_name( index ) click to toggle source

Returns the name of Nth joystick on the system. The name is implementation-dependent. See also name.

    # File lib/rubygame/joystick.rb, line 68
    def self.get_name( index )
      SDL.JoystickName( index )
    end

new( index ) click to toggle source

Create and initialize an interface to the Nth joystick on the system. Raises SDLError if the joystick could not be opened.

    # File lib/rubygame/joystick.rb, line 122
    def initialize( index )
      @struct = SDL.JoystickOpen( index )
      if( @struct.pointer.null? )
        raise( Rubygame::SDLError, "Could not open joystick %d: %s"%
               [index, SDL.GetError()] )
      end
    end

num_joysticks() click to toggle source

Returns the total number of joysticks detected on the system.

    # File lib/rubygame/joystick.rb, line 60
    def self.num_joysticks
      SDL.NumJoysticks()
    end

### Public Instance Methods

axes() click to toggle source

Returns the number of axes (singular: axis) featured on the Joystick. Each control stick generally has two axes (X and Y), although there are other types of controls which are represented as one or more axes.

    # File lib/rubygame/joystick.rb, line 153
    def axes
      SDL.JoystickNumAxes( @struct )
    end

balls() click to toggle source

Returns the number of trackballs featured on the Joystick. A trackball is usually a small sphere which can be rotated in-place in any direction, registering relative movement along two axes.

    # File lib/rubygame/joystick.rb, line 162
    def balls
      SDL.JoystickNumBalls( @struct )
    end

buttons() click to toggle source

Returns the number of buttons featured on the Joystick. A button can be in one of two states: neutral, or pushed.

    # File lib/rubygame/joystick.rb, line 179
    def buttons
      SDL.JoystickNumButtons( @struct )
    end

hats() click to toggle source

Returns the number of hats featured on the Joystick. A hat is a switch which can be pushed in one of several directions, or centered.

    # File lib/rubygame/joystick.rb, line 171
    def hats
      SDL.JoystickNumHats( @struct )
    end

index() click to toggle source

Returns the index number of the Joystick, i.e. the identifier number of the joystick that this interface controls. This is the same number that was given to Joystick.new.

    # File lib/rubygame/joystick.rb, line 135
    def index
      SDL.JoystickIndex( @struct )
    end

name() click to toggle source

Returns a String containing the name of the Joystick. The name is implementation-dependent. See also Joystick.get_name.

    # File lib/rubygame/joystick.rb, line 143
    def name
      SDL.JoystickName( self.index )
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.