Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by Alejandro Mora · Feb 24, 2011 at 05:31 PM · combinegetkeydownkeys

How can I get a combination of keys pressed?

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!"); } }

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Feb 24, 2011 at 05:53 PM 0
Share

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

3 Replies

· Add your reply
  • Sort: 
avatar image
17

Answer by Bunny83 · Feb 24, 2011 at 05:47 PM

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
    }
}
Comment
Add comment · Show 9 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Alec-Slayden · Feb 24, 2011 at 05:52 PM 1
Share

@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.

avatar image Bunny83 · Feb 24, 2011 at 05:57 PM 1
Share

Sure, that's one of those combinations that won't work in the editor properly. ;)

avatar image Alejandro Mora · Feb 24, 2011 at 06:21 PM 0
Share

Ohhh!! :O yeah, I get that! Thanks a lot! =)

avatar image Bunny83 · Oct 24, 2012 at 08:26 PM 1
Share

@paulius-liekis: Not really. It is a built-in combination of the editor itself, like CTRL+ALT+$$anonymous$$ is built-in the OS. You either use a different combination or use it only in your final build.

avatar image DoctorWhy · Oct 24, 2012 at 10:10 PM 1
Share

I beleive something like this would work as a work around/$$anonymous$$ake it so you don't have to remember to change it on final build:

 #if UNITY_EDITOR
     if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.Z))
 #else
     if((Input.Get$$anonymous$$ey($$anonymous$$eyCode.RightControl) || Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftControl)) && Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Z))
 #endif    
     {
         UndoLastAction();
     }
Show more comments
avatar image
0

Answer by martin-3dar · Dec 11, 2016 at 10:53 PM

this might be useful for anyone running this question:

https://docs.unity3d.com/ScriptReference/Input-inputString.html

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by vladipetrenko · Nov 18, 2018 at 02:11 PM

On Unity 2018.2.7f1 for Linux, please use this:

             Event e = Event.current;
             if (e.type == EventType.KeyUp && e.control && e.keyCode == KeyCode.S)
             {
                     Debug.Log("SAVE");
             }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Nov 19, 2018 at 01:36 AM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

how do i make first person character rotate left and right along with camera? 0 Answers

unity dictionary with array as keys causes problems 0 Answers

merge vertices at runtime? 1 Answer

Combining textures into one image. 1 Answer

Combine Animation 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges