- Home /
The question is answered, right answer was accepted
How to check if control is pressed in a text field?
I need to be able to see if control is pressed while entering in keys into a string in a textfield. I'd like to do something like this, maybe in hexadecimal notation
if (c == 0xA2)
An extensive hour and half of googling yielded no results. Do you know how to do this? Thanks!
I'm fairly certain it's not possible as there are no callbacks available for the built in GUI elements, the most any of them give you is the return value which I assume won't include any of the control key presses. In fact the GUI elements in Unity operate quite strangely in my opinion... I know they are planning an overhaul for Unity 4.3 based in part on the NGUI package (the guy that wrote NGUI went to work for Unity to fix their GUI!). Hopefully we'll see some more sensible design with more flexible control...
I hope so! I can't believe you can't do just check like that. Well thank you, I am going to use the timer idea. Its should work perfectly for now. Thank you
Answer by Jamora · Nov 05, 2013 at 01:44 PM
This is how I'd basically check if ctrl was pressed while a text field was focused. It'll register every frame ctrl is pressed, so if the intention is to react once to pressing ctrl (i.e. Input.GetKeyDown type of behavior) then additional checks need to be added.
GUI.SetNextControlName("CtrlField");
myString = GUILayout.TextField(myString);
if(Event.current.control && GUI.GetNameOfFocusedControl().Equals("CtrlField"))
Debug.Log("ctrl pressed while textfield focused");
Is there a way to check if its left or right control?
Yes. Change your if-clause:
if(Event.current.keyCode == $$anonymous$$eyCode.LeftControl && GUI.GetNameOfFocusedControl().Equals("CtrlField"))
That's perfect! Thank you! Is there a detailed documentation on that subject? Some times the official documentation is very lacking.
There's only the official documentation that I know of.
I see you've asked a lot of questions, but haven't marked any as answered. You should press the tick mark below the thumbs to mark the most correct/helpful answer as the solution. Gives the answerer a warm and fuzzy feeling and a few points.
Answer by Zaeran · Nov 05, 2013 at 05:31 AM
if(Input.GetKey(KeyCode.LeftControl)){
//stuff to do when control is pressed
}
Right, but how can they check that this is happening whist the user is editing the text field? I'm curious about this myself now.
One (poor) manual hack way could be to set a timer each time the text input changes, and if recently changes assume the text field is in focus.
you make a good point. I suppose you just keep a temp string of the textbox input, and put in a if(tempString != Textbox.text){//if ctrl pressed}.
It may also be worth looking at NGUI in the Asset Store... there's a free version to start with. That might provide the level of control needed to do this (if it indicates which field has focus or provides proper callbacks on key presses etc).
Hm. I've had literally no complaints before with Unity. I need to write my own mock control key stuff because I want it changed in the main menu. I do like the idea of the timer however. This will be rough, but that will do fine. Thank you everyone!
Follow this Question
Related Questions
How do i take a string from a gui and show it in the gameworld c# 1 Answer
Taking data from text file 2 Answers
Rich text character-by-character shows tags. How to hide them? 2 Answers
For loop on text without effecting each other 1 Answer
particle system position on top of string position 0 Answers