简体   繁体   English

SendInput发送鼠标输入和键盘输入

[英]SendInput sending mouse input AND keyboard input

=) =)

I am using C++ (not VC++) on windows7 laptop. 我在Windows7笔记本电脑上使用C ++(不是VC ++)。

I have a problem with this method for moving the mouse x/y from it's current position. 使用此方法从当前位置移动鼠标x / y时,我遇到了问题。 Every time it calls send input for a mouse event it moves the mouse but also turns off my screen (the equivalent of Fn+F2). 每次它为鼠标事件调用send输入时,它都会移动鼠标,但也会关闭我的屏幕(相当于Fn + F2)。 I debugged the program and noticed that not only did mi but also ki and hi had values (this was for x=25, y=25 and absolure=false): 我调试了程序,发现mikihi都具有值(这是x = 25,y = 25和absolure = false的值):

    mi:
      dx            25
      dy            25  
      mouseData     0   
      dwFlags       1   
      time          2686400 
      dwExtraInfo   0   
    ki:
      wVk           25  
      wScan         0
      dwFlags       25  
      time          0   
      dwExtraInfo   1   
    hi:
      uMsg          25
      wParamL       25  
      wParamH       0       

I have tried to set ki and hi to 0 but if I do that then mi is also set to 0 and then no mouse moves but the screen is still put to sleep! 我尝试将ki和hi设置为0,但是如果我这样做,则mi也设置为0,然后没有鼠标移动,但屏幕仍然处于睡眠状态! :$ :$

Here are some of the methods I used. 这是我使用的一些方法。

int Controller::mouse_move(long x, long y, bool absolute) {
   mouse.dx = x;
   mouse.dy = y;

   if (absolute) {
       mouse.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE;
   }else{
       mouse.dwFlags = MOUSEEVENTF_MOVE;
   }

   mouse.mouseData = 0;
   mouse.dwExtraInfo = 0;

   input.type = INPUT_MOUSE;
   input.mi = mouse;

   SendInput(1, &input, sizeof (input));

   return 0;
}

OR 要么

int Controller::mouse_move(long x, long y, bool absolute, int index_vector_no) {
    input.type = INPUT_MOUSE;
    input.mi.dx = x;
    input.mi.dy = y;

    if (absolute) {
        input.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE;
    }else{
        input.mi.dwFlags = MOUSEEVENTF_MOVE;
    }

    input.mi.mouseData = 0;
    input.mi.dwExtraInfo = 0;

    input.ki.dwExtraInfo = 0;
    input.ki.dwFlags = 0;
    input.ki.time = 0;
    input.ki.wScan = 0;
    input.ki.wVk = 0;

    input.hi.uMsg = 0;
    input.hi.wParamH = 0;
    input.hi.wParamL = 0;

    SendInput(1, &input, sizeof (input));

    return 0;
}

Can anyone thing why this is sending keyboard input as well as mouse?! 有人能解释为什么这会发送键盘输入和鼠标吗? is it just something to do with my laptop configuration? 这与我的笔记本电脑配置有关吗?

Thanks in advance! 提前致谢! =) =)

The reason the others change is because they're all in a union, so they share the same memory. 其他人更改的原因是因为他们都在一个联合体中,所以他们共享相同的内存。

About the screen turning off, you didn't set mi.time to 0, so it's uninitialized. 关于屏幕关闭,您没有将mi.time设置为0,因此未初始化。 It's often a good idea to start initializing these structs to {0}, so you don't forget things. 开始将这些结构初始化为{0}通常是一个好主意,这样您就不会忘记事情。 The timestamp becomes some random number, which can cause a timeout on the display. 时间戳变为某个随机数,这可能会导致显示器超时。

You can use this: 您可以使用此:

input.mi.time = 0;

Before: 之前:

input.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP;

It works for me. 这个对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM