# Rollover / button press effect via code

**URL:** https://discussions.unity.com/t/rollover-button-press-effect-via-code/798024
**Category:** UGUI
**Tags:** UGUI
**Created:** [June 29, 2020, 2:07pm UTC](https://discussions.unity.com/t/rollover-button-press-effect-via-code/798024 "2020-06-29T14:07:52Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![keystagefun](https://avatars.discourse-cdn.com/v4/letter/k/e274bd/32.png) [@keystagefun](https://discussions.unity.com/u/keystagefun)
#### Post date: [June 29, 2020, 2:07pm UTC](https://discussions.unity.com/t/rollover-button-press-effect-via-code/798024/1 "2020-06-29T14:07:52Z")

</div>

Hi there.

I’m wondering if anyone knows a way to get the standard UI Button to _appear_ as if it’s being pressed / moused over even when it isn’t?

Basically, I have created a series of buttons to allow the user to select multiple choice answers to a question and have got the buttons working for both button presses with a mouse / touchscreen as well as input via a physical keyboard or game pad.

The button uses a 9 slice background and displays different background images for three states.

What I want is for the visual part of the button pressed effect to trigger when a certain physical key is pressed on an external keyboard, in the same way it is if I press it with a mouse click or tap on a mobile device. The function I’m running onClick works fine, but the visual change seems to be part of the built-in functionality of the button and I can’t find a way to access it or any info in the docs / on these forums.

I know I can hack it and have an overlay image that is hidden until the related keyboard key is pressed, that then displays on top of the button at the same size / location, but I wondered if there was a cleaner way of doing this that just links into the button press functionality and triggers the background image change.

Thanks,

Ian

---

<div class="post-metadata">

### Author: ![keystagefun](https://avatars.discourse-cdn.com/v4/letter/k/e274bd/32.png) [@keystagefun](https://discussions.unity.com/u/keystagefun)
#### Post date: [June 29, 2020, 3:55pm UTC](https://discussions.unity.com/t/rollover-button-press-effect-via-code/798024/2 "2020-06-29T15:55:56Z")

</div>

Answered my own question… this does the trick

```csharp
// Assumes the sprite state for the button has been set up already and images assigned to the various states
// myButtonGameObject is the game object the button is attached to

Button button = myButtonGameObject.GetComponent<Button>();
SpriteState spriteState = new SpriteState();
spriteState = button.spriteState;
button.image.sprite = spriteState.pressedSprite;

```
