API Reference

Class List

pc.Mouse

Extends: pc.EventHandler

A Mouse Device, bound to a DOM Element.

Summary

Static Methods

isPointerLockedCheck if the mouse pointer has been locked, using pc.Mouse#enabledPointerLock

Methods

attachAttach mouse events to an Element.
detachRemove mouse events from the element that it is attached to
disableContextMenuDisable the context menu usually activated with right-click
disablePointerLockReturn control of the mouse cursor to the user
enableContextMenuEnable the context menu usually activated with right-click.
enablePointerLockRequest that the browser hides the mouse cursor and locks the mouse to the element.
isPressedReturns true if the mouse button is currently pressed
updateUpdate method, should be called once per frame
wasPressedReturns true if the mouse button was pressed this frame (since the last call to update).
wasReleasedReturns true if the mouse button was released this frame (since the last call to update).

Events

mousedownFired when a mouse button is pressed
mousemoveFired when the mouse is moved
mouseupFired when a mouse button is released
mousewheelFired when a mouse wheel is moved

Inherited

Methods

fireFire an event, all additional arguments are passed on to the event listener
hasEventTest if there are any handlers bound to an event name
offDetach an event handler from an event.
onAttach an event handler to an event
onceAttach an event handler to an event.

Details

Static Methods

isPointerLocked()

Check if the mouse pointer has been locked, using pc.Mouse#enabledPointerLock

Returns

Boolean True if locked

Constructor

Mouse([element])

Create a new Mouse device

Parameters

elementElementThe Element that the mouse events are attached to

Methods

attach(element)

Attach mouse events to an Element.

Parameters

elementElementThe DOM element to attach the mouse to.

detach()

Remove mouse events from the element that it is attached to

disableContextMenu()

Disable the context menu usually activated with right-click

disablePointerLock([success])

Return control of the mouse cursor to the user

Parameters

successpc.callbacks.LockMouseFunction called when the mouse lock is disabled

enableContextMenu()

Enable the context menu usually activated with right-click. This option is active by default.

enablePointerLock([success], [error])

Request that the browser hides the mouse cursor and locks the mouse to the element. Allowing raw access to mouse movement input without risking the mouse exiting the element. Notes:

  • In some browsers this will only work when the browser is running in fullscreen mode. See pc.Application#enableFullscreen
  • Enabling pointer lock can only be initiated by a user action e.g. in the event handler for a mouse or keyboard input.

Parameters

successpc.callbacks.LockMouseFunction called if the request for mouse lock is successful.
errorpc.callbacks.LockMouseFunction called if the request for mouse lock is unsuccessful.

isPressed(button)

Returns true if the mouse button is currently pressed

Parameters

buttonNumberThe mouse button to test. Can be:

Returns

Boolean True if the mouse button is current pressed

update()

Update method, should be called once per frame

wasPressed(button)

Returns true if the mouse button was pressed this frame (since the last call to update).

Parameters

buttonNumberThe mouse button to test. Can be:

Returns

Boolean True if the mouse button was pressed since the last update

wasReleased(button)

Returns true if the mouse button was released this frame (since the last call to update).

Parameters

buttonNumberThe mouse button to test. Can be:

Returns

Boolean True if the mouse button was released since the last update

Events

mousedown

Fired when a mouse button is pressed

Parameters

eventpc.MouseEventThe MouseEvent object

mousemove

Fired when the mouse is moved

Parameters

eventpc.MouseEventThe MouseEvent object

mouseup

Fired when a mouse button is released

Parameters

eventpc.MouseEventThe MouseEvent object

mousewheel

Fired when a mouse wheel is moved

Parameters

eventpc.MouseEventThe MouseEvent object

Inherited

Methods

fire(name, [arg1], [arg2], [arg3], [arg4], [arg5], [arg6], [arg7], [arg8])

Fire an event, all additional arguments are passed on to the event listener

obj.fire('test', 'This is the message');

Parameters

nameObjectName of event to fire
arg1*First argument that is passed to the event handler
arg2*Second argument that is passed to the event handler
arg3*Third argument that is passed to the event handler
arg4*Fourth argument that is passed to the event handler
arg5*Fifth argument that is passed to the event handler
arg6*Sixth argument that is passed to the event handler
arg7*Seventh argument that is passed to the event handler
arg8*Eighth argument that is passed to the event handler

Returns

pc.EventHandler Self for chaining.

hasEvent(name)

Test if there are any handlers bound to an event name

obj.on('test', function () { }); // bind an event to 'test'
obj.hasEvent('test'); // returns true
obj.hasEvent('hello'); // returns false

Parameters

nameStringThe name of the event to test

Returns

Boolean true if the object has handlers bound to the specified event name.

off([name], [callback], [scope])

Detach an event handler from an event. If callback is not provided then all callbacks are unbound from the event, if scope is not provided then all events with the callback will be unbound.

var handler = function () {
};
obj.on('test', handler);

obj.off(); // Removes all events
obj.off('test'); // Removes all events called 'test'
obj.off('test', handler); // Removes all handler functions, called 'test'
obj.off('test', handler, this); // Removes all hander functions, called 'test' with scope this

Parameters

nameStringName of the event to unbind
callbackpc.callbacks.HandleEventFunction to be unbound.
scopeObjectScope that was used as the this when the event is fired

Returns

pc.EventHandler Self for chaining.

on(name, callback, [scope])

Attach an event handler to an event

obj.on('test', function (a, b) {
    console.log(a + b);
});
obj.fire('test', 1, 2); // prints 3 to the console

Parameters

nameStringName of the event to bind the callback to
callbackpc.callbacks.HandleEventFunction that is called when event is fired. Note the callback is limited to 8 arguments.
scopeObjectObject to use as 'this' when the event is fired, defaults to current this

Returns

pc.EventHandler Self for chaining.

once(name, callback, [scope])

Attach an event handler to an event. This handler will be removed after being fired once.

obj.once('test', function (a, b) {
    console.log(a + b);
});
obj.fire('test', 1, 2); // prints 3 to the console
obj.fire('test', 1, 2); // not going to get handled

Parameters

nameStringName of the event to bind the callback to
callbackpc.callbacks.HandleEventFunction that is called when event is fired. Note the callback is limited to 8 arguments.
scopeObjectObject to use as 'this' when the event is fired, defaults to current this

Returns

pc.EventHandler Self for chaining.