Aria  2.8.0
ArKeyHandler Class Reference

Perform actions when keyboard keys are pressed. More...

#include <ArKeyHandler.h>

Public Types

enum  KEY {
  UP = 256, DOWN, LEFT, RIGHT,
  ESCAPE, SPACE, TAB, ENTER,
  BACKSPACE, _StartFKeys, F1, F2,
  F3, F4, F5, F6,
  F7, F8, F9, F10,
  F11, F12, _EndFKeys, PAGEUP,
  PAGEDOWN, HOME, END, INSERT,
  DEL
}
 These are symbols for the non-ascii keys. More...
 

Public Member Functions

bool addKeyHandler (int keyToHandle, ArFunctor *functor)
 This adds a keyhandler, when the keyToHandle is hit, functor will fire. More...
 
 ArKeyHandler (bool blocking=false, bool addAriaExitCB=true, FILE *stream=NULL, bool takeKeysInConstructor=true)
 This object will take over key capture when constructed, and release key capture when destroyed. More...
 
void checkKeys (void)
 intnernal, use addKeyHandler, Checks for keys and handles them
 
int getKey (void)
 internal, use addKeyHandler instead... More...
 
bool remKeyHandler (int keyToHandler)
 This removes a key handler, by key. More...
 
bool remKeyHandler (ArFunctor *functor)
 This removes a key handler, by key. More...
 
void restore (void)
 Sets stdin back to its original settings, if its been restored it won't read anymore. More...
 
void takeKeys (bool blocking=false)
 Takes the key control over. More...
 
 ~ArKeyHandler ()
 Destructor. More...
 

Protected Member Functions

int getChar (void)
 

Protected Attributes

ArFunctorC< ArKeyHandlermyAriaExitCB
 
bool myBlocking
 
std::map< int, ArFunctor * > myMap
 
struct termios myOriginalTermios
 
bool myRestored
 
FILE * myStream
 
bool myTookKeys
 

Detailed Description

Perform actions when keyboard keys are pressed.

This class is used for handling input from the keyboard. Use addKeyHandler() to associate a functor with a keyboard key. ArKeyHandler will call that functor when the key is pressed.

You should also register the keyhandler with Aria::setKeyHandler(), and before you create a key handler you should see if one is already there with Aria::getKeyHandler(). Only one key handler can be created in a program, and this lets independent parts of a program use the same key handler.

You can attach a key handler to a robot with ArRobot::attachKeyHandler() which will put a task into the robots list of tasks to check for new keyboard input in each cycle, and which will also add a handler to end the robot's task cycle when Escape is pressed (you can override this by replacing it with your own handler). If you have mulitple ArRobot objects, only create one key handler and attach it to one robot.

Alternatively, you can call checkKeys() periodically to check for new key input.

Examples:
actionGroupExample.cpp, actsColorFollowingExample.cpp, demo.cpp, dpptuExample.cpp, gotoActionExample.cpp, gripperExample.cpp, gyroExample.cpp, imuExample.cpp, lineFinderExample.cpp, triangleDriveToActionExample.cpp, and wander.cpp.

Member Enumeration Documentation

◆ KEY

These are symbols for the non-ascii keys.

Enumerator
UP 

Up arrow (keypad or 4 key dirs)

DOWN 

Down arrow (keypad or 4 key dirs)

LEFT 

Left arrow (keypad or 4 key dirs)

RIGHT 

Right arrow (keypad or 4 key dirs)

ESCAPE 

Escape key.

SPACE 

Space key.

TAB 

Tab key.

ENTER 

Enter key.

BACKSPACE 

Backspace key.

_StartFKeys 

F key offset (internal; don't use).

F1 

F1.

F2 

F2.

F3 

F3.

F4 

F4.

F5 

F5 (not supported on Windows yet)

F6 

F6 (not supported on Windows yet)

F7 

F7 (not supported on Windows yet)

F8 

F8 (not supported on Windows yet)

F9 

F9 (not supported on Windows yet)

F10 

F10 (not supported on Windows yet)

F11 

F11 (not supported on Windows yet)

F12 

F12 (not supported on Windows yet)

_EndFKeys 

F key range (internal; don't use)

PAGEUP 

Page Up (not supported on Windows yet)

PAGEDOWN 

Page Down (not supported on Windows yet)

HOME 

Home key (not supported on Windows yet)

END 

End key (not supported on Windows yet)

INSERT 

Insert key (not supported on Windows yet)

DEL 

Special delete key (often forward-delete) (not supported on Windows yet)

Constructor & Destructor Documentation

◆ ArKeyHandler()

ArKeyHandler::ArKeyHandler ( bool  blocking = false,
bool  addAriaExitCB = true,
FILE *  stream = NULL,
bool  takeKeysInConstructor = true 
)

This object will take over key capture when constructed, and release key capture when destroyed.

Parameters
blockingwhether or not to block waiting on keys, default is false, ie not to wait... you probably only want to block if you are using checkKeys yourself like after you start a robot run or in its own thread or something along those lines
addAriaExitCBtrue to add an aria exit cb to restore the keys
streamthe FILE * pointer to use, if this is NULL (the default) then use stdin, otherwise use this...
takeKeysInConstructorwhether to take the keys when created or not (default is true)

◆ ~ArKeyHandler()

ArKeyHandler::~ArKeyHandler ( )

Destructor.

Reseases control of the keyboard and restores state before this key handler was created.

Member Function Documentation

◆ addKeyHandler()

bool ArKeyHandler::addKeyHandler ( int  keyToHandle,
ArFunctor functor 
)

This adds a keyhandler, when the keyToHandle is hit, functor will fire.

Parameters
keyToHandleA character value, such as 'a' or '1' or '[', or a member of the KEY enum.
functorFunctor to call when keyToHandle is received
Returns
true If no previous key handler functor exists for keyToHandle and the handler functor was stored, or false if a handler for that key already exists. that key
Examples:
actionGroupExample.cpp, actsColorFollowingExample.cpp, dpptuExample.cpp, gripperExample.cpp, gyroExample.cpp, lineFinderExample.cpp, and triangleDriveToActionExample.cpp.

◆ getKey()

int ArKeyHandler::getKey ( void  )

internal, use addKeyHandler instead...

Gets a key from the stdin if ones available, -1 if there aren't any available

◆ remKeyHandler() [1/2]

bool ArKeyHandler::remKeyHandler ( int  keyToHandle)

This removes a key handler, by key.

Parameters
keyToHandleThe character value or code to clear the handler for.
Returns
true if the remKeyHandler succeeded, which means that a key handler for keyToHandle was found and rmeoved, or false if no handler for that value was found.

◆ remKeyHandler() [2/2]

bool ArKeyHandler::remKeyHandler ( ArFunctor functor)

This removes a key handler, by key.

Parameters
functorthe functor of the handler to remove
Returns
true if the remKeyHandler succeeded, which means that the functor was found and removed from the handlers, or false if no handler with the given functor was found.

◆ restore()

void ArKeyHandler::restore ( void  )

Sets stdin back to its original settings, if its been restored it won't read anymore.

For internal or special use, since it's called in the destructor.

Examples:
actsColorFollowingExample.cpp, and imuExample.cpp.

◆ takeKeys()

void ArKeyHandler::takeKeys ( bool  blocking = false)

Takes the key control over.

For internal or special use, since it's called in the constructor.


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