windows 如何在不触摸系统级别的任何输入设备的情况下触发按键或鼠标单击事件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4057810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I fire a key press or mouse click event without touching any input device at system level?
提问by Javed Akram
How can I fire an automatic key press or mouse click event when a color appears on the screen on other application or browser?
当其他应用程序或浏览器的屏幕上出现颜色时,如何触发自动按键或鼠标单击事件?
回答by CodesInChaos
It depends a lot on what you want. Do you want to send the keys to
这在很大程度上取决于你想要什么。你想把钥匙寄给
- your Application
- another fixed Application
- Simulate a global keypress
- 你的申请
- 另一个固定的应用程序
- 模拟全局按键
Simulating keys globally
全局模拟密钥
All of these will cause problems targeting a specific application and the active window changes.
所有这些都会导致针对特定应用程序和活动窗口更改的问题。
SendKeys
Sends Messages to the active app. It's a high level function taking a string which encodes a sequence of keys.keybd_event
is very low level and injects a global keypress. In most casesSendKeys
is easier to use.mouse_event
simulates mouse input.SendInput
supersedes these functions. It's more flexible but a bit harder to use.
SendKeys
向活动应用程序发送消息。它是一个高级函数,它采用一个对一系列键进行编码的字符串。keybd_event
非常低级别并注入全局按键。在大多数情况下SendKeys
更容易使用。mouse_event
模拟鼠标输入。SendInput
取代这些功能。它更灵活,但使用起来有点困难。
Sending to a specific window
发送到特定窗口
When working with a fixed target window, sending it messages can work depending on how the window works. But since this doesn't update all statesit might not always work. But you don't have a race condition with changing window focus, which is worth a lot.
使用固定目标窗口时,可以根据窗口的工作方式向其发送消息。但是由于这不会更新所有状态,因此它可能并不总是有效。但是你没有改变窗口焦点的竞争条件,这很有价值。
WM_CHAR
sends a character in the basic multilingual plane (16 bit)WM_UNICHAR
sends a character supporting the whole unicode rangeWM_KEYDOWN
andWM_KEYUP
Sends keys which will be translated to characters by the keyboard layout.
WM_CHAR
在基本多语言平面(16 位)中发送一个字符WM_UNICHAR
发送支持整个 unicode 范围的字符WM_KEYDOWN
和WM_KEYUP
发送将被键盘布局转换为字符的键。
My recommendation is when targeting a specific window/application try using messages first, and only if that fails try one of the lower level solutions.
我的建议是在针对特定窗口/应用程序时首先尝试使用消息,只有在失败时才尝试使用较低级别的解决方案之一。
回答by udit043
when a color appears on the screen on other application or browser
当其他应用程序或浏览器的屏幕上出现颜色时
I made one program using OpenCV and C++ for operating mouse with finger gesture. I used 3 color strips for 3 mouse function.
我使用 OpenCV 和 C++ 制作了一个程序,用于用手指手势操作鼠标。我使用了 3 个颜色条来实现 3 个鼠标功能。
- Yellow color for Left click
- Blue color for Right click
- Pink color for controlling cursor position
- 左键单击黄色
- 右键单击时为蓝色
- 用于控制光标位置的粉红色
Whenever camera detect these colors, associated function takes place, I have used mouse_eventfor performing mouse function. For more information you may read my code, blog, video.
每当相机检测到这些颜色时,相关功能就会发生,我使用mouse_event来执行鼠标功能。有关更多信息,您可以阅读我的代码、博客、视频。
回答by ?yvind Br?then
I'm not 100% sure what you want, but if all you are after is running the method linked the the button.Clicked
event, then you can manually run the method just like any other method.
我不是 100% 确定您想要什么,但是如果您所追求的只是运行与button.Clicked
事件相关联的方法,那么您可以像任何其他方法一样手动运行该方法。
回答by Mike Clark
You can use the .NET SendKeys class to send keystrokes. Emulating mouse clicks requires P/Invoke. I don't know how to detect colors on the screen.
您可以使用 .NET SendKeys 类来发送击键。模拟鼠标点击需要 P/Invoke。我不知道如何检测屏幕上的颜色。