# GetKey?

**URL:** <https://discussions.unity.com/t/getkey/659773>\
**Category:** Physics\
**Tags:** Scripting\
**Created:** [March 28, 2017, 9:11pm UTC](https://discussions.unity.com/t/getkey/659773 "2017-03-28T21:11:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![milo55545](https://avatars.discourse-cdn.com/v4/letter/m/b782af/32.png) [@milo55545](https://discussions.unity.com/u/milo55545)\
**Post date:** [March 28, 2017, 9:11pm UTC](https://discussions.unity.com/t/getkey/659773/1 "2017-03-28T21:11:32Z")

</div>

This code is supposed to add a force to the player when the W key is pressed.  
Here is the code:

```csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MovementControl : MonoBehaviour {

    bool ForwardDown = Input.GetKeyDown(KeyCode.W);
    bool ForwardUp = Input.GetKeyUp(KeyCode.W);
    bool isForward;

    public Rigidbody physics;

    // Use this for initialization
    void Start () {
        physics = GetComponent<Rigidbody>();
    }
   
    // Update is called once per frame
    void Update () {
        if (ForwardDown)
            isForward = true;
        if (ForwardUp)
            isForward = false;
        if(isForward) {
            physics.AddForce (transform.forward * 10);
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![Vedrit](https://avatars.discourse-cdn.com/v4/letter/v/9fc29f/32.png) [@Vedrit](https://discussions.unity.com/u/Vedrit)\
**Post date:** [March 28, 2017, 9:17pm UTC](https://discussions.unity.com/t/getkey/659773/2 "2017-03-28T21:17:10Z")

</div>

The thing about GetKeyDown and GetKeyUp is that it only activates for 1 frame  
If you use GetKey, it will always return true while being pressed.

```auto
if (Input.GetKey(KeyCode.W)
physics.AddForce(transform.forward * 10);

```

---

<div class="post-metadata">

**Author:** ![Kurt-Dekker](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/kurt-dekker/32/101578_2.png) [@Kurt-Dekker](https://discussions.unity.com/u/Kurt-Dekker)\
**Post date:** [March 28, 2017, 9:17pm UTC](https://discussions.unity.com/t/getkey/659773/3 "2017-03-28T21:17:39Z")

</div>

You don’t need to event-drive-check it like that, and in any case you’re only calling the Input functions once, at variable instantiation.

Instead, this will work, entirely inside of your Update() loop:

```auto
 bool GoForward = Input.GetKey(KeyCode.W);
 if (GoForward) {
    // do your go forward stuff
 } else {
    // do your I'm not going forward stuff (might be nothing?)
 }

```

A bit simpler that way… no external stuff needed.

Edit: yeah, exactly what @Vedrit said, but I like having that extra variable around to debug it when it misbehaves, and to give you a point to shim in other things like maybe a touchscreen input!

---

<div class="post-metadata">

**Author:** ![milo55545](https://avatars.discourse-cdn.com/v4/letter/m/b782af/32.png) [@milo55545](https://discussions.unity.com/u/milo55545)\
**Post date:** [March 28, 2017, 9:19pm UTC](https://discussions.unity.com/t/getkey/659773/4 "2017-03-28T21:19:56Z")

</div>

> [@Kurt-Dekker](#):
>
> You don’t need to event-drive-check it like that, and in any case you’re only calling the Input functions once, at variable instantiation.
> 
> Instead, this will work, entirely inside of your Update() loop:
> 
> ```auto
> bool GoForward = Input.GetKey(KeyCode.W);
> if (GoForward) {
> // do your go forward stuff
> } else {
> // do your I'm not going forward stuff (might be nothing?)
> }
> 
> ```
> 
> A bit simpler that way… no external stuff needed.
> 
> Edit: yeah, exactly what @Vedrit said, but I like having that extra variable around to debug it when it misbehaves, and to give you a point to shim in other things like maybe a touchscreen input!

Wow that was fast! Thank you!
