Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
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
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 zombience · Aug 14, 2013 at 04:50 PM 0
Share

do you need to use keyboard events? why not just use Input.Get$$anonymous$$eyDown($$anonymous$$eycode.A) ?

4 Replies

· Add your reply
  • Sort: 
avatar image
1

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
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 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
1

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

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

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 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
0

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

you've added a new "answer" to your own question. please add comments ins$$anonymous$$d.

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

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

Event Trigger For Key Pressed 4 Answers

EventSystem lag on first touch 0 Answers

checking for keyboard input with exceptions 0 Answers

Mac Keys not showing up, or very strangely... 2 Answers

When to use 'delegate', 'event' or 'Action' ? 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