MATLAB Answers

determine if the mouse is pressed in a given moment

61 ビュー (過去 30 日間)
Arabarra 2017 年 4 月 21 日
編集済み: Bruno Luong 2020 年 11 月 14 日
I’m wondering if the Figure class has some (probably hidden) property that expresses if the mouse is pressed (and which button) in the moment that the moment that the property is read. If the figure had an event for button up and down events that would also solve my problem.
I know I could set WindowButtonDownFcn and WindowButtonUpFcn to inform a global variable to keep track on when the mouse is used, but that's deeply unpractical (in my code WindowButtonDownFcn gets changed frequently depending on the actions of the user)

  0 件のコメント



Jan 2017 年 9 月 19 日
編集済み: Jan 2017 年 9 月 19 日
It would be easy, if you do not change the WindowButtonDownFcn frequently, but keep it statically and use a persistent variable for branching. Then you had to insert the code to store the button state at one location only.
If you check the button state, you have to consider the currently active window also. You do not want to react to clicks in the task bar or the desktop.
[EDITED] The FEX submission is not working out of the box. Try this:
% !!! UNTESTED !!!
function [L, R, M] = GetMouseKeyStatus()
if ~ispc
error('Running under Windows only.');
if ~libisloaded('user32')
loadlibrary('user32.dll', 'user32.h');
L = calllib('user32', 'GetAsyncKeyState', int32(1)) ~= 0;
R = calllib('user32', 'GetAsyncKeyState', int32(2)) ~= 0;
M = calllib('user32', 'GetAsyncKeyState', int32(4)) ~= 0;

  3 件のコメント

Arabarra 2017 年 9 月 21 日
Thanks Jan... hm, a pity this option is only available in Windows. I'll try it, anyway.
Bruno Luong
Bruno Luong 2020 年 11 月 14 日
It seems LOADLIBRARY is not working on my system (Windows 10, 20H2)
K>> loadlibrary('user32.dll', 'user32.h')
Error using loadlibrary
Could not find file user32.h.
Error in loadlibrary
user32.dll still exists however.
Here is my workaround. I create an h-file called WinMouse.h and place it together with ismousedpressed.m
function keypressed = ismousedpressed()
% keypressed = ismousedpressed()
% returns
% 0 no button is pressed
% 1 is left button
% 2 is right button
% 3 is scroll button push (not scrolling)
% Restriction: Windows only
% TIP: call ismousedpressed() once when your app start
% so that when really needed; ismousedpressed() returns result faster.
% Author: Mbvalentin
% Modified by Bruno Luong
if ~libisloaded('user32')
loadlibrary('C:\WINDOWS\system32\user32.dll', 'user32.h');
% user.h is removed on certain recent version of Windows
% We replace with a simple customized header file
here = fileparts(mfilename('fullpath'));
loadlibrary('C:\WINDOWS\system32\user32.dll', [here '\WinMouse.h']);
left = calllib('user32','GetAsyncKeyState',int32(1)) ~= 0;
right = calllib('user32','GetAsyncKeyState',int32(2)) ~= 0;
scroll = calllib('user32','GetAsyncKeyState',int32(4)) ~= 0;
keypressed = max([1 2 3].*double([left right scroll]));
end % ismousedpressed
File WinMouse.h, this suppose the GetAsyncKeyState signature won't change on Windows OS (which likely the case)
/* WinMouse.h
* Replacement of user32.h if it's not available
see ismousedpressed.m
This file must be located at the same place as ismousedpressed */
short GetAsyncKeyState(
int vKey


その他の回答 (1 件)

Ankit Bhardwaj
Ankit Bhardwaj 2017 年 4 月 24 日
編集済み: Ankit Bhardwaj 2017 年 4 月 24 日
You can use ButtonDownFcn callback for your figure. This callback executes whenever the user clicks a mouse button while the pointer is in the figure window, but not over a child object such as a uicontrol, uipanel, axes, or axes child. Moreover, mouse selection type returns 'normal', 'extend' , 'alt' , or 'open'. MATLAB maintains this property to provide information about the last mouse-button press that occurred within the figure window. This information indicates the type of selection made. Please go through the following documentation to learn about callback properties of figure object.

  2 件のコメント

Arabarra 2017 年 9 月 19 日
Hi Ankit,
thanks for your suggestion. This would be rather unpractical in my case, as in my GUI I define a large number of "modus" (similar to the zoom, rotation, etc of a regular matlab figure). This means, a lot of ButtonDownFcns that change during runtime. To implement your solution, I would be forced to make certain that every ButtonDownFcn triggers an event that tells some listener that the button was pressed, and that every ButtonUpFcn broadcasts the release of the button. That'd be a titanic effort, specially because I rely on a large library of code that I didn't develop specifically for this application (and which I don't wish to modify, as they are used by other applications).
cheers, Daniel
Walter Roberson
Walter Roberson 2017 年 9 月 19 日
I wonder if iptaddcallback() would help ?



Community Treasure Hunt

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

Start Hunting!

Translated by