Keyboard and Mouse Input¶
Here is a list of examples.
Tutorial 1¶
// ========================================================================== //
// This file is part of Sara, a basic set of libraries in C++ for computer
// vision.
//
// Copyright (C) 2019 David Ok <david.ok8@gmail.com>
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License v. 2.0. If a copy of the MPL was not distributed with this file,
// you can obtain one at http://mozilla.org/MPL/2.0/.
// ========================================================================== //
//! @example
#include <DO/Sara/Graphics.hpp>
using namespace std;
using namespace DO::Sara;
GRAPHICS_MAIN()
{
cout << "Basic mouse functions" << endl;
Window W = create_window(512, 512, "Mouse");
draw_string(10, 10, "Please click anywhere", Black8);
click();
draw_string(10, 40, "click again (left=BLUE, middle=RED, right=done)",
Black8);
int button;
Point2i p;
while ((button=get_mouse(p)) != MOUSE_RIGHT_BUTTON)
{
Rgb8 color;
if (button == MOUSE_LEFT_BUTTON)
color = Blue8;
else if (button == MOUSE_MIDDLE_BUTTON)
color = Red8;
else
color = Black8;
fill_circle(p, 5, color);
}
close_window(W);
return 0;
}
Tutorial 2¶
// ========================================================================== //
// This file is part of Sara, a basic set of libraries in C++ for computer
// vision.
//
// Copyright (C) 2019 David Ok <david.ok8@gmail.com>
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License v. 2.0. If a copy of the MPL was not distributed with this file,
// you can obtain one at http://mozilla.org/MPL/2.0/.
// ========================================================================== //
//! @example
#include <DO/Sara/Graphics.hpp>
using namespace std;
using namespace DO::Sara;
GRAPHICS_MAIN()
{
Window w = create_window(300, 300);
set_active_window(w);
Event e;
do
{
get_event(1, e);
fill_rect(rand()%300, rand()%300, rand()%50, rand()%50,
Color3ub(rand()%256, rand()%256, rand()%256));
//microSleep(100); // TODO: sometimes if you don't put this, the program
// freezes in some machine. Investigate.
} while (e.key != KEY_ESCAPE);
cout << "Finished!" << endl;
close_window(active_window());
return 0;
}
Tutorial 3¶
// ========================================================================== //
// This file is part of Sara, a basic set of libraries in C++ for computer
// vision.
//
// Copyright (C) 2019 David Ok <david.ok8@gmail.com>
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License v. 2.0. If a copy of the MPL was not distributed with this file,
// you can obtain one at http://mozilla.org/MPL/2.0/.
// ========================================================================== //
//! @example
#include <DO/Sara/Graphics.hpp>
using namespace std;
using namespace DO::Sara;
GRAPHICS_MAIN()
{
cout << "Advanced event handling" << endl;
Window W = create_window(1024, 768);
set_active_window(W);
Image<Color3ub> I;
load(I, src_path("../../datasets/ksmall.jpg"));
Event ev;
do
{
get_event(500, ev); // Wait an event (return if no event for 500ms)
switch (ev.type)
{
case NO_EVENT:
break;
case MOUSE_PRESSED_AND_MOVED:
clear_window();
display(I, ev.mousePos - I.sizes() * 3 / 8, 0.75);
cout << "Mouse moved. Position = " << endl << ev.mousePos << endl;
break;
case KEY_PRESSED:
cout << "Key " << ev.key << " pressed" << endl;
break;
case KEY_RELEASED:
cout << "Key " << ev.key << " released" << endl;
break;
case MOUSE_PRESSED:
clear_window();
display(I, ev.mousePos - I.sizes() * 3 / 8, 0.75);
cout << "Button " << ev.buttons << " pressed" << endl;
break;
case MOUSE_RELEASED:
cout << "Button " << ev.buttons << " released" << endl;
break;
}
} while (ev.type != KEY_PRESSED || ev.key != KEY_UP);
close_window(W);
return 0;
}