0

Now here is the situation we are in:

     getinput:
     //things happen here
     string typed = Console.ReadLine();
     try
     if (typed == "com")
     {
           //things happen here
     }
     else if  (Console.ReadKey(true).Key == (ConsoleKey.F1) + (ConsoleModifiers.Alt)) 
     {
           System.Environment.Exit(0);
     }
     //other else if's happen here
     else
     {
           Console.WriteLine("\n" + typed + " isn't an option.");
           goto getInput;
     }
     }
     catch (Exception)
     {
     }
     goto getInput;

what i want to do with this is when i press alt+f1 the program will terminate itself however because the program waits for some input from me to write even with the working version (without the alt part) it wants me to type the things then press enter, which i dont want. how does one handlde this??

aGoodFellow
  • 41
  • 10
  • You have to wait for an input, `Console.ReadLine()`, on another thread, while main thread waits only for special characters, like termination command, in your case. – Tigran Aug 01 '18 at 14:52
  • If you want to be able to interrupt at any time, then you cannot use `Console.ReadLine` at all, as it won't return until you press enter, giving no chance to check for the termination sequence. – Alejandro Aug 01 '18 at 15:40
  • 1
    A user is not going to discover that keystroke by himself. He already knows how terminate the program, he'll click the console's Close button or type Ctrl+C. No help needed. If you want to do this anyway then you need to consistently use ReadKey(), [like this](https://stackoverflow.com/a/19735111/17034). – Hans Passant Aug 01 '18 at 15:43

2 Answers2

0
        static void Main(string[] args)
        {
            Console.TreatControlCAsInput = true;
            var typed = ReadLine();
            if (typed == "com")
            {
                Console.WriteLine("com");
                //things happen here
            }
            //other else if's happen here
            else
            {
                Console.WriteLine("\n" + typed + " isn't an option.");

            }


        }
        public static string ReadLine() {
            StringBuilder sb = new StringBuilder();
            do
            {
                ConsoleKeyInfo key = Console.ReadKey();
                if ((key.Modifiers & ConsoleModifiers.Alt) != 0)
                {
                    if (key.Key == ConsoleKey.K)
                    {
                        Console.WriteLine("killing console");
                        System.Environment.Exit(0);

                    }
                }
                else
                {
                    sb.Append(key.KeyChar);
                    if (key.KeyChar == '\n'||key.Key==ConsoleKey.Enter)
                    {
                        return sb.ToString();
                    }
                }
            } while (true);

        }

that code will help you with your problem, just be aware that when you reading a line by char's you will need to handle things like backspace

Or Yaacov
  • 3,597
  • 5
  • 25
  • 49
  • There is a slight problem, if i type the code under 'string typed' line it doesnt read at all and if i type it over 'string typed' line every time i want to enter another entry in program (aside form the key press) it wants me to do it twice. – aGoodFellow Aug 01 '18 at 16:32
  • do you mean that you reading a line at a time and not keys? if not, can you edit your question and add some more of your code? – Or Yaacov Aug 01 '18 at 18:34
  • Just edited few things. I belive that last else might have something to do with the problem. – aGoodFellow Aug 01 '18 at 19:45
  • look at my last edit, and mark if it solve your problem :) – Or Yaacov Aug 01 '18 at 20:14
  • The key works but sadly there is still a problem, i tried to do some debugging, and i belive i find the problem too but i cannot find a solution for it. So the problem goes like this: [link](https://i.imgur.com/A3i02hS.png) When i debugged it i find a conclusion; it probably stll tries to read the key when i input a string and it does not accept that string as an input. – aGoodFellow Aug 01 '18 at 22:41
  • @aGoodFellow - Edited my answer. Should work as expected now. – oRole Aug 02 '18 at 11:33
0

First of all, please consider using loops instead of goto, as gotos are dangerous. Why? Have a look here: 'Goto' is this bad?

To solve your problem you can use the ConsoleKeyInfo class in combination with the Console.ReadKey() method to get information about single key presses. With this you can check for any key-combination right before adding up the single characters to a string. A working example could look like:

namespace Stackoverflow
{
    using System;

    class Program
    {
        public static void Main(string[] args)
        {    
            ConsoleKeyInfo keyInfo = default(ConsoleKeyInfo); 
            string input = string.Empty;

            // loop while condition is true
            while (true)
            {
                // read input character-wise as long as user presses 'Enter' or 'Alt+F1'
                while (true)
                {
                    // read a single character and print it to console
                    keyInfo = Console.ReadKey(false);

                    // check for close-combination
                    if (keyInfo.Key == ConsoleKey.F1 && (keyInfo.Modifiers & ConsoleModifiers.Alt) != 0)
                    {
                        // program terminates
                        Environment.Exit(0);
                    }

                    // check for enter-press
                    if (keyInfo.Key == ConsoleKey.Enter)
                    {
                        // break out of the loop without adding '\r' to the input string
                        break;
                    }

                    // add up input-string
                    input += keyInfo.KeyChar;
                }

                // optional: enter was pressed - add a new line
                Console.WriteLine();

                // user pressed enter, do something with the input
                try
                {
                    if (input == "com")
                    {
                        // right option - do something
                    }
                    else
                    {
                        // wrong option - reset ConsoleKeyInfo + input
                        Console.WriteLine("\n" + input + " isn't an option.");
                        keyInfo = default(ConsoleKeyInfo);
                        input = string.Empty;
                        continue;
                    }
                }
                catch (Exception)
                {
                    // handle exceptions
                }
            }
        }
    }
}
oRole
  • 1,316
  • 1
  • 8
  • 24
  • sorry for the late response. when i entered an input for the first time it does not read and wants me to enter again, when i enter for the second time it works but it wants me to enter inputs for two times and in the second time it does not register alt+f1, i tried to debug it but even with that i couldn't understand, im slowly start to think that i either have to give up on this or just relase all the code. – aGoodFellow Aug 02 '18 at 20:00
  • Do you use the piece of code that I posted or did you adapt yours? If I execute the code above, it works exactly how it should. Can you post the code that you have now (update/edit your question), so I can have a look at it again? – oRole Aug 02 '18 at 20:15
  • i belive the problem lies in the command `string typed = Console.ReadLine().ToLower();` i have in the code (which is crucial for my code to work here) those two are colliding with eachother. Also i forgot the answer, the reason i still use gotos is i dont know how to oparate without them, and its realy hard to find a decent guide that covers all the codes i should use for not using goto for sort im avare of how bad goto is but because its a shortcut i will eventualy learn better ways, i dont mind it that much right now. – aGoodFellow Aug 02 '18 at 21:30
  • Unfortunately I'm not able to help you If you don't share the code you are using. You can simply replace your posted piece of code with that one I've posted. You can replace `getInput:` by `while(true) { }` and `goto getInput` by continue. If you want to end the loop use `break` or place a condition into `while(condition) {}`. – oRole Aug 02 '18 at 22:02
  • Im so sorry for taking so long, i had a lots of other things to do today and i started to change all the gotos to other things (works wonderfully thank you for informing me!) and i didn't wanted to give you an unstable code, its really late today and i couldn't tinker around with my code too much so im gonna finish the working build tomorrow. Also is it ok if i pm my code im really embarrassed by how simple yet messy my code is. – aGoodFellow Aug 03 '18 at 23:35
  • umm.. you havent aswered yet, is everything ok? can i send the cod via pm of some sort?? – aGoodFellow Aug 05 '18 at 19:35