使用 Lua 控制鼠标的 Winform 应用

我尝试使用 Logictech 鼠标发送字符串,就像 Logictech 提供的软件一样

enter image description here

我点击前进按钮它将发送字符串,但我无法使用 Winform 发送字符串并修改它

我发现 Logictech 有G-series Lua API

我试图使用 OnEvent 函数来控制鼠标点击以发送字符串

但我以前从未接触过 Lua。

这是我的 C# 代码

using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using LuaInterface;
namespace LuaScript
{
    public partial class Form1 : Form
    {
        public Lua lua = new Lua();
        string var = "aabbcc";
        public Form1()
        {
            InitializeComponent();
            lua.DoFile("logitechSendString.lua");
            object[] objs = lua.GetFunction("OnEvent").Call(this, var);
            lua.Close();
        }
    }
}

我的 Lua 代码

function OnEvent(event, arg)
  if (arg!=null)then
    PressKey(arg)
    ReleaseKey(arg)
    Sleep(50)
    PressMouseButton("Forward")
    ReleaseMouseButton("Forward")
  end
end

但是它会编译错误

System.IO.FileLoadException
  HResult=0x80131621
  Message=Mixed mode assembly is built against version 'v2.0.50727' of the runtime and cannot be loaded in the 4.0 runtime without additional configuration information.
  Source=LuaInterface
  StackTrace:
   LuaInterface.Lua..ctor()
   LuaScript.Form1..ctor()  D:\Visual Studio\LuaScript\LuaScript\Form1.cs: 15
   LuaScript.Program.Main()  D:\Visual Studio\LuaScript\LuaScript\Program.cs:19

如何解决这个错误?

如何使用 Lua 控制鼠标?

谢谢

点赞
用户1847592
用户1847592

这是一种不需要C#的解决方案。

字符串“aabbcc”存储在LGS脚本中。

在您的个人资料子菜单中选择Scripting,并复制此Lua脚本:

-- 当用户按下中间鼠标按钮时,会发生以下情况:
--   1. 模拟输入字符串“aabbcc”
--   2. 模拟“向前”(X2)鼠标按钮的按下和释放
function OnEvent(event, arg, family)
   if event == "MOUSE_BUTTON_PRESSED" and arg == 3 then  -- 按下中间鼠标按钮
      PressAndReleaseKey("a", "a", "b", "b", "c", "c")   -- 在此更改您的字符串
      Sleep(50)           -- 50毫秒等待
      PressMouseButton(5) -- 5 = X2(向前)
      Sleep(50)
      ReleaseMouseButton(5)
   end
end

要用另一个字符串替换“aabbcc”,您应该再次转到“Scripting”菜单项,修改文本并保存(Ctrl-S)。

2020-02-23 13:47:34