无法使SendInput()工作 [英] Can't get SendInput() to work

查看:185
本文介绍了无法使SendInput()工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有点绝望.我已经尝试了几个小时,但是我无法使SendInput()正常工作.老实说,我什至无法得到认可.它总是说:

I'm kind of desperate. I have been trying for hours now, but I just can't get SendInput() to work. To be honest, I can't even get it to be recognized. It always says:

Error   1   The type or namespace name 'INPUT' could not be found (are you missing a using directive or an assembly reference?)

我只是找不到要使用的库.关于要包含的内容的信息几乎为零,我所能找到的全部是C ++的,或者当我尝试using时根本不存在的信息.请帮忙!

And I just can't find out which libraries to use. There's almost zero information about what to include for this, all I can find is for C++ or is simply not existing when I try using it. Please Help!

我正在尝试让我的程序执行鼠标单击操作……这是代码,它是我发现并试图开始工作的众多版本之一. 在此版本中,该程序也找不到INPUTSendInputEventType

I'm trying to make my program do a mouseclick... Here's the code, it's one of many versions I found and tried to get to work. In this version, the program also can't find INPUT and SendInputEventType

using System;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace autoPlayer
{
    class Win32
    {
        enum SystemMetric
        {
          SM_CXSCREEN = 0,
          SM_CYSCREEN = 1,
        }

        [DllImport("user32.dll")]
        static extern int GetSystemMetrics(SystemMetric smIndex);

        int CalculateAbsoluteCoordinateX(int x)
        {
          return (x * 65536) / GetSystemMetrics(SystemMetric.SM_CXSCREEN);
        }

        int CalculateAbsoluteCoordinateY(int y)
        {
          return (y * 65536) / GetSystemMetrics(SystemMetric.SM_CYSCREEN);
        }

        public static void ClickLeftMouseButton(int x, int y)
        {
            INPUT mouseInput = new INPUT();
            mouseInput.type = SendInputEventType.InputMouse;
            mouseInput.mkhi.mi.dx = CalculateAbsoluteCoordinateX(x);
            mouseInput.mkhi.mi.dy = CalculateAbsoluteCoordinateY(y);
            mouseInput.mkhi.mi.mouseData = 0;


            mouseInput.mkhi.mi.dwFlags = MouseEventFlags.MOUSEEVENTF_MOVE | MouseEventFlags.MOUSEEVENTF_ABSOLUTE;
            SendInput(1, ref mouseInput, Marshal.SizeOf(new INPUT()));

            mouseInput.mkhi.mi.dwFlags = MouseEventFlags.MOUSEEVENTF_LEFTDOWN;
            SendInput(1, ref mouseInput, Marshal.SizeOf(new INPUT()));

            mouseInput.mkhi.mi.dwFlags = MouseEventFlags.MOUSEEVENTF_LEFTUP;
            SendInput(1, ref mouseInput, Marshal.SizeOf(new INPUT()));
        } 
    }
}

如果有人能帮助我,我会很高兴!

I would be so glad If anyone could help!

推荐答案

您缺少INPUT结构.并非您想的那样(using猜测),不是来自System.Windows.Input.

You are missing the INPUT structure. It doesn't come from System.Windows.Input if that's what you thought (guessing due to using).

您可以在此处

您的SendInputEventType实际上只是一个枚举或一个带有常量的类. PInvoke也已定义了.

Your SendInputEventType is really just going to be an enum or a class with constants. PInvoke has that defined as well.

internal enum INPUT_TYPE : uint 
{
      INPUT_MOUSE = 0,
      INPUT_KEYBOARD = 1,
      INPUT_HARDWARE = 2
}

您可以更新代码以使用此名称,也可以重命名此枚举以匹配您的代码.由你决定.

You can update your code to use this name or rename this enum to match your code. It's up to you.

这是一个很好的答案,其中有一个示例,该示例定义了以下结构来实现SendInput:如何以编程方式鼠标移动,单击,右键单击并在Winform和WPF中按键等?

Here is a great answer that has an example of implementing SendInput with those structures defined: how to programatically mouse move,click,right click and keypress, etc. in winform and wpf?

这篇关于无法使SendInput()工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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