MacOSX下Visual Studio中C#终端控制台应用启动时全屏实现
在macOS上实现C#控制台应用启动自动全屏
macOS的控制台窗口控制没有跨平台的原生C# API支持,我们可以通过调用系统的osascript执行AppleScript脚本,模拟终端全屏快捷键来实现需求。
完整实现代码
using System; using System.Diagnostics; namespace FullScreenConsoleDemo { class Program { static void Main(string[] args) { // AppleScript脚本:将当前终端窗口设为前端并触发全屏 var fullScreenScript = @"tell application ""Terminal"" to set frontmost of window 1 to true tell application ""System Events"" to tell process ""Terminal"" keystroke ""f"" using {command down, control down} end tell"; // 调用osascript执行脚本 var process = Process.Start(new ProcessStartInfo { FileName = "/usr/bin/osascript", Arguments = $"-e '{fullScreenScript}'", UseShellExecute = false, CreateNoWindow = true }); process?.WaitForExit(); // 这里可以添加你的业务逻辑代码 Console.WriteLine("终端已进入全屏状态,按任意键退出..."); Console.ReadKey(); } } }
关键说明
- 核心逻辑:通过模拟终端的全屏快捷键
Command+Control+F来触发全屏,这是macOS系统终端的默认全屏快捷键 - 权限配置:首次运行时系统会弹出权限提示,需要在系统设置 > 隐私与安全性 > 辅助功能中,给你的调试终端(或Visual Studio)添加权限,否则脚本无法模拟按键操作
- 适配iTerm2:如果使用iTerm2作为终端,只需将脚本中的
Terminal替换为iTerm即可
内容的提问来源于stack exchange,提问作者user22181550
相关产品推荐
相关产品推荐

