Click here to Skip to main content
15,891,423 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a console app which I have inherited. I have added in a routine for handling the keyboard press of CTRL-D. Unfortunately I have discovered there is an existing CTRL-BREAK handler in place and my routine is effectively wiping out its operation.

Is there a way to include additional keys into the keyboard handler?
I'd like to add in CTRL_D if at all possible.

What I have tried:

Here's some of the code for the handler. Its nothing special and the same as web examples.
C++
private static bool TermHandlerRoutine(CtrlTypes dwCtrlType)
       {
           switch (dwCtrlType)
           { //no break so all close events handled same
               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
       }



BTW, what does it mean when there is no value after the flag. ie CTRL_BREAK_EVENT = 1,
Posted
Updated 25-Feb-16 6:36am
v2
Comments
Sergey Alexandrovich Kryukov 25-Feb-16 0:43am    
And where did you try to handle Ctrl-D? And why? Say, you DLL-import SetConsoleCtrlHandler, but where do you call it?
Anyway, why not staying with just .NET FCL, why abusing console behavior, which, by the way, includes standard CancelKeyPress event (to handle Ctrl+C without P/Invoke)?

Using P/Invoke without really serious reasons is generally bad, and using it for nothing is even worse. Honestly, if you want to handle different keys, create a windowed application, which, by the way, can mimic console.

System.Console is designed for simple utilities, mostly with input using a command line with no interactive input, excluding, perhaps, some Ctrl-C handling...

—SA
Member 11709930 25-Feb-16 16:06pm    
I have this in the main loop-
ConsoleKeyInfo cki;
cki = Console.ReadKey();
if ((cki.Modifiers & ConsoleModifiers.Control) != 0)
{
if (cki.Key == ConsoleKey.D)
// do stuff
{}
}

however this seems to be absorbing the windows message as it never fires the ctrlBreak handler whilst this is in place.

I didn't write the original app but I believe console was used for outright speed. This thing runs a heap of multithreading in a real time stock market application.

1 solution

The Console Control Handlers are not intended to be used for "normal" input. They handle a set of special key combinations and Windows signals to terminate the console application. If you need to act upon a special key combination choose one that is not reserved and handle it using normal input techniques (e.g. Console.KeyAvailable and Console.ReadKey).


Quote:
BTW, what does it mean when there is no value after the flag. ie CTRL_BREAK_EVENT = 1,

When no value is specified, the value of the previous enumerator plus one is assigned. When there is no value for the first enumerator, zero is assigned. See enum (C# Reference)[^].
 
Share this answer
 
Comments
Member 11709930 25-Feb-16 15:59pm    
Thanks Jochen
So CTRL_BREAK_EVENT, becomes CTRL_BREAK_EVENT=1, by default. Didn't know that and explains things nicely.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900