Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
386 views
in Technique[技术] by (71.8m points)

c - How can I simulate mouse events from code?

I would like to simulate mouse events using the Win32 API; how can I do it?

What I want to do is simulate the event at the most basic level, the level at which the system has just the event type and the co-ordinates and hasn't yet figured which window it must relay it to.

I don't know if that's how things work. Either way, I need help doing it. Would I have to meddle at the driver level?!

To make my requirements clear, I don't want to target any window, I just want the system to think the mouse was clicked or moved by the user. And I would be coding in C.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You're looking for the SendInput function, which allows you to synthesize mouse movements and button clicks in your code by specifying an array of INPUT structures corresponding to input events.

UINT WINAPI SendInput(
  __in  UINT nInputs,     // number of structures in the pInputs array
  __in  LPINPUT pInputs,  // an array of INPUT structures, representing an event
  __in  int cbSize        // the size, in bytes, of an INPUT structure
);

Note, however, that this function is subject to User Interface Privilege Isolation (UIPI), which means that your application is only permitted to inject input to applications that are running at an equal or lesser integrity level.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...