Calling User32.dll mouse_event
古いコメントを表示
Hi,
I don't know if this is the right place to ask, but here goes:
I want to control the mouse using Matlab (version R2010B). I tried the java robot code, but it does not work in my application. It has to be done by calling the user32.dll. I can move the mouse using "setCursorPos". But now I also want to emulate a mouse click (left button only for now).
So basically my question is: How do I successfully call mouse_event in user32 to perform a left click ?
Here is my code so far:
clc
screenSize = get(0, 'screensize');
if(libisloaded('user32')==0)
disp('loading dll...')
loadlibrary('C:\WINDOWS\system32\user32.dll','user32.h');
else
disp('dll already loaded')
end
calllib('user32','SetCursorPos',screenSize(3)/2,screenSize(4)/2); %center cursor on screen
So far so good. But when I add something like this at the end, it just crashes and I have to restart matlab:
MouseEventLeftDown = 2;
MouseEventLeftUp = 4;
calllib('user32','mouse_event',MouseEventLeftDown,0,0,0,0);
calllib('user32','mouse_event',MouseEventLeftUp,0,0,0,0);
Thanks in advance,
Eelco
採用された回答
その他の回答 (4 件)
Jayson
2018 年 1 月 3 日
2 投票
HI I met the same problem. When I input the word "loadlibrary('C:\WINDOWS\system32\user32.dll','user32.h')", I got back the message "Could not find file user32.h", I dont know why, can anyone help me?
victoria vincze
2011 年 7 月 18 日
0 投票
Hy
I'm about to try to do something like this, but your solution doesn't work for me. It cannot find user32.h. Does somebody know how can I solve this?
V
2 件のコメント
Jan
2011 年 7 月 18 日
Did you install a C-compiler?
victoria vincze
2011 年 7 月 20 日
Of Course.
I've a mex file, which sets the cursor, but it also works incorrectly cause it sets the cursor on the left upper corner instead of the center of the screen.
Chirag Gupta
2011 年 7 月 18 日
I haven't looked at user32.h, but I will take a guess with checking the function syntax in the header file for mouse_event.
When you pass MouseEventLeftDown = 2; you are actually passing a double value (MATLAB defaults to double) [Check using whos]. If the mouse_event expects an int32, it will crash. Try and cast the value to the type expected.
calllib('user32','mouse_event',int32(MouseEventLeftDown),0,0,0,0);
2 件のコメント
Jan
2011 年 7 月 18 日
@Chirag: But the last argument is a ULONG_PTR. Does the DOUBLE 0 match this on 32-bit Matlab's also?
Chirag Gupta
2011 年 7 月 18 日
@Jan: I haven't seen the header file. But if it's a ULONG_PTR then the best course is to create a libpointer of type uint32.
deng
2013 年 5 月 19 日
編集済み: Walter Roberson
2013 年 5 月 19 日
clc
screenSize = get(0, 'screensize');
if(libisloaded('user32')==0)
disp('loading dll...')
loadlibrary('C:\WINDOWS\system32\user32.dll','user32.h');
else
disp('dll already loaded')
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
loading dll...
??? Error using ==> loadlibrary>lFullPath at 553
Could not find file user32.h.
Error in ==> loadlibrary at 221
header=lFullPath(header);
Error in ==> matlabdiaoyongwindowshanshu at 5
loadlibrary('C:\WINDOWS\system32\user32.dll','user32.h');
1 件のコメント
Walter Roberson
2013 年 5 月 19 日
Which operating system are you using, and are you running the operating system in 32 bit or 64 bit mode? Are you using 32 bit MATLAB or 64 bit?
カテゴリ
ヘルプ センター および File Exchange で Startup and Shutdown についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!