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
1
Question by Karsten · May 14, 2012 at 09:15 PM · inputeventinputmanagerkeypress

Why is this script showing all keycodes except shift?

as the header says...here the script, it shows all except shift keys, why?!

 using UnityEngine;
 using System.Collections;
 
 public class RainInput : MonoBehaviour
 {
     public string event1;
     
     void Update()
     {
         if (event1.Length > 0)
         {
             Debug.Log(event1);
         }
     
         event1 = "";
     }
     
     void OnGUI()
     {
         Event e = Event.current;
         {
             string tmpstr = "" + e.keyCode;
             if (tmpstr == "None")
             {
                 return;
             }
             event1 = tmpstr;
         }
     }
 }
Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by Bunny83 · May 14, 2012 at 09:32 PM

Because the Event class is ment for GUI key input quite similar to the windows char-messages. Shift, Ctrl and Alt are modifier keys so usually you don't execute an action when pressing those keys. They change the behaviour of other keys.

To sum up:

  • Event class is used for GUI input

  • Input class is used to process any game input(including shift, alt, ...).

  • Event class can only be used in OnGUI

  • Input class should only be used in Update or LateUpdate

edit
When using the event class you should always check the event type. Here you can see all possible events. Unity processes a lot events with OnGUI. It is called for each event. Usually 2 times per frame (Layout and Repaint event), but also for any kind of key or mouse event (the mouse move event only exists in the editor afaik).

 void OnGUI()
 {
     Event e = Event.current;
     if (e.type == EventType.KeyDown)
     {
         if (e.keyCode == KeyCode.A)
             // ...
     }
 }
 
 void Update()
 {
     if (Input.GetKeyDown(KeyCode.LeftShift ))
     {
         Debug.Log("LeftShift has been pressed");
     }
 }

Here's the list of all key codes

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 Karsten · May 14, 2012 at 09:58 PM 0
Share

thanks i use now Input.Get$$anonymous$$eyDown i was just wondering, it really shows even left control and rightcontrol but not shift if you use Event... anyway best solution is Input.Get$$anonymous$$eyDown atm i think.

avatar image Bunny83 · May 14, 2012 at 11:09 PM 1
Share

But keep in $$anonymous$$d that you shouldn't use the Input class in OnGUI, since OnGUI is called to process events and it can get called multiple times per frame.

avatar image
1

Answer by Bouldeterre · Feb 06, 2013 at 03:20 AM

Look at http://docs.unity3d.com/Documentation/ScriptReference/Event-shift.html http://docs.unity3d.com/Documentation/ScriptReference/Event-control.html http://docs.unity3d.com/Documentation/ScriptReference/Event-alt.html

Shift, Ctrl and Alt can be checked as event with this.

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

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

6 People are following this question.

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

Related Questions

Can an Input event be 'faked' with a script? 1 Answer

replace Input.GetKey for mobile input 1 Answer

Will Unity 3.0 have a scriptable Input Manager 1 Answer

Get list of Axes? 4 Answers

PlayerInput - SendMessages not working after changing the scene 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