Confirmed users
656
edits
No edit summary |
|||
| Line 16: | Line 16: | ||
This API introduces new events on the window object for reading joystick and controller (hereby referred to as joystick) state. | This API introduces new events on the window object for reading joystick and controller (hereby referred to as joystick) state. | ||
=== Connecting to a Joystick === | |||
When a new joystick is connected to the computer, the focused page will first receive a '''MozJoyConnected''' event. If a joystick was already connected when the page loaded, the '''MozJoyConnected''' event will be dispatched to the focused page when the user presses a button or moves an axis. Developers can use '''MozJoyConnected''' like this: | When a new joystick is connected to the computer, the focused page will first receive a '''MozJoyConnected''' event. If a joystick was already connected when the page loaded, the '''MozJoyConnected''' event will be dispatched to the focused page when the user presses a button or moves an axis. Developers can use '''MozJoyConnected''' like this: | ||
| Line 36: | Line 36: | ||
Each joystick will have a unique ID associated with it, which is available on the event's '''joystick''' property. | Each joystick will have a unique ID associated with it, which is available on the event's '''joystick''' property. | ||
=== Disconnecting a Joystick === | |||
When a joystick is disconnected, and if a page has previously received data (e.g., MozJoyConnected), a second event is dispatched to the focused window, '''MozJoyDisconnected''': | When a joystick is disconnected, and if a page has previously received data (e.g., MozJoyConnected), a second event is dispatched to the focused window, '''MozJoyDisconnected''': | ||
| Line 75: | Line 75: | ||
This previous example also demonstrates how the '''joystick''' property can be held after the event has completed--a technique we will use for device state querying later. | This previous example also demonstrates how the '''joystick''' property can be held after the event has completed--a technique we will use for device state querying later. | ||
=== Joystick Button Events === | |||
Joysticks can have one or more buttons, and similar to a mouse button, this API provides events for buttons being pressed and released. When a joystick button is pressed a '''MozJoyButtonDown''' event is dispatched to the currently focused page. Similarly a '''MozJoyButtonUp''' event is dispatched when it is released. Both events provide the same '''joystick''' property, which indicates the joystick (i.e., it's ID) that triggered the event. The button itself (i.e., whichever of the 2, 4, etc. buttons the joystick has) is available on the event's '''button''' property. | Joysticks can have one or more buttons, and similar to a mouse button, this API provides events for buttons being pressed and released. When a joystick button is pressed a '''MozJoyButtonDown''' event is dispatched to the currently focused page. Similarly a '''MozJoyButtonUp''' event is dispatched when it is released. Both events provide the same '''joystick''' property, which indicates the joystick (i.e., it's ID) that triggered the event. The button itself (i.e., whichever of the 2, 4, etc. buttons the joystick has) is available on the event's '''button''' property. | ||
| Line 96: | Line 96: | ||
</pre> | </pre> | ||
=== Joystick Axis Events === | |||
Similar to the button events, the axis events provide a way for developers to know when a user has moved one or more of a joystick's axises (i.e., a "stick"). Just as a joystick can have multiple buttons, so to can there be many axes. The '''MozJoyAxisMove''' event indicates that a joystick's axis has changed, and its new value. The axis is numbered, and its value is given, which is a float between -1.0 (the lowest possible value) and 1.0 (the highest possible value): | Similar to the button events, the axis events provide a way for developers to know when a user has moved one or more of a joystick's axises (i.e., a "stick"). Just as a joystick can have multiple buttons, so to can there be many axes. The '''MozJoyAxisMove''' event indicates that a joystick's axis has changed, and its new value. The axis is numbered, and its value is given, which is a float between -1.0 (the lowest possible value) and 1.0 (the highest possible value): | ||
| Line 116: | Line 116: | ||
</pre> | </pre> | ||
=== Querying the Joystick Object === | |||
All of the '''MozJoy*''' events discussed above included a '''joystick''' property on the event object. We used this in order to determine which joystick (i.e., it's ID) had caused the event, since multiple joysticks might be connected at once. | All of the '''MozJoy*''' events discussed above included a '''joystick''' property on the event object. We used this in order to determine which joystick (i.e., it's ID) had caused the event, since multiple joysticks might be connected at once. | ||