Hello... I'm trying to get a combination of keys pressed or buttons held down.
The right thing is that I want to make a Ctrl+Z function in my proyect, so if I press the key Control, hold down it and then press the Z key, I want that my last action to be eliminated.
I have something like this, but it doesn't work at all...
Event e = Event.current;
if (e.control)
{
print("control!");
if (Input.GetKeyDown("Z"))
{
print("Deshacer!");
}
if (Input.GetKeyDown("Y"))
{
print("Rehacer!");
}
}
You could be a bit more precisely. Where do you use this code? In Update or in OnGUI? What get printed? nothing? As i said in my answer, Event is only for GUI events and is only available in OnGUI
The Event class should only be used in OnGUI() and the Input class should not be used in OnGUI.
For combination like CTRL-Z you can use:
void OnGUI()
{
Event e = Event.current;
if (e.type == EventType.KeyDown && e.control && e.keyCode == KeyCode.Z)
{
// CTRL + Z
}
}
or what should also work:
void Update()
{
if ((Input.GetKey(KeyCode.RightControl) || Input.GetKey(KeyCode.LeftControl)) && Input.GetKeyDown(KeyCode.Z))
{
// CTRL + Z
}
}
@Alejandro: Be warned that while testing in the editor, Ctrl Z may not appear to function properly; in my tests Unity actually thought I was trying to undo something and it never registered in the game. However, when I built the game, the same script worked fine.
@paulius-liekis: Not really. It is a built-in combination of the editor itself, like CTRL+ALT+DEL is built-in the OS. You either use a different combination or use it only in your final build.
I beleive something like this would work as a work around/Make it so you don't have to remember to change it on final build: #if UNITY_EDITOR if(Input.GetKey(KeyCode.Z)) #else if((Input.GetKey(KeyCode.RightControl) || Input.GetKey(KeyCode.LeftControl)) && Input.GetKeyDown(KeyCode.Z)) #endif { UndoLastAction(); }
Event e = Event.current;
if (e.type == EventType.KeyUp && e.control && e.keyCode == KeyCode.S)
{
Debug.Log("SAVE");
}
I don't get this answer. You use a different key combination and use the keyup event. What's the reason for that? Note that CTRL + Z obviously doesn't work properly inside the editor but does work inside a build (as discussed above). Without giving a proper reason for your suggested change this answer seems pretty pointless.
You could be a bit more precisely. Where do you use this code? In Update or in OnGUI? What get printed? nothing? As i said in my answer, Event is only for GUI events and is only available in OnGUI
– Bunny83