运行 Windows SendInput API 时监视器闪烁 [英] Monitor flashing when running a Windows SendInput API

查看:66
本文介绍了运行 Windows SendInput API 时监视器闪烁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗯,我当然应该去python,因为我做了几个这种类型的函数,键盘事件和鼠标事件,但决定尝试学习windows api.

Well, I certainly should go to python since I did several functions of this type, keyboard event and mouse event, but decide to try to learn the windows api.

我的目标是知道鼠标的按钮 1 何时被按下.

My goal is to know when button 1 of the mouse is pressed.

我以非常初学者的方式创建了这个文件,它在 mouseData 中仅返回 0.奇怪的是,每当我运行它时,它会以很短的时间间隔闪烁我的显示器,但在关闭它的情况下会在 1 秒之间闪烁.很奇怪,执行是不可行的.

I created this file in a very beginner way, it returns in mouseData only 0. The curious thing is that whenever I run it, it flashes my monitor at short intervals in blinks, but between 1 second with it off. Very strange that, execution is not viable.

谁能帮我理解并尝试执行,看看它是否只在这里.

Could someone help me understand and try to execute to see if it is only here.

代码:

int main()
{
    DWORD mouseData = 0;

    MOUSEINPUT tagMouse;
    tagMouse.dx = 0;
    tagMouse.dy = 0;
    tagMouse.mouseData = mouseData;
    tagMouse.dwFlags = MOUSEEVENTF_XDOWN;
    tagMouse.dwExtraInfo = 0;

    INPUT tagInput;
    tagInput.type = INPUT_MOUSE;
    tagInput.mi = tagMouse;

    while (true) {
        if (GetAsyncKeyState(VK_DELETE)) break;
        SendInput(1, &tagInput, sizeof(INPUT));
        printf("KEYWORD: %d\n", mouseData);
        Sleep(500);
    }
    system("pause");
    return 0;
}

推荐答案

可以重现你报告的症状"——效果真的很残酷!

I can reproduce your reported 'symptoms' - and the effect is really brutal!

现在,虽然我无法提供完整的解释,但我可以提供修复!您的 tagMouse 结构中有一个未初始化的字段(time 成员,它是系统使用的时间戳).将此设置为零(这会告诉系统生成自己的时间戳)可以解决问题.因此,只需将此行添加到您的其他初始化语句中:

Now, while I cannot offer a full explanation, I can offer a fix! You have an uninitialized field in your tagMouse structure (the time member, which is a time-stamp used by the system). Setting this to zero (which tells the system to generate its own time-stamp) fixes the problem. So, just add this line to your other initializer statements:

//...
    tagMouse.dwExtraInfo = 0;
    tagMouse.time = 0; // Adding this line fixes it!
//...

希望这会有所帮助!

注意:我也希望得到更全面的解释;然而,对我来说,一个未初始化的字段闻起来像是未定义的行为!我已经为 time 字段尝试了各种其他值(即不是零),但还没有找到一个有效的.

Note: I, too, would appreciate a fuller explanation; however, an uninitialized field, to me, smells like undefined behaviour! I have tried a variety of other values (i.e. not zero) for the time field but haven't yet found one that works.

讨论 此处在 devblogs可能 帮助.这句话似乎很相关:

The discussion here on devblogs may help. This quote seems relevant:

谁知道如果程序检查会造成什么样的破坏时间戳和通知,它们要么来自未来,要么时光倒流.

And who knows what sort of havoc that will create if a program checks the timestamps and notices that they are either from the future or have traveled back in time.

这篇关于运行 Windows SendInput API 时监视器闪烁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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