Keyboard

Keyboard is used for getting input from user. It connects to keyboard and mouse keys. Keyboard has also some functions to slow the program in order to make it more user friendly.

Table of Functions

Information

Keyboard keys are integer constants. You can use them as characters 'C' or use virtual keys. Virtual keys are used without quotations e. g. VK_RETURN. They are defined by windows and can be found at microsoft docs. The following table summarizes the most commonly used virtual keys.

Virtual Key Description Virtual Key Description
VK_LBUTTON Left mouse button VK_SPACE Spacebar
VK_RBUTTON Right mouse button VK_SHIFT Shift key
VK_MBUTTON Middle mouse button VK_CONTROL Control key
VK_UP Up arrow key VK_MENU Alt key
VK_LEFT Left arrow key VK_CAPITAL Caps Lock key
VK_RIGHT Right arrow key VK_RETURN Enter key
VK_DOWN Down arrow key VK_ESCAPE Escape key

Following examples show basic usage of the keyboard object.

if (Keyboard.getPressed(VK_UP)) // UP arrow was pressed
while (Keyboard.get('A')) // While 'A' key is down
if (Keyboard.getReleased('1')) // '1' key is released
Keyboard.wait(30); // slows program to 30 FPS

Functions

bool Keyboard.get(int key)

This function tests if a keyboard key is currently down. It outputs true as long as the key is down.

You can use a Keyboard.wait() to slow down a while statement.

Parameters

int - any virtual key or character.

Return value

Return type is bool. Return value is true, when key is currently down. Otherwise is false.

Example

First example gets current input of 'W' key. The second example is while loop that loops until the spacebar is pressed.

if (Keyboard.get('W')) { ... }
while (!Keyboard.get(VK_SPACE)) { ... }

bool Keyboard.getPressed(int key)

This function checks if a key was pressed since the last call of Keyboard.wait(). Keyboard getPressed outputs true only once when key is pressed. Afterwards output becomes false regardless of the key state.

To check current state of the keyboard use Keyboard.get() instead.

Parameters

int - any virtual key or character.

Return value

Return type is bool. Return value is true, once when the key is pressed. Otherwise is false.

Example

This function is useful when detecting single click on the mouse. Following code checks when mouse left button is pressed and then it prints a red 'X' on the mouse position using a Cursor.

while(!Keyboard.get(VK_ESCAPE))
{
  if (Keyboard.getPressed(VK_LBUTTON))
  {
    int mx = Mouse.getColumns();
    int my = Mouse.getRows();
    Cursor.printChar(mx,my,'X',LIGHTRED);
  }
}

bool Keyboard.getReleased(int key)

This function checks if a key was released since the last call of Keyboard.wait(). Keyboard getReleased outputs true only once when key is released. Afterwards output becomes false regardless of the key state.

To check current state of the keyboard use Keyboard.get() instead.

Parameters

int - any virtual key or character.

Return value

Return type is bool. Return value is true, once when the key is released. Otherwise is false.

Example

This function can be used when clicking on menu buttons. Only when a key button is released the desired action will be executed. User can change their mind in the last moment.

In the following example a code checks if an ENTER key was pressed. Then it clears the console and based on a custom integer variable position chooses which action to take.

if (Keyboard.getReleased(VK_RETURN))
{
  Cursor.clearScreen();
  if (position == 1)
    std::cout<<"Starting a new project";
  else if (position == 2)
    std::cout<<"Loading a saved project";
}

bool Keyboard.wait(unsigned int ticks)

This function slow downs program to specific number of ticks per second. It is also used for functions Keyboard.getPressed and Keyboard.getReleased.

If you want to stop the program until user presses a key use Keyboard.waitUser() instead.

This function should be used in while statements to slow down their fast repeats. Furthermore, this function should be the last function in the while statement.

Parameters

int - ticks per second that program should have. Passing a 0 doesn't stop program but will update keyboard functions.

Return value

Return type is bool. The output is true if all the functions were executed correctly. When error occurs return value is false.

Example

It can be used in games to slow down movement of player on the screen. It can also be used in menus to slow down fast scrolling using keyboard keys.

This example changes x and y coordinates 30 times per second.

while(!Keyboard.get(VK_ESCAPE))
{
  if (keyboard.getPressed(VK_LEFT))  { x -= 10; }
  if (keyboard.getPressed(VK_RIGHT)) { x += 10; }
  if (keyboard.getPressed(VK_UP))    { y -= 10; }
  if (keyboard.getPressed(VK_DOWN))  { y += 10; }
  Keyboard.wait(30);
}

bool Keyboard.waitUser()

This function stops the application until user presses some button. Releasing a button doesn't trigger this function.

If you want to slow down program to specific number of ticks per second use Keyboard.wait() instead.

This function is used to stop a program from continuing until a user presses a button.

Parameters

This function doesn't have any parameters.

Return value

Return type is bool. The output is true if all the functions were executed correctly. When error occurs return value is false.

Example Following example writes information on screen and waits until user presses any button. After pressing a button Cursor clears the screen.

std::cout << "That's all from the keyboard object!" << std::endl;
std::cout << "Press any key to continue..." << std::endl;
Keyboard.waitUser();
Cursor.clearScreen();