• 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
Question by IPTN · Jun 16, 2011 at 03:07 PM · editorinputeditor-scriptingkeyboardevent

How to check if a key is down in editor script

I am writing an editor script and I want to be able to check if a key is or has been pressed regardless of what has focus. I realize that Input won't work. So far I haven't been able to find anyt$$anonymous$$ng in the documentation or on Google that would allow me to do t$$anonymous$$s. I was wondering how to do t$$anonymous$$s or if it is even possible.

Comment
FranArjona
piersb
abj87

People who like this

3 Show 2
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 zachypin · Jun 16, 2011 at 04:04 PM 0
Share

Why wouldn't input work?

avatar image IPTN · Jun 16, 2011 at 04:06 PM 0
Share

Just doesn't work in Editor Scripts

2 Replies

· Add your reply
  • Sort: 
avatar image

Answer by gabhead · Jul 28, 2011 at 03:11 PM

 [CustomEditor(typeof(yourClassTarget))]
 public class yourEditorClass: Editor
 {
     void OnSceneGUI()
     {
         VertexPaint script = (VertexPaint)target;
         Event e = Event.current;
         switch (e.type)
         {
             case EventType.keyDown:
             {
                 if (Event.current.keyCode == (KeyCode.A))
                 {
                     script.Painting = true;
                 }
                 break;
             }
         }
     }
 }
Comment
thienhaflash
Bunny83
Kody
Alexphauge
NinjaISV
FranArjona
Sarseth
PhoenixRising1
Ali_V_Quest
LivioDeLaCruz
Nehrk
eDmitriy
Phoenix116
FinnTess
Ash-Blue
And 6 more...

People who like this

21 Show 2 · 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 thienhaflash · Sep 03, 2012 at 03:58 PM 3
Share

This code should work, vote up everyone ! I used Event.current.type to check, too and it's working. But please think it twice before using the code like this, as this kind of code will get the input even user is typing the name of the GameObjects or rename / input some value and that may not what you want. Narrow down a bit by checking EditorWindow.focusedWindow should be good.

avatar image Bunny83 · Sep 03, 2012 at 06:45 PM 0
Share

@thienhaflash: Yes, absolutely ;) A lot people don't think about the fact that the editor is ment for many different tasks. You could also, like the TerrainScript that comes with Unity, display your own "toolbar" and only check the key when your "editor" is active / in the right mode.

avatar image

Answer by tribaleur · Apr 08, 2020 at 06:59 PM

I know that t$$anonymous$$s post is archeology, but to complete the exemple code of the answer you have to add the instruction e.Use(); after your code.

      [CustomEditor(typeof(yourClassTarget))]
      public class yourEditorClass: Editor
      {
          void OnSceneGUI()
          {
              VertexPaint script = (VertexPaint)target;
              Event e = Event.current;
              switch (e.type)
              {
                  case EventType.keyDown:
                  {
                      if (Event.current.keyCode == (KeyCode.A))
                      {
                          script.Painting = true;
                          // EDIT  : t$$anonymous$$s a the instruction to add. 
                          // You realy need it to avoid performance issues !
                          e.Use();
                          // END EDIT
                      }
                      break;
                  }
              }
          }
      }
 
 



Comment
abj87

People who like this

1 Show 3 · 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 · Apr 09, 2020 at 01:00 AM 0
Share

No, not really. You don't generally need it and it doesn't really have much to do with performance. You should call Use when you specifically want to prevent any further processing by Unity or other scripts of that event. You really have to be careful with Use because you currently do not check any modifier keys. So your code will react whenever A is pressed in the sceneview. No matter is it's SHIFT+A, CTRL+A, ALT+A or just A

avatar image homer_3 · Sep 28, 2020 at 06:04 PM 0
Share

How do you actually get this code to run? I've put it in the Editor folder, but it never runs. Also, VertexPaint is unresolved.

avatar image Eldoir homer_3 · Feb 21, 2021 at 10:33 AM 0
Share

HI @homer_3 , You can see on the first line that this is a CustomEditor for yourClassTarget. You should replace yourClassTarget with the name of one of your own scripts, and by doing that, this Editor class will override the default display for your script: you will have to display the fields of your script yourself. More info on CustomEditors on the link below. Also, for your VertexPaint issue, this is because VertexPaint is a custom script that the owner has wrote, so lines 6 and 14 in this sample code are irrelevant for the resolution of this question. The owner should have removed them to avoid confusion. Hope this helps :)

[1] https://docs.unity3d.com/Manual/editor-CustomEditors.html

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

10 People are following this question.

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

Related Questions

Prevent unity hotkeys in editor 0 Answers

Any way to invisibly tag an object? 1 Answer

Updating object on inspector value changes in editor 1 Answer

EditorApplication.update resetting 3 Answers

problem with the digits of my timer 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