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
2
Question by Ochreous · Nov 25, 2015 at 02:31 AM · triggereventkeyboardpressed

Event Trigger For Key Pressed

Is there anyway to get an event trigger to handle when a Key's pressed? I looked through what you could assign the event trigger and I wasn't able to find anything like that.

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

4 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by ExamplesAreExamples · Nov 25, 2015 at 02:22 PM

Unity does not send Key events using the new Event System.

For reference, here's a list of all supported events.

However, you can still process input events if you are the selected object. They are just not sent through the Event System.

See the implementation of InputField.OnUpdateSelected, for reference. This is how Unitys UI uses keyboard input. You'll notice it's very similar to how the old IMGUI got key events from Event.current. Instead, here they use Event.PopEvent in a while loop instead of calling OnGUI several times per frame.

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
2

Answer by btft · Nov 25, 2015 at 03:16 PM

I'm afraid you need to create simple script and catch the key on Update:

 public void Update()
 {
      if (Input.GetKeyUp(KeyCode.X))
     {
        //do stuff
     }
 }






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 unity_wIfM9PlhP0UL9A · Mar 11, 2020 at 07:17 PM 0
Share

Does this make it so when you press x it does stuff

avatar image
0

Answer by ___... · Nov 25, 2015 at 12:02 PM

Input has to be checked every frame in Update. There are different functions available. Some just return the state of a button (down or up) some will return true only the frame a button has been pressed down or up. Buttons have to be defined in the input manager.

There are also functions for reading certain keyboard-keys

Input.GetKey

Input.GetKeyDown

Input.GetKeyUp

In general just look into the scripting reference

A script that toggles a light could look like this:

  // UnityScript (Unity's Javascript)
  function Update()
  {
      if (Input.GetMouseButtonDown(0)) // 0 - left button; 1 - right button; 2 - middle button
      {
          light.enabled = !light.enabled;
      }
  }

Make sure you attach this script to a GameObject that has a Light component attached ;)

The same script in C#:

  // C#
  // FlashLightControl.cs
  using UnityEngine;
  
  public class FlashLightControl : MonoBehaviour
  [
      void Update()
      {
          if (Input.GetMouseButtonDown(0)) 
          {
              light.enabled = !light.enabled;
          }
      }
  }
 
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 BenBB · Nov 25, 2015 at 12:02 PM

You can write a script that manages the keys like this (js) :

 var key : KeyCode;
 
 function Update () {
     
     if(Input.GetKey(key)) {
                  //here you put the code of your event
     }
  }

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

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

TextArea does not fire KeyDown events 1 Answer

New Input System: Event on input type changed (between mouse, keyboard and controller) 1 Answer

Create custom Triggerevent 0 Answers

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

Trigger Event, Ring Pass thru Rod Object 3 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