Cornell Cocos
Cornell Extensions to Cocos2d
Public Member Functions | Static Public Member Functions | Public Attributes | Protected Attributes | Static Protected Attributes | List of all members
KeyboardPoller Class Reference

#include <CUKeyboardPoller.h>

Public Member Functions

bool isActive () const
 
void update ()
 
bool keyDown (EventKeyboard::KeyCode code) const
 
bool keyPressed (EventKeyboard::KeyCode code) const
 
bool keyReleased (EventKeyboard::KeyCode code)
 
int keyCount () const
 
const std::vector< EventKeyboard::KeyCode > keySet () const
 
 ~KeyboardPoller ()
 
void init (int priority)
 
void init (Node *node)
 
void dispose ()
 

Static Public Member Functions

static void start (int priority)
 
static void start (Node *node)
 
static void stop ()
 
static KeyboardPollergetInstance ()
 

Public Attributes

CC_CONSTRUCTOR_ACCESS __pad0__: KeyboardPoller()
 

Protected Attributes

EventListenerKeyboard * _keybdListener
 
bool _active
 
std::unordered_set< EventKeyboard::KeyCode, KeyCodeHasher_previous
 
std::unordered_set< EventKeyboard::KeyCode, KeyCodeHasher_current
 
std::unordered_map< EventKeyboard::KeyCode, bool, KeyCodeHasher_changed
 

Static Protected Attributes

static KeyboardPoller_gKeyboard = nullptr
 

Detailed Description

Class to providing a polling interface for the Keyboard.

Cocos2d decided to make all of its input event-driven. This is a major problem, because it is hard to associate the events with the current animation frame, potentially causing lag. A poller is an alternative input interface that allows us to query the current state of the keyboard when we need it.

Because there is (theoretically) only one keyboard, this class provides a singleton interface. You can start and stop the interface with static methods.

Constructor & Destructor Documentation

KeyboardPoller::~KeyboardPoller ( )

Disposes of this input controller, releasing all listeners.

Member Function Documentation

void KeyboardPoller::dispose ( )

Stops this listener, so that it no longer receives events

KeyboardPoller * KeyboardPoller::getInstance ( )
static

Returns the singleton interface for the keyboard poller.

It is unsafe to make your own keyboard poller. Just use this static method to access the singleton object.

This method will return nullptr if the poller is not yet started.

void KeyboardPoller::init ( int  priority)

Initializes this listener, registering it to receive events.

The listener has fixed priority.

Parameters
priorityThe input priority for the listener.
void KeyboardPoller::init ( Node *  node)

Initializes this listener, registering it to receive events.

The listener has scene priority.

Parameters
nodeThe node for scene priority.
bool KeyboardPoller::isActive ( ) const
inline

Returns whether this listener is active and receiving events

Returns whether this listener is active and receiving events

int KeyboardPoller::keyCount ( ) const
inline

Returns the number of keys currently held down.

Returns
the number of keys currently held down.
bool KeyboardPoller::keyDown ( EventKeyboard::KeyCode  code) const
inline

Returns true if the key is currently held down.

Parameters
codethe keyboard key to test
Returns
true if the key is currently held down.
bool KeyboardPoller::keyPressed ( EventKeyboard::KeyCode  code) const
inline

Returns true if the key was pressed this animation frame.

A key press occurs if the key is down this animation frame, but was not down the previous animation frame.

Parameters
codethe keyboard key to test
Returns
true if the key is currently held down.
bool KeyboardPoller::keyReleased ( EventKeyboard::KeyCode  code)
inline

Returns true if the key was released this animation frame.

A key press occurs if the key is up this animation frame, but was not up the previous animation frame.

Parameters
codethe keyboard key to test
Returns
true if the key is currently held down.
const std::vector< EventKeyboard::KeyCode > KeyboardPoller::keySet ( ) const

Returns a list of the keys currently held down.

This list contains the codes for all of the keys currently held down. This list is a copy; modifying it has not effect on the poller.

Returns
a list of the keys currently held down.
void KeyboardPoller::start ( int  priority)
static

Starts the input processing for this poller.

This method will activate the singleton and assign it a priority. The listener has fixed priority.

Parameters
priorityThe input priority for the keyboard.
void KeyboardPoller::start ( Node *  node)
static

Starts the input processing for this poller.

This method will activate the singleton and assign it a priority. The listener has scene priority.

Parameters
nodeThe node for scene priority.
void KeyboardPoller::stop ( )
static

Stop the input processing for this input controller.

This will deallocate the singleton, freeing memory.

void KeyboardPoller::update ( )

Gather the input for the current animation frame.

This method is used to take any cached key events and add them to the poller state. This should be called just before any state queries in the current animation frame.

Member Data Documentation

bool KeyboardPoller::_active
protected

Whether this listener is active and receiving events

std::unordered_map<EventKeyboard::KeyCode,bool,KeyCodeHasher> KeyboardPoller::_changed
protected

The keys that will change in the next animation frame

std::unordered_set<EventKeyboard::KeyCode,KeyCodeHasher> KeyboardPoller::_current
protected

The currently pressed keys

NS_CC_BEGIN KeyboardPoller * KeyboardPoller::_gKeyboard = nullptr
staticprotected

Singleton keyboard controller

Initialization of static reference

EventListenerKeyboard* KeyboardPoller::_keybdListener
protected

Listener to process keyboard events

std::unordered_set<EventKeyboard::KeyCode,KeyCodeHasher> KeyboardPoller::_previous
protected

The previously pressed keys


The documentation for this class was generated from the following files: