- Home /
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" );
}
}
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()
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" );
}
}
@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.
you guys are missing 2 ')' in your code and if I put them anywhere in any combination the code doesn't work in unity.
:'(
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.
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; } }
@westerlycarrot9, if @kramcomposer's answer is correct, please mark it so.
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
Your answer
Follow this Question
Related Questions
Key for GUI.Button 2 Answers
Setting Scroll View Width GUILayout 1 Answer
Rigidbody Gravity scripting question (Javascript Answers Only!) 3 Answers
How can I attach a GUI.Button to a variable? 2 Answers
DrawTexture GUI iPhone 1 Answer