I have a console app which I have inherited. This has a CTRL-BREAK handler in place which works fine however I have added in a routine for detecting a CTRL-D keypress but this appears to be absorbing all key press messages effectively wiping out and CTRL-C or CTRL-BREAK messages.
Is there a better way to do this or am I up pooh creek?
What I have tried:
This is part of the CTRL-BREAK handler, pretty standard stuff.
private static bool TermHandlerRoutine(CtrlTypes dwCtrlType)
{
switch (dwCtrlType)
{
case CtrlTypes.CTRL_C_EVENT:
case CtrlTypes.CTRL_CLOSE_EVENT:
case CtrlTypes.CTRL_BREAK_EVENT:
case CtrlTypes.CTRL_SHUTDOWN_EVENT:
terminated = true;
return true;
}
return false;
}
#region unmanaged
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool GenerateConsoleCtrlEvent(CtrlTypes sigevent, int dwProcessGroupId);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool SetConsoleCtrlHandler(HandlerRoutine Handler, bool Add);
public enum CtrlTypes
{
CTRL_C_EVENT = 0,
CTRL_BREAK_EVENT,
CTRL_CLOSE_EVENT,
CTRL_LOGOFF_EVENT = 5,
CTRL_SHUTDOWN_EVENT = 6
}
This is the CTRL-D detecting code which resides inside a loop-
ConsoleKeyInfo cki;
cki = Console.ReadKey();
if ((cki.Modifiers & ConsoleModifiers.Control) != 0)
{
if (cki.Key == ConsoleKey.D)
{
}
}
it detects CTRL-D nicely but seems to absorb all keypresses and not pass on anything else to the CTRL-BREAK handler.