- Home /
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.Get$$anonymous$$ey($$anonymous$$eyCode.LeftControl) | Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.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 ins$$anonymous$$d of a keycode enum :
function Update()
{
if ( Input.Get$$anonymous$$ey( $$anonymous$$eyCode.LeftControl ) )
{
Debug.Log( "LeftCtrl Detected" );
if ( Input.Get$$anonymous$$eyDown( $$anonymous$$eyCode.Alpha1 ) )
{
Debug.Log( "LeftCtrl and Alpha1 Detected" );
}
if ( Input.Get$$anonymous$$eyDown( "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 :)$$anonymous$$y point was using | ins$$anonymous$$d 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 :)
$$anonymous$$y next method to test would then be to use the Input $$anonymous$$anager, map the Ctrl key to a button, then use Input.GetButton.
Here are the results of a search I just did for you to look through (untested, just searched) :
http://answers.unity3d.com/questions/214611/command-for-holding-a-key-down-shift-key.html
http://answers.unity3d.com/questions/49285/how-can-i-get-a-combination-of-keys-pressed.html
http://answers.unity3d.com/questions/264010/ctrlkey-combination-not-working-properly.html
http://answers.unity3d.com/questions/9223/how-can-i-check-to-see-if-a-button-combination-is.html http://forum.unity3d.com/threads/138386-How-can-i-check-if-multiple-keys-is-pressed-down-at-once
If my Input $$anonymous$$anager suggestion, or something in one of those works, please let us know.
Answer by AnomalusUndrdog · Apr 08, 2013 at 09:04 AM
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.
Thanks for the help, but i couldn't get that to work. not sure what was up with it but it was not happy.
EDIT
ok, read the undertext again, and yes, it worked with the f (not the 1 though)
i did try and run the core bits in Update but it error'd...
TeamShipAndCommands.Update () (at Assets/_Scripts/TeamShipAndCommands.cs:45) line 45 being the if but i can still have a play. having a look at changing Event.current to Event.$$anonymous$$eyboardEvent. many thanks :D EDIT (again) wont run in update as it's a GUI thing. works though. thankyou :) http://docs.unity3d.com/Documentation/ScriptReference/Event.$$anonymous$$eyboardEvent.htmlNullReferenceException: Object reference not set to an instance of an object