在另一个应用程序中模拟按键和按键释放?
我需要与正在运行的外部应用程序交互,并发送特定的按键和消息。发布。我尝试使用 SendKeys 类,但它只完成了一半的工作,因为按键是通过立即按键释放发送到外部应用程序的。
我需要能够模拟外部应用程序的“按键按下”。我现在尝试使用 SendMessage 东西,但现在它根本不起作用:(而且我什至没有收到错误。
I need to interact with an external application running, and send specific keypresses & releases. I've tried to use the SendKeys class, but it does only half of the job, as the keypress is being sent with an immediate keyrelease to the external applications.
I need to be able to simulate a "key hold down" for the external app. I'm now trying to use the SendMessage thing, but for now it won't work at all :( and I don't even get errors.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
好的,案子解决了。我实际上安装了 VC++ 来尝试核心 keybd_event() 函数,在它工作后我能够在 C# 中明智使用它。
这是代码,令人惊讶的是它非常简单。您需要将此 using 添加到您的代码中才能导入 dll: using System.Runtime.InteropServices;
此代码将按住“1”按钮 3 秒,然后将松开 1 秒并重复该过程。
(代码高亮显示混乱了:/,从“命名空间...”复制到最后一个括号“}”)
Ok, case solved. I actually installed VC++ to try the core keybd_event() function, and after it worked I was able to use it wisely in C#.
Here's the code, and surprisingly it's very simple. You'll need to add this using to your code to be able to import dll's: using System.Runtime.InteropServices;
This code will press and hold the '1' button for 3 secs, and then will release for 1 second and repeat the process.
(the code highlight got messed up :/, copy from 'namespace ...' to the last bracket '}')
您是否尝试过使用 PostMessage 发送
WM_KEYDOWN
和WM_KEYUP
?编辑
你会这样使用它(我用 C++ 编写,但你可以轻松使用 PInvoke 和 ..NET)
have you tried using PostMessage to send
WM_KEYDOWN
andWM_KEYUP
?Edit
You would use it this way (I am writing in C++, but you can easily use PInvoke and ..NET)
您可以使用 WSH Scripting Shell 来执行此操作:
您所需要做的就是添加对“Windows Scripting Host Object”版本 1.0 的 COM 引用。一切都在命名空间
IWshRuntimeLibrary
中。You can use the WSH Scripting Shell to do this:
All you need to do is add a COM reference to "Windows Scripting Host Object", version 1.0. Everything is in the namespace
IWshRuntimeLibrary
.官方API是
SendInput
< /a>.The official API is
SendInput
.