模拟在另一个应用程序按键和keyrelease? [英] Simulating a keypress AND keyrelease in another application?
问题描述
我需要与外部应用程序的运行交互,发送特定的按键和放大器;版本。我试图使用SendKeys类,但它确实只是工作的一半,作为按键正接受立即keyrelease到外部应用程序发送的。
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.
我需要能够以模拟键保持向下,用于外部应用程序。现在我尝试使用SendMessage函数的事情,但现在它不会在所有的工作:(我甚至不出错。
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.
推荐答案
好吧,案件告破。我实际安装VC ++尝试核心keybd_event()函数,之后它的工作,我能够使用它的明智在C#。
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#.
下面的代码,并令人惊讶的是非常简单的,您需要添加此使用您的代码能够导入dll的:使用System.Runtime.InteropServices;
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;
这代码将按住1键3秒,然后将释放1秒钟,重复这个过程。
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 '}')
namespace ConsoleApplication1
{
class Program
{
[DllImport("user32.dll")]
static extern void keybd_event(byte bVk, byte bScan, uint dwFlags,UIntPtr dwExtraInfo);
static void Main(string[] args)
{
while (true)
{
keybd_event((byte)0x31, (byte)0x02, 0, UIntPtr.Zero);
Thread.Sleep(3000);
keybd_event((byte)0x31, (byte)0x82, (uint)0x2, UIntPtr.Zero);
Thread.Sleep(1000);
}
}
}
}
这篇关于模拟在另一个应用程序按键和keyrelease?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!