cisstStereoVision Architecture
[wiki:SVLTutorialFilterImageWindow Handling image window events (mouse and keyboard)]
Example 3 - Image Window Event Handling
List of files
CMakeLists.txt: CMake script for creating compiler dependent projectsCMyEventHandler.h: Image window event handler class declarationCMyEventHandler.cpp: Image window event handler class definitionmain.cpp: Application entry point (main()function)
All files can be found under cisstStereoVision/examples/tutorial2.
Source code
CMake
cmake_minimum_required (VERSION 3.16)
# create a list of libraries needed for this project
set (REQUIRED_CISST_LIBRARIES cisstCommon cisstVector cisstOSAbstraction cisstMultiTask cisstStereoVision)
# find cisst and make sure the required libraries have been compiled
find_package (cisst REQUIRED ${REQUIRED_CISST_LIBRARIES})
if (cisst_FOUND)
add_executable (svlExTutorial2
CMyEventHandler.h
CMyEventHandler.cpp
main.cpp
)
set_property (TARGET svlExTutorial2 PROPERTY FOLDER "cisstStereoVision/examples")
target_link_libraries (svlExTutorial2 ${REQUIRED_CISST_LIBRARIES})
else (cisst_FOUND)
message ("Information: code in ${CMAKE_CURRENT_SOURCE_DIR} will not be compiled, it requires ${REQUIRED_CISST_LIBRARIES}")
endif (cisst_FOUND)
Event handler class declaration
#include <cisstStereoVision/svlWindowManagerBase.h>
class CMyEventHandler : public svlWindowEventHandlerBase
{
public:
CMyEventHandler();
void OnUserEvent(unsigned int winid, bool ascii, unsigned int eventid);
private:
svlRect ROI;
bool LMouseButtonPressed;
};
Event handler class implementation
#include "CMyEventHandler.h"
CMyEventHandler::CMyEventHandler() :
LMouseButtonPressed(false)
{
}
void CMyEventHandler::OnUserEvent(unsigned int winid, bool ascii, unsigned int eventid)
{
if (ascii) { // Event is an ASCII character
std::cout << static_cast<char>(eventid) << std::flush; // Write out character
}
else { // Event is not an ASCII character:
// mouse event or special key
int x, y;
GetMousePos(x, y);
switch (eventid) {
case winInput_LBUTTONDOWN:
LMouseButtonPressed = true;
ROI.left = ROI.right = x;
ROI.top = ROI.bottom = y;
std::cerr << ROI.left << ", "
<< ROI.top << ", "
<< ROI.right << ", "
<< ROI.bottom << std::endl;
break;
case winInput_LBUTTONUP:
if (LMouseButtonPressed) {
ROI.Normalize();
std::cerr << "ROI = " << ROI.left << ", "
<< ROI.top << ", "
<< ROI.right << ", "
<< ROI.bottom << std::endl;
}
LMouseButtonPressed = false;
break;
case winInput_MOUSEMOVE:
if (LMouseButtonPressed) {
ROI.right = x;
ROI.bottom = y;
std::cerr << ROI.left << ", "
<< ROI.top << ", "
<< ROI.right << ", "
<< ROI.bottom << std::endl;
}
break;
case winInput_KEY_F1:
case winInput_KEY_F2:
case winInput_KEY_F3:
case winInput_KEY_F4:
case winInput_KEY_F5:
case winInput_KEY_F6:
case winInput_KEY_F7:
case winInput_KEY_F8:
case winInput_KEY_F9:
case winInput_KEY_F10:
case winInput_KEY_F11:
case winInput_KEY_F12:
case winInput_KEY_PAGEUP:
case winInput_KEY_PAGEDOWN:
case winInput_KEY_HOME:
case winInput_KEY_END:
case winInput_KEY_INSERT:
case winInput_KEY_DELETE:
case winInput_KEY_LEFT:
case winInput_KEY_RIGHT:
case winInput_KEY_UP:
case winInput_KEY_DOWN:
std::cout << "Special key down" << std::endl;
break;
}
}
}
Main program
#include "CMyEventHandler.h"
#include <cisstStereoVision/svlInitializer.h>
#include <cisstStereoVision/svlFilterOutput.h>
#include <cisstStereoVision/svlStreamManager.h>
#include <cisstStereoVision/svlFilterSourceVideoCapture.h>
#include <cisstStereoVision/svlFilterImageWindow.h>
int main()
{
svlInitialize(); // Discover supported devices and codecs
svlStreamManager stream; // Instantiate SVL Stream
svlFilterSourceVideoCapture source(1); // Instantiate video capture filter
svlFilterImageWindow window; // Instantiate image window filter
CMyEventHandler event_handler; // Instantiate our own window event handler
window.SetEventHandler(&event_handler); // Assign event handler to image window filter
source.SetDevice(0); // Select first available capture device
stream.SetSourceFilter(&source); // Assign source filter to stream
source.GetOutput()->Connect(window.GetInput()); // Connect source filter to window filter
stream.Play(); // Initialize and Play video stream
char ch;
std::cin >> ch; // (Wait for key-press)
stream.Release(); // Release stream
return 0;
}