gpt4 book ai didi

c# - 将键盘快捷键添加到控制台应用程序 - 单声道兼容

转载 作者:太空宇宙 更新时间:2023-11-03 14:02:49 24 4
gpt4 key购买 nike

我正在检查在我的控制台应用程序运行时按下了哪些键盘按钮和组合。

我想做的一件事是在控制台应用程序中捕获 Ctrl+C,这样它就不会退出,而是进行一些整理并优雅地关闭。

感谢您提供的任何帮助

最佳答案

如果你在 Unix/Linux 下运行使用 Mono.UnixSignal

我在我的 Linux 应用程序上使用它。优点是它还会检测重启或系统关闭。

本例来自this Mono FAQ page

// Catch SIGINT and SIGUSR1
UnixSignal[] signals = new UnixSignal [] {
new UnixSignal (Mono.Unix.Native.Signum.SIGINT),
new UnixSignal (Mono.Unix.Native.Signum.SIGUSR1),
};

Thread signal_thread = new Thread (delegate () {
while (true) {
// Wait for a signal to be delivered
int index = UnixSignal.WaitAny (signals, -1);

Mono.Unix.Native.Signum signal = signals [index].Signum;

// Notify the main thread that a signal was received,
// you can use things like:
// Application.Invoke () for Gtk#
// Control.Invoke on Windows.Forms
// Write to a pipe created with UnixPipes for server apps.
// Use an AutoResetEvent

// For example, this works with Gtk#
Application.Invoke (delegate () { ReceivedSignal (signal); }
});

关于c# - 将键盘快捷键添加到控制台应用程序 - 单声道兼容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10283860/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com