我一直在编写一个在 Windows 上运行良好的 C# 应用程序。它控制键盘和鼠标,并将击键发送到当前打开的程序。
但是,我想将我的应用程序移植到 Linux,因此我不能使用我目前使用的 MouseKeyboardLibrary.dll 正在做非常 Windows 特定的事情。
是否有一个库可以让我轻松地将键盘和鼠标移动发送到 X11 或 Xorg 中的程序?
我找不到任何已经制作好的东西。但我为你找到了一个起点:xdotool是一个从命令行控制鼠标和键盘的项目。它也是开源的,所以如果你愿意,你可以在 C# 中本地实现相同的功能(或者如果需要,可以使用一些 P/Invokes)。
public static bool LinuxOS
{
get { return Path.DirectorySeparatorChar == '/'; }
}
public static void SendKeys(String output)
{
if (LinuxOS)
{
var args = "";
switch (output)
{
case "{RIGHT}":
args = "key Right";
break;
case "{LEFT}":
args = "key Left";
break;
default:
if (output.StartsWith("{") && output.EndsWith("}"))
output = output.Substring(1, output.Length - 2);
args = "type \"" + output + "\"";
break;
}
var proc = new Process
{
StartInfo =
{
FileName = "xdotool",
Arguments = args,
UseShellExecute = false,
RedirectStandardError = false,
RedirectStandardInput = false,
RedirectStandardOutput = false
}
};
proc.Start();
}
else
{
System.Windows.Forms.SendKeys.Send(output);
}
}