如何全局控制鼠标(在表单之外) [英] How do I control mouse globally(outside of a form)
问题描述
所以基本上我正在尝试制作一个程序,它使用热键全局控制鼠标。程序有一个标志值(0和1),它指示下一个鼠标事件。当Flag Value为0时,当我按下Numpad5时,我希望鼠标移动到其当前坐标上方10个像素,当我再次按下Numpad5时,我希望它向下移动10个像素。现在,就我所想要的概念而言,问题是我想要的。一旦程序被最小化它将无法工作所以我想知道有什么方法可以在全局范围内完成这项工作吗?
So basically I'm trying to make a program, that controls the mouse globally with the use of a hot key. The program has a flag value ( 0 and 1 ) which dictates the next mouse event. When Flag Value is 0, when I press Numpad5 I would like the mouse move 10 pixels above it's current coordinates and when I press Numpad5 again I would like it to move down 10 pixels. Now as far as the concept I have achieved what I wanted but the problem is. Once the program gets minimized it won't work so I was wondering is there any way I can do this work globally?
推荐答案
模拟鼠标事件的唯一可靠方法一般情况下使用Windows API函数SendInput
:
发送输入功能(Windows) [ ^ ]。
如果你使用.NET,你可以通过P / Invoke来实现。它已经为您完成了: http://www.pinvoke.net/default.aspx/user32.sendinput [ ^ ]。
此功能是低级别的,它与物理鼠标设备产生的效果相同,无论应用程序处理什么事件。尝试通过发送或发布Windows消息来执行此操作是一个常见的错误;并非所有应用程序和控件都使用消息泵送。
-SA
The only reliable way to simulate mouse event in general case is using the Windows API functionSendInput
:
SendInput function (Windows)[^].
In case you use .NET, you can do it via P/Invoke. It's already done for you: http://www.pinvoke.net/default.aspx/user32.sendinput[^].
This function is low-level, it has the same effect as physical mouse device produces, no matter what application handles the event. It's a common mistake to try to do it by sending or posting a Windows message; not all applications and control use message pumping.
—SA
这篇关于如何全局控制鼠标(在表单之外)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!