Mouse

Mouse object is used to get position of mouse in screen.

Table of Functions

Information

Mouse cursor position is in pixels. However this is not usually information you want when creating text interface. Therefore you can use handy function getRows and getColumns to get character position of mouse. Doing it this way, you can create applications and games that designed for any monitor screen size.

SWTI Mouse is used only to detect position of mouse. To detect a mouse keys use keyboard object and virtual keys VK_LBUTTON, VK_RBUTTON and VK_MBUTTON.

Mouse cursor is not the console cursor. You can not set mouse position. You can use mouse as following example. It is a simple drawing game. If left mouse button is pressed it writes on mouse character position letter M.

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

Functions

int Mouse.getX()

This function returns relative position of mouse cursor in console window. The position is in pixels. The position in pixels changes depending on screen size and screen position. To get better result use Mouse.getColumns().

Function returns values from 0 to screen width. If mouse is outside the console window on the left, function returns negative numbers. When mouse is outside the console window on the right, function will return numbers larger than the screen width.

Parameters

This function doesn't have any parameters.

Return value

Return type is int. It is the X coordinate of mouse cursor in pixels relative to console window.

Example

This example writes simple sentence about the mouse cursor position in the window.

std::cout << "Mouse is in the ";
if (Mouse.getX() < 0) std::cout << "left to the ";
if (Mouse.getX() > Window.getWidth()) std::cout << "right to the ";
std::cout << " window!" << std::endl;

int Mouse.getY()

This function returns relative position of mouse cursor in console window. The position is in pixels. The position in pixels changes depending on screen size and screen position. To get better result use Mouse.getRows().

Function returns values from 0 to screen height. If mouse is outside the console window from above, function returns negative numbers. When mouse is outside the console window in the bottom, function will return numbers larger than the screen height.

Parameters

This function doesn't have any parameters.

Return value

Return type is int. It is the Y coordinate of mouse cursor in pixels relative to console window.

Example

This example writes simple sentence about the mouse cursor position in the window.

std::cout << "Mouse is in the ";
if (Mouse.getY() < 0) std::cout << "above of the ";
if (Mouse.getY() > Window.getHeight()) std::cout << "bottom of the ";
std::cout << "window!" << std::endl;

int Mouse.getColumns()

This function returns character position of mouse cursor in console window. The position is in columns. Columns are single characters in console. Using this function, you can easily determine position of mouse cursor on any device.

Function returns values from 0 to screen columns. If mouse is outside the console window on the left, function returns negative numbers. When mouse is outside the console window on the right, function will return numbers larger than the screen columns.

Parameters

This function doesn't have any parameters.

Return value

Return type is int. It is the X coordinate of mouse cursor in characters.

Example

This example writes a symbol on screen and slowly moves it to the cursor. Put this example in a loop and define integer mpos to some arbitrary value.

if (mpos < Mouse.getColumns())
  {Cursor.printBlank(mpos, 10); mpos++; Cursor.printChar(mpos, 10, '~');}
if (mpos > Mouse.getColumns())
  {Cursor.printBlank(mpos, 10); mpos--; Cursor.printChar(mpos, 10, '~');}
Keyboard.wait(30);

int Mouse.getRows()

This function returns character position of mouse cursor in console window. The position is in rows. Rows are single characters in console. Using this function, you can easily determine position of mouse cursor on any device.

Function returns values from 0 to screen rows. If mouse is outside the console window from above, function returns negative numbers. When mouse is outside the console window in the bottom, function will return numbers larger than the screen rows.

Parameters

This function doesn't have any parameters.

Return value

Return type is int. It is the Y coordinate of mouse cursor in characters.

Example

This example is easy way how to highlight menu option. You put following code in a loop since it doesn't rewrite text each frame, but only when necessary. Do not forget to set integer variable lastposition.

if (lastposition != Mouse.getRows())
{
  Cursor.setPosition(10, 2);
  if (Mouse.getRows() == 2) Cursor.setColor(LIGHTGREEN);
  else Cursor.setColor(WHITE);
  std::cout << "Start the game";
}
lastposition = Mouse.getRows();
Keyboard.wait(30);