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
0
Question by shananas · Sep 22, 2017 at 02:34 AM · scripting problemkeycode

Detecting Pressed key and returning a KeyCode Variable

There are lots of answers on detecting which key was pressed but I need to get that as a KeyCode variable to reassign my controls and it is always returned as a string. I have a variable of KeyCode that i want to assign the next pressed key on the keyboard to move the character the direction. For example i have a variable KeyCode Foward1 = KeyCode.W; and when i press a button i have a public method called On Click() and i want it to detect the next key pressed and assign it to Foward1 but have no idea how to do this . Can I do it this way or do i need to use OnGUI()

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
2

Answer by Llourn · Sep 22, 2017 at 03:00 AM

Hi @Shananas . I did some looking around and found this. I hope it helps. :)

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using System;
 
 public void DetectPressedKeyOrButton()
  {
      foreach(KeyCode kcode in Enum.GetValues(typeof(KeyCode)))
      {
          if (Input.GetKeyDown(kcode))
              Debug.Log("KeyCode down: " + kcode);
 
         
      }
  }
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 shananas · Sep 22, 2017 at 03:04 AM 0
Share

I think that would work if i replaced debug log with foward1 = kcode ill see if it works

avatar image
-1

Answer by xvivax · Sep 22, 2017 at 02:55 AM

Is it what are you looking for?

     KeyCode tempKeyCode;
     KeyCode Forward1;
 
     void OnGUI()
     {
         Event e = Event.current;
         if (e.isKey)
         { 
             tempKeyCode = e.keyCode;
         }
 
     }
 
     public void OnClick()
     {
         Forward1 = tempKeyCode;
     }

Comment
Add comment · Show 4 · 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 shananas · Sep 22, 2017 at 03:03 AM 0
Share

That would only work if i created an if for every key so it seems inefficient but i might do that if i need to since it will do the job

avatar image xvivax shananas · Sep 22, 2017 at 03:13 AM 0
Share

It automatically detects any letter you press and pass it to temp$$anonymous$$eyCode. Don't be confused by e.$$anonymous$$eyCode it doesn't mean "e" letter, it is just an Event variable name.

avatar image shananas xvivax · Sep 22, 2017 at 03:32 AM 0
Share

Forgot that e was the event my bad. That unbinds the key the problem Im having now is my script is on the player and Im trying to use the buttons to change the script by assigning the newly hit key through the On Click function provided in unity once yo create a button so i wonder what is the best way to do this can i do it this way and change the script on my player or should i do another way

Show more comments

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

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

Related Questions

Multiple button name in a row? 1 Answer

Problem coding script for paddles in Pong without using any physics. 1 Answer

Check if pressed key exists inside an array of KeyCodes 2 Answers

How can make the player move constantly when the key A or D is held down? 1 Answer

animation does not work when one clone got destroy unity 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