11 Replies - 1201 Views - Last Post: 10 January 2012 - 06:16 PM Rate Topic: -----

#1 darkflaw  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 21-August 08

Hook Into Console Text Input

Posted 04 January 2012 - 05:51 PM

I am trying to make a sort of command parser for a console program. What I need to figure out is how to hook into the event that is triggered when text is input by pressing enter. I really don't want to use ReadLine as I want to free up the program for other operations and would like to completely handle text input and block the default output that it prints.

In a nutshell

Command -> Text Input Hook -> My Function -> Result

Is This A Good Question/Topic? 0
  • +

Replies To: Hook Into Console Text Input

#2 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4316
  • View blog
  • Posts: 7,485
  • Joined: 08-June 10

Re: Hook Into Console Text Input

Posted 05 January 2012 - 08:15 AM

Well, in a console program you're going to need to use ReadLine. On the other hand, Console apps don't have to be single-threaded, so you can ReadLine on the main thread, and process the input on another thread, so that you can be ready for more line reading.
Was This Post Helpful? 0
  • +
  • -

#3 darkflaw  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 21-August 08

Re: Hook Into Console Text Input

Posted 08 January 2012 - 08:32 AM

So there is no way I can hook into the text entered event and use my function?
Was This Post Helpful? 0
  • +
  • -

#4 cilaes  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 73
  • Joined: 12-December 11

Re: Hook Into Console Text Input

Posted 08 January 2012 - 10:06 PM

string userInput = Console.ReadLine();


then throw that variable as your argument to your function? Is that what you're looking for?
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5960
  • View blog
  • Posts: 23,238
  • Joined: 23-August 08

Re: Hook Into Console Text Input

Posted 09 January 2012 - 04:31 AM

We've yet to see any effort from you in attempting this. Your explanation of what you're trying to do is vague. You need to provide us more information -- like the code you have so far and how it's working vs. how you want it to work.
Was This Post Helpful? 0
  • +
  • -

#6 darkflaw  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 21-August 08

Re: Hook Into Console Text Input

Posted 09 January 2012 - 05:01 PM

Sorry for all the confusion, it is kind of hard to explain. And code wise there isn't much to show for this particular problem as I am coding a server class by class and this is the first thing that really happens in this particular class.

I will try to explain a little better with actual code examples.

Normally you would just do something like so. Ask for input, print the input and continue.
using System;
using System.Text;

namespace Server
{
    class Core
    {
        static void Main()
        {
            Console.WriteLine("Please enter a number.");
            string Input = Console.ReadLine();
            Console.WriteLine(Input);
        }
    }
}


Now this is roughly what I would like to do in a generalized form.
using System;
using System.Text;

namespace Server
{
    class Core
    {
        static void Main()
        {
            //Overwrite something here so that HandleInput() is called when text is entered instead of Console.ReadLine()

            //I would like to do this because I will eventually be parsing the text, logging it, and various other things such as using a custom output like the rest of my code does 
            //instead of the standard grey text that ReadLine() always shows that would be much easier, in my opinion, to have my own functions for.
        }

        static void HandleInput(string PossibleString)
        {
            Console.WriteLine(PossibleString);
        }
    }
}

Was This Post Helpful? 0
  • +
  • -

#7 cilaes  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 73
  • Joined: 12-December 11

Re: Hook Into Console Text Input

Posted 09 January 2012 - 05:20 PM

cant you just pass the inputted string to HandleInput()?
Was This Post Helpful? 0
  • +
  • -

#8 darkflaw  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 21-August 08

Re: Hook Into Console Text Input

Posted 09 January 2012 - 06:14 PM

Yes, is there a way to stop ReadLine() from auto printing though?
Was This Post Helpful? 0
  • +
  • -

#9 cilaes  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 73
  • Joined: 12-December 11

Re: Hook Into Console Text Input

Posted 09 January 2012 - 07:29 PM

I'm not sure if I understand? ReadLine doesn't "auto-print", you're printing it to the console using WriteLine().

Are you wanting it to HIDE what the user is typing into the console?

class Program
    {
        static void ShowPrompt(bool loop = false)
        {
            Console.Write(">> ");
            string input = Console.ReadLine();
            HandleInput(input);
            if(loop)
                ShowPrompt();
        }

        static void HandleInput(string input)
        {
            Console.WriteLine("Your input: {0}", input);
        }

        static void Main(string[] args)
        {
            ShowPrompt(true);
        }
    }

Was This Post Helpful? 0
  • +
  • -

#10 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4316
  • View blog
  • Posts: 7,485
  • Joined: 08-June 10

Re: Hook Into Console Text Input

Posted 09 January 2012 - 07:37 PM

Here's my example for this. It's a little more manual than you're expecting, you have to deal with it char by char.

public static void Main() {
    ConsoleKeyInfo k;
    do {
        k = Console.ReadKey(true);
    } while (HandleInput(k));
}

private static bool HandleInput(ConsoleKeyInfo k) {
    switch(k.KeyChar) {
        case 'c':
            Console.Write(k.KeyChar);
            break;
        case '\r':
            Console.WriteLine();
            break;
        case 'q':
            return false;
    }
    return true;
}



Console.ReadKey takes one key of input at a time. The (true) parameter means that it will not echo the entered character on screen.

In my HandleInput method, I'm doing some tests. I'm saying, "if it's a 'c', print it. If it's a line break, print a line break. If it's a 'q', quit. Otherwise, do nothing.

You could modify this to handle strings, if you wanted. Here's how I'd do that:

private static readonly StringBuilder Command = new StringBuilder(string.Empty);

public static void Main() {
    ConsoleKeyInfo k;
    do {
        k = Console.ReadKey(true);
    } while (HandleInput(k));
}

private static bool HandleInput(ConsoleKeyInfo k) {
    if (k.Key == ConsoleKey.Backspace && Command.Length > 0)
        Command.Remove(Command.Length - 1, 1);
    else if (k.Key == ConsoleKey.Enter)
        HandleCommand();
    //else if some function keys, etc...
    else
        Command.Append(k.KeyChar);
    return true;
}

private static void HandleCommand() {
    //do something with _command, then clear it
    var command = Command.ToString();
    Command.Clear();
    Console.Clear();
}


Was This Post Helpful? 0
  • +
  • -

#11 darkflaw  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 21-August 08

Re: Hook Into Console Text Input

Posted 10 January 2012 - 05:48 PM

Thanks for the examples, looks like reading each char will probably be the way to go until I discover something easier/better, sorry again for all the confusion and I didn't really mean to demand any code if it seemed like I was.
Was This Post Helpful? 0
  • +
  • -

#12 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4316
  • View blog
  • Posts: 7,485
  • Joined: 08-June 10

Re: Hook Into Console Text Input

Posted 10 January 2012 - 06:16 PM

I'm reading by char as an example to prevent the text from being echoed to the console. If you don't mind that, you could modify it to work with ReadLines instead of ReadKeys.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1