Callback functions WindowButtonUpFcn not assigning proper SelectionType when multiple buttons are pressed

8 次查看(过去 30 天)
I want my program to do the following:
  • Create a figure and detect when it's been clicked
  • If it's been left clicked, at the release of the click do a certain action
  • If, while holding left click, you right click instead, then cancel the action
However when I right click while holding left click, the WindowButtonDownFcn callback doesn't go off and the WindowButtonUpFcn callback instead detects the SelectionType as 'normal' when it should be 'alt'; then it doesn't register the left click release.
Here is a simplified version of the code where there are only the parts relevant to the problem
function test
board = 0;
figure('WindowButtonDownFcn', @choice);
imshow(board);
function choice(src, ~)
seltype = src.SelectionType;
if strcmp(seltype,'normal')
disp('left click')
src.WindowButtonUpFcn = @answer;
%If left clicking, assign the callback for click release to
%function 'answer'
elseif strcmp(seltype,'alt')
disp('right click')
src.WindowButtonUpFcn = '';
%If right clicking, reset the callback for click release to
%null
end
end
function answer(src, ~)
seltype = src.SelectionType;
if strcmp(seltype,'normal')
%If releasing left click, give the answer required
disp('OK')
end
end
end
The outcome is the following:
Press Left Click: all as it should be
Press Right Click: the 'right click' that should show up doesn't
Release Right Click: the 'OK' shows up
Release Left Click: nothing shows up
I'm not sure why the "right click press" instance does not make the program go into the code that should remove the assignment on the WindowButtonUpFcn callback, and I'm even less sure why the right click release gets registered as a left click release.
  2 个评论
Voss
Voss 2022-5-11
Verify what the SelectionType actually is when the right-click happens. If the left mouse button is already down, then I believe the right-click comes in as an 'extend' type, not 'alt' type.
DAMIANO YOEME BUSSAGLI
编辑:DAMIANO YOEME BUSSAGLI 2022-5-11
I had similar problems recently on MATLAB Online.
Trying your code on MATLAB Online (version '9.12.0.1896817 (R2022a)') I get incorrects outputs (exactly as you showed).
But trying the code on desktop (MATLAB version '9.12.0.1927505 (R2022a) Update 1') the outputs are the correct ones:
  1. Press Left Click: disp('left click')
  2. Press Right Click: disp('right click')
  3. Release Right Click: nothing shows up
  4. Release Left Click: nothing shows up
Unfortunately I don't know why this happens, but it should be a problem of MATLAB Online.

请先登录,再进行评论。

回答(1 个)

Abhishek Chakram
Abhishek Chakram 2023-10-6
Hi Alessio Bernazzi.
It is my understanding that you want to create a program that does the following tasks:
  • Create a figure and detect the clicks on it
  • If left clicked, do a certain task at the release of the click
  • If right clicked, while holding left click, cancel the task
Here is a sample code to achieve the same:
% Initialize the click state variables
clc;
global leftClick rightClick
leftClick = false;
rightClick = false;
% Create a figure
hfig = figure();
% Set the WindowButtonDownFcn callback
hfig.WindowButtonDownFcn = @(src, evt) mouseButtonDown(src, evt);
% Set the WindowButtonUpFcn callback
hfig.WindowButtonUpFcn = @(src, evt) mouseButtonUp(src, evt);
% Callback function for left mouse button down
function mouseButtonDown(src, evt)
global leftClick rightClick
if strcmp(evt.Source.SelectionType, 'normal') % Left click
leftClick = true;
elseif strcmp(evt.Source.SelectionType, 'alt') % Right click
rightClick = true;
elseif strcmp(evt.Source.SelectionType, 'extend') % Right click
rightClick = true;
leftClick = true;
end
end
% Callback function for left mouse button up
function mouseButtonUp(src, evt)
global leftClick rightClick
if leftClick && ~rightClick
% Perform the desired action for left click release
disp('Left click released');
elseif leftClick && rightClick
% Cancel the action for right click
disp('Right click detected while holding left click');
end
% Reset the click state variables
leftClick = false;
rightClick = false;
end
In this example, the variables “leftClick” and “rightClick” is used to track the mouse events.
You can refer to the following documentation to know more about the functions used:
Hope this answers your question.
Best Regards,
Abhishek Chakram

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

产品


版本

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by