Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 LANDO · Jan 27, 2011 at 10:24 PM · guicharactercontrollerkeyboardgui-buttonkeycode

Assign keystrokes to GUI button

Is there a way to assign a keystroke to a GUI button?

I'm attempting to control my game character through MouseEvents on GUI buttons and have been running into problems, which could all be solved if I could link up keyboard hits (wasd and arrows) to the GUI.

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
6
Best Answer

Answer by Bunny83 · Jan 27, 2011 at 11:40 PM

You shouldn't use the Input class inside OnGUI because that can break the GUI system and can bring up errors. Inside OnGUI you should use the Event class.

Here's a little helper function (C#):

bool GUIKeyDown(KeyCode key)
{
    if (Event.current.type == EventType.KeyDown)
        return (Event.current.keyCode == key);
    return false;
}

And the same in JS (Unityscript)

function GUIKeyDown(key : KeyCode) : bool
{
    if (Event.current.type == EventType.KeyDown)
        return (Event.current.keyCode == key);
    return false;
}

Here an example how to use this function inside OnGUI():

{
   if (GUIKeyDown(KeyCode.W)) // the "W" key
   {
      [...]
   }

But be warned: it does not work like Input.GetKeyDown() function because Event actually maps the windows messages like WM_KEYDOWN. The function will also return true when a key is autorepeated by the OS. To simulate Input.GetKey() you would need to keep track of both events EventType.KeyDown and EventType.KeyUp but that would get to complicated.

If you need this functionality i recomend to but your actual code, that should be executed by GUI.Button or by a key, in a seperate function. Call that function from your OnGUI() with your GUI.Button and do the Key-check in Update().

You may also have a look at Event.KeyboardEvent

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 LANDO · Jan 28, 2011 at 10:01 PM 0
Share

I'm having some trouble adding the helper function to my JavaScript script, I do see what your suggesting and it makes sense. I'll keep playing with it to see what I can get, I really appreciate the helpful information and links. Thanks for the help!

avatar image Bunny83 · Jan 28, 2011 at 10:44 PM 0
Share

sorry, but you didn't specified your preferred language. I use C# in the first place, but i will give you the function in JS as well ;)

avatar image LANDO · Jan 28, 2011 at 10:49 PM 0
Share

Thank you very much! I have found a temporary fix, however it seems your approach will work the best. I'm using (sorry for posting code in the comment space): if(GUI.RepeatButton (Rect (870,468,43,43), dPadUp, "label") {
rover.animation.Blend ("drive", 0.3);
rover.transform.Translate(Vector3.forward);
print ("$$anonymous$$oving Forward!!");
}

avatar image LANDO · Jan 28, 2011 at 11:44 PM 0
Share

Thanks again, it's working!

avatar image
1

Answer by DaveA · Jan 27, 2011 at 10:30 PM

You can assign keys to things using the Input Manager. You can program them directly by looking for key press/hold/release in any Update or OnGUI function. As far as declaring or defining an 'accelerator' for a GUI button, I don't think so, but that would be nice. I suppose one could write a subclass to do that, and maybe even sell it cheap on the Asset Store.

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 LANDO · Jan 28, 2011 at 10:04 PM 0
Share

I've tried assigning keys through the input manager and wasn't able to find a method to assign a GUI.Button or Texture 2D. I assume it isn't possible but I'll take a second look into it. It may come down to me checking out the Asset Store, it would definitely be worth it. Thanks for your suggestions and help!

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

No one has followed this question yet.

Related Questions

GUI based character control 2 Answers

Is there a way to combine the Update and OnGUI functions? 1 Answer

Controlling Pause Menu with Keyboard 2 Answers

GUI controlled movement 0 Answers

gui button to work exctly like up arrow key 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