Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 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 anything in the documentation or on Google that would allow me to do this. I was wondering how to do this or if it is even possible.

Comment
Add comment · 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
21

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
Add comment · 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
1

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

I know that this 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  : this a the instruction to add. 
                          // You realy need it to avoid performance issues !
                          e.Use();
                          // END EDIT
                      }
                      break;
                  }
              }
          }
      }
 
 



Comment
Add comment · 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.

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

SetReplacementShader in Editor Utility? 0 Answers

mouseposition and clicks in editor sceneview 0 Answers

breaking out of OnGUI() ticks? 1 Answer

Unity Editor Scripts: GameObject Added / Removed Event? 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