Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Press key when hovering over a button to activate?

Asked by 4 years ago

Basically what i want to do is when my mouse is hovering over a TextButton i can press a key and it'll detect it. How would i go about doing this? Heres the code i wrote but it doesn't work:

button.MouseMoved:Connect(function()
    mouse.KeyDown:Connect(function(input)
        print(input)
        if input.KeyCode==Enum.KeyCode['F'] and button.favorite.Visible==false then
            button.favorite.Visible=true
        else
            button.favorite.Visible=false
        end
    end)
end)
0
KeyDown is deprecated, please practice UserInputService. Ziffixture 6913 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

So event listeners like Hover and KeyDown activate no matter where they are when you hit the key. So you need to put a debounce. Which is a fancy way of saying make a bool variable = true when you are hovering and only run the code inside KeyDown when it’s true. Also I believe there is a mouseHover event you can use instead of MouseMoved. Not sure but you can look into that.

local variableName = false
button.MouseMoved:Connect(function()
    variableName = true
    mouse.KeyDown:Connect(function(input)
        if variableName then 
        print(input)
             if input.KeyCode==Enum.KeyCode['F'] and button.favorite.Visible==false then
                 button.favorite.Visible=true
             else
                 button.favorite.Visible=false
              end
              variableName = false
        end 
    end)
end)

0
Thank you! outcasino 0 — 4y
0
Actually the KeyDown code runs hundreds of times and also when i stop hovering over the item. Is there anyway to fix this? outcasino 0 — 4y
0
Nvm i forgot to set the variable back to false outcasino 0 — 4y
0
Does it work? If it does re accept answer lol. If it doesn’t I can look at it again. SethHeinzman 284 — 4y
0
Also use User input service instead of KeyDown like the guy said. I won’t post the code since I’m hanging out with my family but it should be a simple fix. SethHeinzman 284 — 4y
Ad

Answer this question