Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 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 /
avatar image
1
Question by WesterlyCarrot9 · Mar 08, 2013 at 06:08 AM · javascriptguibuttonkeyassign

Assign a hotkey to a GUI button?

Hello!I've been trying to figure out how can i assign a hotkey to my GUI buttons. I've managed to make the button create a Box when pressed by the mouse and then close it and also managed somehow to make it open with the I key but i cannot close it with it. Also i cannot open with mouse and then close with I. Here is the script i use:

 var charPressed = false;
 var pushkey = false;
 function OnGUI() {
     if( GUI.Button (new Rect (Screen.width - 118,Screen.height - 120,110,30 ), "Inventory" ) ) {
         charPressed = !charPressed;
     }
     if( charPressed ) {
         GUI.Box( Rect( 350, 120, 300, 110 ), "INVENTORY" );
     }
     if(Input.GetKeyUp(KeyCode.I)) {
         pushkey = true;
     }
     if(pushkey == true) {
         GUI.Box( Rect( 350, 120, 300, 110 ), "INVENTORY" );
     }
 }
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

3 Replies

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

Answer by kramcomposer · Mar 08, 2013 at 06:14 AM

just change:

 if(Input.GetKeyUp(KeyCode.I)) {
        pushkey = true;
     }

TO:

 if(Input.GetKeyUp(KeyCode.I)) {
        charPressed = !charPressed;
     }

Also, you may need to move your Input code to Update()

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 WesterlyCarrot9 · Mar 08, 2013 at 06:21 AM 0
Share

Thank you so much!!

avatar image robertbu · Mar 08, 2013 at 06:27 AM 0
Share

As @kramcoposer suggested you do need to move your key detection code to Update(). That's because there can be multiple OnGUI() calls per frame. You can delete some of your code as well:

 var charPressed = false;
 
 function Update() {
    if(Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.I)) {
        charPressed = !charPressed;
     }
 }
 function OnGUI() {
     if( GUI.Button (new Rect (Screen.width - 118,Screen.height - 120,110,30 ), "Inventory" ) ) {
        charPressed = !charPressed;
     }
     
     if(charPressed) {
        GUI.Box( Rect( 350, 120, 300, 110 ), "INVENTORY" );
     }
 }
avatar image Chronos-L · Mar 08, 2013 at 06:30 AM 0
Share

@westerlycarrot9, @kramcomposer, Input.Get$$anonymous$$eyUp() do work in OnGUI(), but it is not working perfectly.

Update

 var up = 0;
 
 function Update() {
    if( Input.Get$$anonymous$$eyUp( $$anonymous$$eyCode.I ) ) {
       Debug.Log( "up = " + up );
       up++;
    }
 }

OnGUI

 var up = 0;
 
 function OnGUI() {
    if( Input.Get$$anonymous$$eyUp( $$anonymous$$eyCode.I ) ) {
       Debug.Log( "up = " + up );
       up++;
    }
 }

For some reasons that I do not fully understand, Input.Get$$anonymous$$eyUp() returns true multiple time in OnGUI() even though the key is press and released once. But it works perfectly fine in Update().

@robertbu just explained the reason just before I posted this comment.

avatar image benchemistry · May 01, 2014 at 07:00 AM 0
Share

you guys are missing 2 ')' in your code and if I put them anywhere in any combination the code doesn't work in unity.

:'(

avatar image
0

Answer by Chronos-L · Mar 08, 2013 at 06:18 AM

If both GUI.Button and Input.GetKey() is affecting the same GUI.Box, then they should affect the same boolean value.

 var showBox = false;  

 function Update() {
     if(Input.GetKeyUp(KeyCode.I)) {
        showBox = !showBox;
     }
 }
   
 function OnGUI() {
     if( GUI.Button (new Rect (Screen.width - 118,Screen.height - 120,110,30 ), "Inventory" ) ) {
        showBox = !showBox;
     }
 
     if( showBox ) {
        GUI.Box( Rect( 350, 120, 300, 110 ), "INVENTORY" );
     }
 }

Your question title is misleading, it makes me think about dynamic key binding to gui button during run time, which is a much more complicated subject.

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 WesterlyCarrot9 · Mar 08, 2013 at 06:23 AM 0
Share

Never$$anonymous$$d kramcomposer gave me the right answer! :D This is the right script that works:

var charPressed = false; var pushkey = false; function OnGUI() { if( GUI.Button (new Rect (Screen.width - 118,Screen.height - 120,110,30 ), "Inventory" ) ) { charPressed = !charPressed; } if( charPressed ) { GUI.Box( Rect( 350, 120, 300, 110 ), "INVENTORY" ); } if(pushkey == true){ GUI.Box( Rect( 350, 120, 300, 110 ), "INVENTORY" );
} }
function Update() { if(Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.I)) { charPressed = !charPressed; } }

avatar image Chronos-L · Mar 08, 2013 at 06:33 AM 0
Share

@westerlycarrot9, if @kramcomposer's answer is correct, please mark it so.

avatar image
0

Answer by XxPlasma_TurtlexX · Aug 10, 2021 at 10:21 AM

i made a mod awhile back still have the mod and im trying to make my gui.buttons to work off keybinds but its being a pain my mod uses visual studio please help,how do you make the gui.button close and open with a keybind not the box

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

14 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

Related Questions

Key for GUI.Button 2 Answers

GUI Button Disappearing 1 Answer

How to get the key from the button name 1 Answer

Javascript GUI.Button help, Error BCE0077 1 Answer

I Need help assigning animation clips to GUI buttons 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