C#KEYEVENTF_KEYUP在特定应用中不起作用 [英] C# KEYEVENTF_KEYUP doesn't work in specific app

查看:920
本文介绍了C#KEYEVENTF_KEYUP在特定应用中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要模拟一些键,这里是代码:

I need to emulate some keys, here the code:

[DllImport("user32.dll")]
private static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

private const int KEYEVENTF_EXTENDEDKEY = 1;
private const int KEYEVENTF_KEYUP = 2;

public static void KeyDown(Keys vKey)
{
    keybd_event((byte)vKey, 0, KEYEVENTF_EXTENDEDKEY, 0);
}

public static void KeyUp(Keys vKey)
{
    keybd_event((byte)vKey, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
}

KeyboardSend.KeyDown(Keys.Z);
KeyboardSend.KeyUp(Keys.Z);

但是在一个特定的应用程序中,"KeyUp"方法不起作用,并且在我按下键盘上的键之前一直被按下.

But in one specific application "KeyUp" method doesn't work and is depressed until i press key on keyboard.

我做错了什么?

推荐答案

我在 Ryan West 的解决方案中看到的唯一变化是,他从调用函数中删除了KEYEVENTF_EXTENDEDKEY标志, c3>在KeyDownKeyUp方法中的第三个参数中,因此,在KeyDown方法中,调用keybd_event函数的第三个参数为0,而在KeyUp方法中为仅KEYEVENTF_KEYUP. 仅有很小的变化就导致KeyUp方法起作用并解决了您的问题.

The only change I see in Ryan West's solution, is that he removed the KEYEVENTF_EXTENDEDKEY flag away from calling function keybd_event in the third argument, in both KeyDown and KeyUp methods, so as a result, in KeyDown method, the third argument of calling keybd_event function is 0, and in KeyUp method, it is only KEYEVENTF_KEYUP. That only small change caused KeyUp method to work and solve your problem.

适当地阅读MSDN网站中的keybd_event功能,如果您之前阅读过,甚至再次.

Read about keybd_event function in MSDN site properly, and even again, if you read before.

它们提供有关该函数及其参数的信息和详细信息.他们还解释了您可以在keybd_event函数的第三个参数中使用的所有标志,尤其是问题KEYEVENTF_EXTENDEDKEY的情况.

They give information and details about that function and its arguments. They also explain all the flags that you can use in the third argument of keybd_event function, especially the case in your problem KEYEVENTF_EXTENDEDKEY.

如果您将正确阅读 也许,您将理解为什么KEYEVENTF_EXTENDEDKEY标志导致您的KeyUp方法 不起作用,以及为什么必须删除该标志才能使您的KeyUp方法起作用.

If you will read properly and again, maybe you will understand why KEYEVENTF_EXTENDEDKEY flag causes your KeyUp method not to work, and why you have to remove that flag, in order to cause your KeyUp method to work.

可能还可以学习如何在未来中正确调用keybd_event功能 ,这样就不会出现任何问题

You may also learn how, in the future, to call keybd_event function correctly, so you won't get any problem with it.

这篇关于C#KEYEVENTF_KEYUP在特定应用中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆