How to make sure that when clicking on the button attribute, it will not be saved

Hi everybody,

when I add a button component to a gameObject.

Then, when I run the game, I click on it (when I'm done clicking, I release the mouse), but when I use the keyboard to press inter, the property is pressed again. (Unless I have to touch a part other than the area of that button, then I can no longer press inter.)

This is annoying, so how can I prevent pressing enter from having anything to do with the button anymore?
I am using unity 2023.

Thank you very much.

I think the ENTER key driving the button is a result of the button now being selected after you played with it.

A script that senses (using EventTrigger for example) when you are done interacting with the button and then tells the EventSystem "hey this button is not selected anymore" might also suffice.

It might be sufficient to disable navigation on that button. Not sure about that... it might still retain its selected state.


Yes, my problem is like that, it seems unity has saved that button state, when pressing enter that state will be active.
If you want it to stop working, the first thing after pressing that button is to click anywhere else on the game screen (except for the button's button), then everything will return to normal and press The enter button will not do anything.
And when you finish pressing the button, without touching the screen (except for the button area), when you press the enter button on the keyboard, it will send the button again. This is really annoying for games with diverse combinations of button selection (eg inventory boxes) using the mouse or keyboard buttons such as left, right, down, up and click. enter to select (or more simply, use the mouse to select items in the inventory item).
But if you use the mouse to click on an inventory box, and use the keyboard to move to another box, when you press enter it will be the old box again.

And how did the two (2) separate things that I already suggested to you work out?

Oddly, you didn't mention trying my two separate suggestions in your response.

1 Like

The Button class uses EventSystem's interfaces to handle input event callbacks. It inherits from the Selectible class which handles the basic navigation behavior (e.g. choosing which element is to be highlighted next) via keyboard/gamepad/mouse/pointer/etc as well as how the element's appearance changed under selection states (unselected, selected, hovered, disabled).

While the Button class itself listens to 2 more EventSystem events: the Click event and the Submit event. It uses both events to allow different types on input devices to be able to interact with it. Submit event is called when the "submit button" is pressed (enter by default), and is done so players using exclusively keyboard/gamepad can press the button. The Click events fire when a PointerDown and the next PointerUp event fire on the button and its done so players using exclusively touchpad/mouse/vr can interact with the button.

So there are a few solutions.
1. unbind the "Submit" key in the input settings (set to enter by default)
2. (if using StandaloneInputModule) set Eventsystem.current.sendNavigationEvents to false to disable the move, submit, and cancel events from getting called.
3. If you want to preserve keyboard/gamepad navigation while in menu but are using that submit key as an in-game input. I suggest that you tell the eventsystem to deselect the button via Eventsystem.SetSelectedGameObject(null); while exiting that menu before the player is expected to use that key in-game. this way if players press the submit button while ingame the event system won't press that button from the menu since you've deselected it.
4. If you are only using the EventSystem for menu navigation you can just disable the Eventsystem itself while the menu is closed and enable it while its open. This way the event system can preserve the ability for gamepad/keyboards to be able to navigate the menus
5.you can also just write a custom Button class that excludes submit functionality, then use that component instead for the buttons you don't want pressed via keyboard/gamepad buttons

PointerButton.cs

[spoiler]

This is literally the same code for Unity's Button class but with the submit logic (and anything explicitly internal to Unity, e.g. constructors) taken out, making it only react to mouse/touchpad/VR interactions while it ignores keyboard/gamepad input

using System;
using System.Collections;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.Serialization;

namespace MyProject.UIExtensions
{
    [AddComponentMenu("UI/Pointer Button", 31)]
    public class PointerButton: Selectable, IPointerClickHandler, IEventSystemHandler
    {
        [Serializable]
        public class ButtonClickedEvent : UnityEvent
        {
        }

        [FormerlySerializedAs("onClick")]
        [SerializeField]
        private ButtonClickedEvent m_OnClick = new ButtonClickedEvent();

        public ButtonClickedEvent onClick { get { m_OnClick;}  set { m_OnClick = value; } }

        private void Press()
        {
            if (IsActive() && IsInteractable())
            {
                UISystemProfilerApi.AddMarker("PointerButton.onClick", this);
                m_OnClick.Invoke();
            }
        }

        public virtual void OnPointerClick(PointerEventData eventData)
        {
            if (eventData.button == PointerEventData.InputButton.Left)
            {
                Press();
            }
        }
    }
}

[/spoiler]

1 Like

Thanks for all the replies. My problem has been resolved.

EventSystem.current.sendNavigationEvents = false;

have a nice day.

9636479--1369679--upload_2024-2-11_7-32-45.png