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 Beta4 · Mar 01, 2019 at 04:06 PM · disablekeys

How do I disable a key on the keyboard when it enters its fuction?

I have a simple question. How would i disable a button on a keyboard when it enters the function

  DisableKey()
  {
        // What is the correct code to disable the "X" key on the keyboard?
  }

Like for example once it hits game over i want to have the player not to be able to fire with the x button and i want to do it by simply disabling the X keystroke. Im sure there are other ways of not letting the player fire but I would like to learn how to do it by simply make sure if the player hits the X it doesnt work anymore. Im trying to learn new things :) Thanks again.

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 Carterryan1990 · Mar 01, 2019 at 04:15 PM 0
Share

Can you give more details of what exactly you are trying to do? I might be able to help.

1 Reply

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

Answer by Hellium · Mar 01, 2019 at 05:04 PM

You can't "disable a key on the keyboard". What you have to do is to decide whether you run code when the key is pressed or not.


I would have done this using a Dictionary and custom functions:

 // At the top of your script
 using System.Collections.Generic;
 
 // In your class
 private Dictionary<KeyCode, bool> keys = new Dictionary<KeyCode, bool>();
 
 private bool GetKeyDown( Keycode keyCode )
 {
     if( !keys.ContainsKey( keyCode )
         keys.Add( keyCode, true );
     return Input.GetKeyDown( keyCode ) && keys[keyCode];
 }
 
 // Do the same for GetKey and GetKeyUp
 
 private void DisableKey( KeyCode keyCode )
 {
     if( !keys.ContainsKey( keyCode )
         keys.Add( keyCode, false );
     else
         keys[keyCode] = false;
 }
 
 private void EnableKey( KeyCode keyCode )
 {
     if( !keys.ContainsKey( keyCode )
         keys.Add( keyCode, true );
     else
         keys[keycode] = true;
 }


Then, instead of calling Input.GetKeyXXX( ... ), call GetKey( ... )


You could also have these functions (and dictionary) in a static class in order to have a global manager like Unity's Input class.

Comment
Add comment · Show 5 · 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 Beta4 · Mar 01, 2019 at 05:38 PM 0
Share

Well thank you so much at least have learned that I cannot disable a key on the keyboard. Thank you so much for your input. I definitely learned something new today :)

avatar image TreyH Beta4 · Mar 01, 2019 at 07:36 PM 0
Share

Also, if this has answered your question, go ahead and mark it as the solution so that the thread can be closed. :-)

avatar image TreyH · Mar 01, 2019 at 07:32 PM 0
Share

A few disparities with keycode, keyCode, and key code for folks planning to copy / paste, btw.

avatar image Bunny83 TreyH · Mar 01, 2019 at 07:42 PM 2
Share

I've fixed those cases ^^.

Note that keys[keycode] = true; does also work if the key doesn't exist yet in the dictionary. So the Enable / Disable method could look just like this:

 private void Disable$$anonymous$$ey( $$anonymous$$eyCode keyCode )
 {
     keys[keyCode] = false;
 }
  
 private void Enable$$anonymous$$ey( $$anonymous$$eyCode keyCode )
 {
     keys[keycode] = true;
 }

avatar image Hellium TreyH · Mar 01, 2019 at 08:06 PM 0
Share

$$anonymous$$y bad, I wrote it from my phone and I've surely missed some mistakes made by the auto correct.

@Bunny83, I could not remember if the assignment could be done directly this way. Thanks for the re$$anonymous$$der!

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

103 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

Related Questions

If my character's raycast collides with an object in front of it (such as a wall), how can I temporarily disable any movement going forward? 1 Answer

If your pressing one key, how to disable another? 3 Answers

setting input buttons 1 Answer

Want to rotate camera horizontally around object using left and right arrow keys 0 Answers

Keys conflicts when shooting or grabbing objects 2 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