• 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
Question by Bonobo · Aug 14, 2013 at 04:41 PM · eventkeyboardeventskeyboardevent

How can I detect Keyboard Events?

I recently copy/pasted some TileManager from here: http://wiki.unity3d.com/index.php?title=TileManager

However, it doesn't work. I started outputting Debug Logs to find out why it wasn't working and I discovered that keyboard events are never detected.

How can I get Unity to detect keyboard events?

Here are a few log statements that I used:

     //get the current event
     Event e = Event.current;
     
     /////////////////////////////////////////////////////////////////////////////////
     // The following if statements work correctly
     if(e.shift) 
     {
         Debug.Log("Shift");
     }
     
     if(e.type.Equals(EventType.MouseMove)) 
     {
         Debug.Log("Mouse Move");
     }
     /////////////////////////////////////////////////////////////////////////////////
     /////////////////////////////////////////////////////////////////////////////////
     // The following if statements do not execute even when they should
     if(e.isKey) // Does not work
     {
         Debug.Log("Key");
     }
     
     if (e.Equals (Event.KeyboardEvent ("[enter]"))) // Does not work
     {
         Debug.Log("Enter");
     }
     
     if(e.type.Equals(EventType.KeyDown)) // Does not work
     {
         Debug.Log("Key Down");
     }
     ///////////////////////////////////////////////////////////////////////////////// 
Comment

People who like this

0 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 zombience · Aug 14, 2013 at 04:50 PM 0
Share

do you need to use keyboard events? why not just use Input.GetKeyDown(Keycode.A) ?

4 Replies

· Add your reply
  • Sort: 
avatar image

Answer by zombience · Aug 15, 2013 at 09:09 PM

I just tried using keyboard events in a custom editor.

this worked for me:

 [CustomEditor(typeof(Manipulator))]
 public class ManipulatorEditor : Editor
 {
     public override void OnInspectorGUI()
     {
 
         if (Event.current.keyCode == KeyCode.S)
         {
             Debug.Log("hit s");
         }
     }
 }

note that this only works when you have the referenced class on an object (Manipulator.cs in this case), and have the inspector window of that object selected. the rest of the time, the keypresses will not be registered.

Comment
chirhotec

People who like this

1 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 chirhotec · Sep 17, 2013 at 06:36 PM 0
Share

I just spent 2 or 3 hours trying to figure this out. The key part of this solution is that "have the inspector window of that object selected. the rest of the time, the keypresses will not be registered". ie, I was trying to press the keys in the hierarchy. So frustrating!

Is there a way to do something similar in the hierarchy?

avatar image

Answer by Ali-Nagori · May 30, 2014 at 03:59 PM

if you want these Event in your case place your code inside OnGUI

void OnGUI() { ........... ...........

Comment
Chom1czek

People who like this

1 Show 0 · 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

Answer by highpockets · Aug 14, 2013 at 04:52 PM

Input.GetKeyDown("space"); for example, if you want the space bar to do something

Comment

People who like this

0 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 highpockets · Aug 14, 2013 at 04:55 PM 0
Share

Find more input functions here: http://docs.unity3d.com/Documentation/ScriptReference/Input.html

avatar image

Answer by Bonobo · Aug 15, 2013 at 03:13 PM

I have tried using Input instead of Events.

The problem with this solution is that this script is for an Editor extension that runs even when the game isn't playing.

Input only seems to work when I actually run the game itself.

Comment

People who like this

0 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 zombience · Aug 15, 2013 at 09:07 PM 0
Share

you've added a new "answer" to your own question. please add comments instead.

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

21 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Argument 1: cannot convert from 'method group' to 'UnityAction' 1 Answer

How to change target of UnityEvent 3 Answers

Any way to pass the function of a listener as a string? 1 Answer

Animation Event not working properly 0 Answers

Android Keyboard height not working 0 Answers


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