Author Topic: How do I have a key press cause a button click?  (Read 12146 times)

makeshiftwings

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 25
    • View Profile
How do I have a key press cause a button click?
« on: July 25, 2012, 04:52:12 PM »
I've got a toolbar of buttons that are clickable, and I'd like to have them also respond to shortcut keys.  Currently I added a script onto each button that checks for their corresponding key press, and if so, calls SendMessage("OnClick").  This mostly works, in that the button's click sound goes off and any custom OnClick methods get called, but it doesn't call OnPress true and OnPress false the way it would with an actual mouse click, so the button doesn't animate.  Is there a way to automatically simulate a full click on a button, or should I manually call OnPress(true) and OnClick and then start a CoRoutine to call OnPress(false) a second later?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How do I have a key press cause a button click?
« Reply #1 on: July 25, 2012, 07:54:13 PM »
  1. using UnityEngine;
  2.  
  3. [AddComponentMenu("Game/UI/Button Key")]
  4. public class UIButtonKey : MonoBehaviour
  5. {
  6.         public KeyCode keyCode = KeyCode.None;
  7.  
  8.         void Update ()
  9.         {
  10.                 if (!UICamera.inputHasFocus)
  11.                 {
  12.                         if (Input.GetKeyDown(keyCode))
  13.                         {
  14.                                 SendMessage("OnPress", true, SendMessageOptions.DontRequireReceiver);
  15.                         }
  16.  
  17.                         if (Input.GetKeyUp(keyCode))
  18.                         {
  19.                                 SendMessage("OnPress", false, SendMessageOptions.DontRequireReceiver);
  20.                                 SendMessage("OnClick", SendMessageOptions.DontRequireReceiver);
  21.                         }
  22.                 }
  23.         }
  24. }

optimisticmonkey

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 2
  • Posts: 15
    • View Profile
Re: How do I have a key press cause a button click?
« Reply #2 on: February 04, 2014, 01:34:02 AM »
This works great, but I get:
NullReferenceException: Object reference not set to an instance of an object
UIButtonColor.OnPress (Boolean isPressed) (at Assets/NGUI/Scripts/Interaction/UIButtonColor.cs:158)
UIButton.OnPress (Boolean isPressed) (at Assets/NGUI/Scripts/Interaction/UIButton.cs:90)
UnityEngine.Component:SendMessage(String, Object, SendMessageOptions)
RightArrowButton:Update() (at Assets/MyScripts/RightArrowButton.cs:36)

On the KeyUp because the currentTouch.current is not set in:

protected virtual void OnPress (bool isPressed)
   {
      if (enabled)
      {
         if (!mStarted) Start();
         
         if (isPressed)
         {
            TweenColor.Begin(tweenTarget, duration, pressed);
         }
         else if (UICamera.currentTouch.current == gameObject && UICamera.currentScheme == UICamera.ControlScheme.Controller)
         {
            TweenColor.Begin(tweenTarget, duration, hover);
         }
         else TweenColor.Begin(tweenTarget, duration, mColor);
      }
   }

What is best way to fix?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How do I have a key press cause a button click?
« Reply #3 on: February 04, 2014, 03:02:05 AM »
This is an old script.

Use UIKeyBinding. It comes with NGUI.