---
title: "Change texture on key down"  
description: "Change texture on key down"  
author: "Anonymous User"  
published: 2013-12-12  
updated: 2013-12-12  
canonical: https://www.mindstick.com/forum/1762/change-texture-on-key-down  
category: "c#"  
tags: ["c#"]  
reading_time: 1 minute  

---

# Change texture on key down

```
private void CheckKeyboardAndUpdateMovement(){    KeyboardState keyboardState = Keyboard.GetState();    if (keyboardState.IsKeyUp(Keys.Left)) { ChangeTexture(1); }    if (keyboardState.IsKeyUp(Keys.Right)) { ChangeTexture(2); }    if (keyboardState.IsKeyDown(Keys.Left))    {        Movement -= Vector2.UnitX;        ChangeTexture(3);    }    if (keyboardState.IsKeyDown(Keys.Right))    {        Movement += Vector2.UnitX;        ChangeTexture(4);    }    if ((keyboardState.IsKeyDown(Keys.Space) || keyboardState.IsKeyDown(Keys.Up)) && IsOnFirmGround())    {        Movement = -Vector2.UnitY * JumpHeight;    }}
```

Here it is my [method](https://www.mindstick.com/forum/166/webservice-method) to get [keyboard](https://www.mindstick.com/forum/23117/android-soft-keyboard-close-hide) state and change [texture](https://answers.mindstick.com/qa/116534/navigator-pear-a-perfect-blend-of-taste-texture) based on which key is pressed.

it works if [direction](https://yourviews.mindstick.com/story/5034/10-animals-with-the-best-sense-of-direction) are pressed, but doesn't make its own [job](https://www.mindstick.com/articles/167406/how-to-upgrade-your-freelance-designer-job) when nothing is pressed (just because both the IsKeyUp are [true](https://yourviews.mindstick.com/view/81326/boycott-chinese-products-dream-will-come-true)). Only the cases' [order](https://www.mindstick.com/articles/12276/how-timely-order-deliveries-can-improve-customer-experience) prevents the [static](https://www.mindstick.com/articles/12098/the-static-keyword-the-static-methods) texture to be shown while moving the sprite... My [question](https://www.mindstick.com/blog/23175/how-to-solve-neet-question-paper-in-less-time) is, how can I make a clean solution of this [problem](https://yourviews.mindstick.com/view/81399/tackling-the-problem-of-unemployment-during-corona-pandemic)?

## Replies

### Reply by Pravesh Singh

Hi Ankit,\
You can try this :\

```
enum Direction { Left = 1, Right = 2}Direction dir = Direction.Left; //or whatever private void CheckKeyboardAndUpdateMovement(){    KeyboardState
keyboardState = Keyboard.GetState();   
ChangeTexture((int)dir);    if
(keyboardState.IsKeyDown(Keys.Left))    {        Movement -=
Vector2.UnitX;       
ChangeTexture(3);        dir =
Direction.Left;    }    if
(keyboardState.IsKeyDown(Keys.Right))    {        Movement +=
Vector2.UnitX;       
ChangeTexture(4);        dir =
Direction.Right;    }    if
((keyboardState.IsKeyDown(Keys.Space) || keyboardState.IsKeyDown(Keys.Up))
&& IsOnFirmGround())    {        Movement =
-Vector2.UnitY * JumpHeight;    }}
```


---

Original Source: https://www.mindstick.com/forum/1762/change-texture-on-key-down

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
