having trouble with "ctrl + number"

ive been having a problem with ctrl and a number not running the lines of code inside the braces.

it wont work inside the editor or after a build to an EXE.

i found someone having the same trouble and tried there solution but it didn’t seem to work for me.

i have also tried plugging in a USB keyboard to see if it’s my laptop thats causing the trouble. still same problem.

if (Input.GetKey(KeyCode.LeftControl) && Input.GetKeyDown(KeyCode.Alpha1))
	{
	print("Doesn't print this");
	}

or

if (Input.GetKeyDown("1"))
{
	ControlGroup(1);
}


void ControlGroup (int number)
	{
		if ((Input.GetKey (KeyCode.LeftControl)) || (Input.GetKey (KeyCode.RightControl))) 
		{
			switch(number)
			{
				case 1:
				print ("Doesn't print this either");
				break;
			}

any ideas what i’m missing?

many thanks in advance

Try this; if (Input.GetKey(KeyCode.LeftControl) | Input.GetKeyDown(KeyCode.Alpha1))

This is most strange, you are absolutely correct. I just did a test in uJS, and it had the exact problem you are describing. I even tried checking for a string instead of a keycode enum : function Update() { if ( Input.GetKey( KeyCode.LeftControl ) ) { Debug.Log( "LeftCtrl Detected" ); if ( Input.GetKeyDown( KeyCode.Alpha1 ) ) { Debug.Log( "LeftCtrl and Alpha1 Detected" ); } if ( Input.GetKeyDown( "1" ) ) { Debug.Log( "LeftCtrl and String 1 Detected" ); } } } Upvoting question.

No i haven't tested actually.I just written that the same way i do for Windows applications on C# so i thought this might help :) By the way i have no idea why you are telling that :)My point was using | instead of ||(Cause in c# apps(Wİndows form actually) this usage works for access modifier keys.(Ctrl + X + X...) > There is no problem using extra parenthesis, as long as they encapsulate the appropriate items.

Well as i said i didn't mentioned "(" so the change was simply "||" that to "|" according to the general c# usage. I wouldn't change parenthesis and send this as an answer :)

Look, there is no problem here, I just wanted to clarify that using | will throw a warning in Unity. And it didn't fix the problem.

1 Answer

1

Try this:

void OnGUI()
{
	Event e = Event.current;
	if (e.isKey && e.control && e.keyCode == KeyCode.F)
	{
		Debug.Log("ctrl + F");
	}
}

Unofrtunately this has to be done in OnGUI. Take note Ctrl + 1 seems to be a hotkey that Unity already uses so it won’t get detected when running in the Unity Editor. I’ll try to see if there’s some other way to detect Control + some key in Update.