Hello Guest

Author Topic: Removing characters from Input.KeyString  (Read 3111 times)

0 Members and 1 Guest are viewing this topic.

Fatesauce

  • Member
  • Posts: 26
    • View Profile
Removing characters from Input.KeyString
« on: July 21, 2015, 04:08:46 AM »
I have created a textbox using Kyles example in another post as a base.  I want to prevent the "Return" key (and possibly others) from being pushed to the text graphic, effectively restricting it to 1 line.  At the moment you can hit enter indefinitely until the end of the string is somewhere miles off the screen.  I've found a bunch of ways to do this in windows forms and console, how would i do it here?

Thanks heaps.

arcanebro

  • Member
  • Posts: 3
    • View Profile
Re: Removing characters from Input.KeyString
« Reply #1 on: July 21, 2015, 08:10:45 AM »
I don't know what code you are talking about, but character for 'return' key is \n
Code: [Select]
void KeyPressed(char c) {
    if (c == '\n')
        return;
    else {
        textBox.Text = textBox.Text + c;
    }
}

Fatesauce

  • Member
  • Posts: 26
    • View Profile
Re: Removing characters from Input.KeyString
« Reply #2 on: July 21, 2015, 03:17:55 PM »
Ok ive found a solution, though it does seem somewhat untidy. It works well though.

Code: [Select]
if (TextInput.Length > 0)
                {
                    if (TextInput.ElementAt(TextInput.Length -1) == '\n')
                    {
                        TextInput = TextInput.Substring(0, TextInput.Length - 1);
                        Input.KeyString = TextInput;
                    }

                    if (!Input.KeyPressed(Key.Return) || !Input.KeyDown(Key.Return))
                    {
                        Input.KeyString = TextInput;
                    }
                }

The problem with lust having the block of code that removed '\n', is that i noticed the Input.KeyString updated faster than the update loop, so if i hit enter alot or held enter down, the Input.KeyString would update faster than the code could check it,  the extra block at the bottom fixes this though.