Unable to get following flags, EVENT_FLAG_L(/R/M)BUTTON, EVENT_MOUSEWHEEL

Sir:

I am coding with c++ using opencv, 4.5.4+dfsg-9+b3, Debian testing AMD64.
Currently, I am unable to get EVENT_FLAG_L(/R/M)BUTTON, EVENT_MOUSEWHEEL, flags while EVENT_FLAG_CTRL(SHIFT/ALT)KEY flags work well.
Callback function is defined as

cv::setMouseCallback(windowName, onMouseCall, 0);
void onMouseCall(int event, int x, int y, int flags, void* userdata);

Probably mouse istelf is good, because MOUSEMOVE, BUTTONDOWN(/UP/DBLCLK) EVENTs look good.

Questions are:
“mouse moves while a button hold down” yields EVENT_MOUSEMOVE and EVENT_FLAG_R(/M/L)BUTTONDOWN. is this correct?
Is any library (or kernel module??) missing?

Can any experts direct me to next step?

Thank you very much in advance.

Hi,
It’s defined here in source code
and in the doc here

Sir
Thanks for your reply.
Yes, the doc/source reads “…indicates that the left mouse button is down…”. I thought EVENT_FLAG_L(R/M)BUTTON would be set, when “mouse moves with a button is hold down”. However, these flags do not work. EVENT_FLAG_**KEY flags are set when "mouse moves with a key is pressed.

read the docs carefully. there are events and there are flags. different and distinct things.

in the flags, you can find currently pressed buttons.

the event says what just happened, i.e. mouse move, button was pressed, scroll wheel, …

Sir:
Thanks for your reply.
Apology, my original post was confusing.
With “events”, most events work, except for EVENT_MOUSEWHEEL.

With “flags”, Alt/Shift/Ctrl-key related flags do work. Left/Middle/Right mousebutton related flags do not.

My understanding is the flags are set with mousemove event. (with other events also?)
During troubleshooting, I found that mousewheel event also was non-functioning. So, original confusing post went.

Probably, I have made one step ahead(sideways??)

As the document suggests, these flag represents binary digit. So, I expect 33 (or 34) when mouse button is down with alt-key down. But, what I get is “moving application window” without event/flag. With alt-key;no mousebutton, I get event 0, flag 32 and x,y coordinates. I guess mousebutton flag is eaten up by OS, in my case Debian/xfce.

On windows this code works when window “Test event” is selected

void onMouse(int event, int x, int y, int flags, void *userdata)
{
    if (event == cv::EVENT_MOUSEWHEEL)
        cout << "EVENT_MOUSEWHEEL";
}

void main()
{
    VideoCapture v(0);
    if (!v.isOpened())
        return;
    char key = 0;
    cv::Mat img;
    cv::namedWindow("Test Event");
    cv::setMouseCallback("Test Event", onMouse);
    while (key != 27)
    {
        v >> img;
        if (img.empty())
            return;
        imshow("Test Event", img);
        key = cv::pollKey();
    }
}

Sir:
Thank you for your reply.
One step ahead (sideways, backwards??)
If your code complies on windows, it must be some sort of platform compatibility issue.
Yes, it compiles on Debian with following header lines and appropriate changes --“void main” to “int main”, “return” to “return 1” and so on.

#include “opencv2/highgui/highgui.hpp”
#include “opencv2/opencv.hpp”
#include <iostream>

using namespace std;
using namespace cv;

Unfortunately, situation is unchanged, (mousewheel event and mousebutton flags do not work)

Probably, platform dependent issue is hiding behind. It also could be a header problem.
My compiler is g++ 11.2.0

if you use the “fancy” Qt-styled imshow window with zoom and toolbar and statusbar, some stuff might not work. other than that, I’m out of ideas.

Sir:
Thank you for your reply.

  1. I am not sure if my window manager depends on qt.
  2. With alt-key pressed (flag = 32), mousebutton events (1,2,4,6,7,8,9) do not work. Event 5 works. With shift/ctrl (flag 8 or 16), no problem.
  3. There are two “highgui.hpp”, one under opencv2/, the other opencv2/highgui/. Their contents are different. Both compile without error. Situation did not resolve.

For the time being, I avoid mousebutton flag. I will keep eyes open.

things on GNU+linux generally do whatever they want. your window manager might swallow some events that it thinks are meant for itself and not the application. can’t discount anything.

I tried on a rasberry using bullseye. It works with mouse_wheel. What is your configuration (getBuildInformation)?

program :

#include <opencv2/opencv.hpp>
#include <iostream>


void onMouse(int event, int x, int y, int flags, void *userdata)
{
    if (event == cv::EVENT_MOUSEWHEEL)
        std::cout << "EVENT_MOUSEWHEEL";
}

int main(int argc, char**argv)
{
    cv::Mat img = cv::imread("/home/pi/opencv/samples/data/baboon.jpg", cv::IMREAD_COLOR);
    char key = 0;
    cv::namedWindow("Test Event");
    cv::setMouseCallback("Test Event", onMouse);
    while (key != 27)
    {
       if (img.empty())
            return 0;
       cv::imshow("Test Event", img);
       key = cv::pollKey();
    }

    return 0;
}

opencv config

General configuration for OpenCV 4.5.5-dev =====================================
  Version control:               4.5.5-310-g9aa647068b

  Platform:
    Timestamp:                   2022-04-08T08:52:04Z
    Host:                        Linux 5.15.25-v7+ armv7l
    CMake:                       3.18.4
    CMake generator:             Unix Makefiles
    CMake build tool:            /usr/bin/gmake
    Configuration:               Release

  CPU/HW features:
    Baseline:
      requested:                 DETECT

  C/C++:
    Built as dynamic libs?:      YES
    C++ standard:                11
    C++ Compiler:                /usr/bin/c++  (ver 10.2.1)
    C++ flags (Release):         -fsigned-char -W -Wall -Wreturn-type -Wnon-virtual-dtor -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG  -DNDEBUG
    C++ flags (Debug):           -fsigned-char -W -Wall -Wreturn-type -Wnon-virtual-dtor -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -fvisibility=hidden -fvisibility-inlines-hidden -g  -O0 -DDEBUG -D_DEBUG
    C Compiler:                  /usr/bin/cc
    C flags (Release):           -fsigned-char -W -Wall -Wreturn-type -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -fvisibility=hidden -O3 -DNDEBUG  -DNDEBUG
    C flags (Debug):             -fsigned-char -W -Wall -Wreturn-type -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -fvisibility=hidden -g  -O0 -DDEBUG -D_DEBUG
    Linker flags (Release):      -Wl,--gc-sections -Wl,--as-needed -Wl,--no-undefined
    Linker flags (Debug):        -Wl,--gc-sections -Wl,--as-needed -Wl,--no-undefined
    ccache:                      NO
    Precompiled headers:         NO
    Extra dependencies:          dl m pthread rt
    3rdparty dependencies:

  OpenCV modules:
    To be built:                 calib3d core dnn features2d flann gapi highgui imgcodecs imgproc ml objdetect photo python3 stitching ts video videoio
    Disabled:                    world
    Disabled by dependency:      -
    Unavailable:                 java python2
    Applications:                tests perf_tests apps
    Documentation:               NO
    Non-free algorithms:         NO

  GUI:                           GTK2
    GTK+:                        YES (ver 2.24.33)
      GThread :                  YES (ver 2.66.8)
      GtkGlExt:                  NO
    VTK support:                 NO

  Media I/O:
    ZLib:                        /usr/lib/arm-linux-gnueabihf/libz.so (ver 1.2.11)
    JPEG:                        libjpeg-turbo (ver 2.1.2-62)
    WEBP:                        build (ver encoder: 0x020f)
    PNG:                         /usr/lib/arm-linux-gnueabihf/libpng.so (ver 1.6.37)
    TIFF:                        build (ver 42 - 4.2.0)
    JPEG 2000:                   build (ver 2.4.0)
    OpenEXR:                     /usr/lib/arm-linux-gnueabihf/libImath-2_5.so /usr/lib/arm-linux-gnueabihf/libIlmImf-2_5.so /usr/lib/arm-linux-gnueabihf/libIex-2_5.so /usr/lib/arm-linux-gnueabihf/libHalf-2_5.so /usr/lib/arm-linux-gnueabihf/libIlmThread-2_5.so (ver 2_5)
    HDR:                         YES
    SUNRASTER:                   YES
    PXM:                         YES
    PFM:                         YES

  Video I/O:
    DC1394:                      NO
    FFMPEG:                      YES
      avcodec:                   YES (58.91.100)
      avformat:                  YES (58.45.100)
      avutil:                    YES (56.51.100)
      swscale:                   YES (5.7.100)
      avresample:                NO
    GStreamer:                   NO
    v4l/v4l2:                    YES (linux/videodev2.h)

  Parallel framework:            pthreads

  Trace:                         YES (with Intel ITT)

  Other third-party libraries:
    Lapack:                      NO
    Eigen:                       NO
    Custom HAL:                  NO
    Protobuf:                    build (3.19.1)

  OpenCL:                        YES (no extra features)
    Include path:                /home/pi/opencv/3rdparty/include/opencl/1.2
    Link libraries:              Dynamic load

  Python 3:
    Interpreter:                 /usr/local/bin/python3 (ver 3.9.7)
    Libraries:                   /usr/local/lib/libpython3.9.a (ver 3.9.7)
    numpy:                       /usr/local/lib/python3.9/site-packages/numpy/core/include (ver 1.22.2)
    install path:                lib/python3.9/site-packages/cv2/python-3.9

  Python (for build):            /usr/local/bin/python3

  Java:
    ant:                         NO
    JNI:                         /usr/lib/jvm/default-java/include /usr/lib/jvm/default-java/include/linux /usr/lib/jvm/default-java/include
    Java wrappers:               NO
    Java tests:                  NO

  Install to:                    /usr/local

Sir:
Thank you for your replies:

Crackwits: I 120% agree with you. Only one thing I can currently say is that “gimp, which uses gtk - not qt, behaves correct when mousemove with mousebutton events.” Probably, my window manager does not eat mousebutton. It probably eats mousebutton event(s) when mousecursor is on window’s title bar.

laurent,berger: The code complies and runs. But situation is same. Can you please direct me to openCV config file? Raspberry pi OS is Debian descendant, so probably config is similar. I need to check entire changelog.

To get opencv config use cout<<cv::getBuildInformation() in your C++ code

Sir:

config follows:

General configuration for OpenCV 4.5.4 =====================================
Version control: unknown

Extra modules:
Location (extra): /build/opencv-9623pJ/opencv-4.5.4+dfsg/contrib/modules
Version control (extra): unknown

Platform:
Timestamp: 2022-01-26T20:31:19Z
Host: Linux 5.10.0-11-amd64 x86_64
CMake: 3.22.1
CMake generator: Ninja
CMake build tool: /usr/bin/ninja
Configuration: Release

CPU/HW features:
Baseline: SSE SSE2
requested: SSE2
disabled: SSE3
Dispatched code generation: SSE4_1 SSE4_2 FP16 AVX AVX2 AVX512_SKX
requested: SSE4_1 SSE4_2 AVX FP16 AVX2 AVX512_SKX
SSE4_1 (14 files): + SSE3 SSSE3 SSE4_1
SSE4_2 (1 files): + SSE3 SSSE3 SSE4_1 POPCNT SSE4_2
FP16 (0 files): + SSE3 SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX
AVX (4 files): + SSE3 SSSE3 SSE4_1 POPCNT SSE4_2 AVX
AVX2 (29 files): + SSE3 SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2
AVX512_SKX (5 files): + SSE3 SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2 AVX_512F AVX512_COMMON AVX512_SKX

C/C++:
Built as dynamic libs?: YES
C++ standard: 11
C++ Compiler: /usr/bin/c++ (ver 11.2.0)
C++ flags (Release): -g -O2 -ffile-prefix-map=/build/opencv-9623pJ/opencv-4.5.4+dfsg=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -fvisibility=hidden -fvisibility-inlines-hidden -g -O2 -ffile-prefix-map=/build/opencv-9623pJ/opencv-4.5.4+dfsg=. -fstack-protector-strong -Wformat -Werror=format-security -DNDEBUG
C++ flags (Debug): -g -O2 -ffile-prefix-map=/build/opencv-9623pJ/opencv-4.5.4+dfsg=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -fvisibility=hidden -fvisibility-inlines-hidden -g -DDEBUG -D_DEBUG
C Compiler: /usr/bin/cc
C flags (Release): -g -O2 -ffile-prefix-map=/build/opencv-9623pJ/opencv-4.5.4+dfsg=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fsigned-char -W -Wall -Werror=return-type -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -fvisibility=hidden -g -O2 -ffile-prefix-map=/build/opencv-9623pJ/opencv-4.5.4+dfsg=. -fstack-protector-strong -Wformat -Werror=format-security -DNDEBUG
C flags (Debug): -g -O2 -ffile-prefix-map=/build/opencv-9623pJ/opencv-4.5.4+dfsg=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fsigned-char -W -Wall -Werror=return-type -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -fvisibility=hidden -g -DDEBUG -D_DEBUG
Linker flags (Release): -Wl,-z,relro -Wl,-z,now -Wl,–gc-sections -Wl,–as-needed -Wl,-z,relro -Wl,-z,now
Linker flags (Debug): -Wl,-z,relro -Wl,-z,now -Wl,–gc-sections -Wl,–as-needed
ccache: NO
Precompiled headers: NO
Extra dependencies: dl m pthread rt
3rdparty dependencies:

OpenCV modules:
To be built: alphamat aruco barcode bgsegm bioinspired calib3d ccalib core datasets dnn dnn_objdetect dnn_superres dpm face features2d flann freetype fuzzy hdf hfs highgui img_hash imgcodecs imgproc intensity_transform java line_descriptor mcc ml objdetect optflow phase_unwrapping photo plot python3 quality rapid reg rgbd saliency shape stereo stitching structured_light superres surface_matching text tracking video videoio videostab viz wechat_qrcode ximgproc xobjdetect xphoto
Disabled: world
Disabled by dependency: sfm
Unavailable: cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev cvv gapi julia matlab ovis python2 ts
Applications: apps
Documentation: doxygen python javadoc
Non-free algorithms: NO

GUI: GTK3
GTK+: YES (ver 3.24.31)
GThread : YES (ver 2.70.2)
GtkGlExt: NO
OpenGL support: NO
VTK support: YES (ver 9.1.0)

Media I/O:
ZLib: /usr/lib/x86_64-linux-gnu/libz.so (ver 1.2.11)
JPEG: /usr/lib/x86_64-linux-gnu/libjpeg.so (ver 62)
WEBP: /usr/lib/x86_64-linux-gnu/libwebp.so (ver encoder: 0x020e)
PNG: /usr/lib/x86_64-linux-gnu/libpng.so (ver 1.6.37)
TIFF: /usr/lib/x86_64-linux-gnu/libtiff.so (ver 42 / 4.3.0)
JPEG 2000: OpenJPEG (ver 2.4.0)
OpenEXR: /usr/lib/x86_64-linux-gnu/libImath-2_5.so /usr/lib/x86_64-linux-gnu/libIlmImf-2_5.so /usr/lib/x86_64-linux-gnu/libIex-2_5.so /usr/lib/x86_64-linux-gnu/libHalf-2_5.so /usr/lib/x86_64-linux-gnu/libIlmThread-2_5.so (ver 2_5)
GDAL: YES (/usr/lib/libgdal.so)
GDCM: YES (3.0.10)
HDR: YES
SUNRASTER: YES
PXM: YES
PFM: YES

Video I/O:
DC1394: YES (2.2.6)
FFMPEG: YES
avcodec: YES (58.134.100)
avformat: YES (58.76.100)
avutil: YES (56.70.100)
swscale: YES (5.9.100)
avresample: NO
GStreamer: YES (1.18.5)
PvAPI: NO
v4l/v4l2: YES (linux/videodev2.h)
gPhoto2: YES

Parallel framework: TBB (ver 2020.3 interface 11103)

Trace: YES (built-in)

Other third-party libraries:
VA: NO
Lapack: NO
Eigen: YES (ver 3.4.0)
Custom HAL: NO
Protobuf: /usr/lib/x86_64-linux-gnu/libprotobuf.so (3.12.4)

OpenCL: YES (no extra features)
Include path: /usr/include/CL
Link libraries: Dynamic load

Python 3:
Interpreter: /usr/bin/python3 (ver 3.9.10)
Libraries: /usr/lib/x86_64-linux-gnu/libpython3.9.so (ver 3.9.10)
numpy: /usr/lib/python3/dist-packages/numpy/core/include (ver 1.21.5)
install path: lib/python3.9/dist-packages

Python (for build): /usr/bin/python3

Java:
ant: /usr/bin/ant (ver 1.10.12)
JNI: /usr/lib/jvm/default-java/include /usr/lib/jvm/default-java/include/linux /usr/lib/jvm/default-java/include
Java wrappers: YES
Java tests: NO

Install to: /usr

difference is GUI: GTK3
GTK+: YES (ver 3.24.31) for your config

my config

 GUI:                           GTK2
    GTK+:                        YES (ver 2.24.33)

Sir:

One point I am not confident.
Is this correct?
When mousecursor is moved while a mousebutton is pressed down, “event 0, flag 1 (or 2, 4)” is returned to mousecallback function.

Is the term “button down” identical to “button is pressed” ?

By the way, mousewheel event still is not functional.

try to use EVENT_MOUSEHWHEEL instead of EVENT_MOUSEWHEEL

and I think answer is here

#if defined(GTK_VERSION3_4)
        // NOTE: in current implementation doesn't possible to put into callback function delta_x and delta_y separately
        double delta = (event->scroll.delta_x + event->scroll.delta_y);
        cv_event   = (event->scroll.delta_y!=0) ? CV_EVENT_MOUSEHWHEEL : CV_EVENT_MOUSEWHEEL;
#else
        cv_event = CV_EVENT_MOUSEWHEEL;
#endif //GTK_VERSION3_4

        state    = event->scroll.state;

        switch(event->scroll.direction) {
#if defined(GTK_VERSION3_4)
        case GDK_SCROLL_SMOOTH: flags |= (((int)delta << 16));
            break;
#endif //GTK_VERSION3_4
        case GDK_SCROLL_LEFT:  cv_event = CV_EVENT_MOUSEHWHEEL;
            /* FALLTHRU */
        case GDK_SCROLL_UP:    flags |= ~0xffff;
            break;
        case GDK_SCROLL_RIGHT: cv_event = CV_EVENT_MOUSEHWHEEL;
            /* FALLTHRU */
        case GDK_SCROLL_DOWN:  flags |= (((int)1 << 16));
            break;
        default: ;
        };

I think it’s a bug. wait and see

Sir:
Thank you for your replies.

Yes, it was not accurate. (Most of lowercase terms are not accurate.)

I only checked Debian bug, and did not go further, apology. In GitHub, I have not found EVENT_FLAG_* problem. Is there any other place to check?

Sir:

Codename of my Debian testing is “bookworm”, which succeeds “bullseye”

Raspberry pi uses “mutter” (or openbox in less memory one) as its window manager. They say that they have moved to GTK3 par this page.