diff --git a/BeefLibs/corlib/src/Console.bf b/BeefLibs/corlib/src/Console.bf index b48277e8..12461396 100644 --- a/BeefLibs/corlib/src/Console.bf +++ b/BeefLibs/corlib/src/Console.bf @@ -7,6 +7,12 @@ namespace System { public static class Console { + public enum CancelKind + { + CtrlC, + CtrlBreak + } + static Encoding InputEncoding = Encoding.ASCII; static Encoding OutputEncoding = Encoding.ASCII; @@ -16,6 +22,9 @@ namespace System static readonly ConsoleColor sOriginalForegroundColor = sForegroundColor; static readonly ConsoleColor sOriginalBackgroundColor = sBackgroundColor; + static Event sOnCancel ~ _.Dispose(); + static bool sCancelEventRegistered; + public static ConsoleColor ForegroundColor { get { return sForegroundColor; } @@ -42,6 +51,31 @@ namespace System public uint16[2] mMaximumWindowSize; } + public static ref Event OnCancel + { + get + { + if (!sCancelEventRegistered) + { + sCancelEventRegistered = true; +#if BF_PLATFORM_WINDOWS + SetConsoleCtrlHandler(=> ConsoleCtrlHandler, true); +#endif + } + return ref sOnCancel; + } + } + +#if BF_PLATFORM_WINDOWS + [CallingConvention(.Stdcall)] + public static Windows.IntBool ConsoleCtrlHandler(int32 ctrlType) + { + bool terminate = true; + if ((ctrlType == 0) || (ctrlType == 1)) + sOnCancel((.)ctrlType, ref terminate); + return terminate ? false : true; + } + [CLink, CallingConvention(.Stdcall)] static extern int SetConsoleTextAttribute(void* hConsoleOutput, uint16 wAttributes); @@ -51,7 +85,11 @@ namespace System [CLink, CallingConvention(.Stdcall)] static extern void* GetStdHandle(uint32 nStdHandle); -#if BF_PLATFORM_WINDOWS + [CallingConvention(.Stdcall)] + function Windows.IntBool ConsoleCtrlHandler(int32 ctrlType); + [CLink, CallingConvention(.Stdcall)] + static extern Windows.IntBool SetConsoleCtrlHandler(ConsoleCtrlHandler handler, Windows.IntBool addHandler); + public static this() { let handle = GetStdHandle(STD_OUTPUT_HANDLE);