• 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, w$$anonymous$$ch could all be solved if I could link up keyboard $$anonymous$$ts (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 t$$anonymous$$s 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 t$$anonymous$$s 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
avatar image Bunny83 · Jan 28, 2011 at 10:44 PM 0
Share
avatar image LANDO · Jan 28, 2011 at 10:49 PM 0
Share
avatar image LANDO · Jan 28, 2011 at 11:44 PM 0
Share
avatar image
1

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

You can assign keys to t$$anonymous$$ngs 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 t$$anonymous$$nk 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

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

Making a character jump with the Character Controller Component instead of Capsule Collider 0 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