<p>I have code like this to move the player in my game left, right, up, and down:</p>

<pre><code>keysSetRepeat(20, 5);

while (lives) {
    scanKeys();

    if (keysDownRepeat() &amp; (KEY_LEFT | KEY_RIGHT | KEY_UP | KEY_DOWN)) {
        u8 new_x = x;
        u8 new_y = y;

        if (keysDownRepeat() &amp; KEY_LEFT) {
            new_x--;
        } else if (keysDownRepeat() &amp; KEY_RIGHT) {
            new_x++;
        } else if (keysDownRepeat() &amp; KEY_DOWN) {
            new_y++;
        } else if (keysDownRepeat() &amp; KEY_UP) {
            new_y--;
        }

        // ...
    }

    // ...

    swiWaitForVBlank();
}
</code></pre>

<p>Why are the keys not being detected? If I replace <code>keysDownRepeat()</code> with <code>keysDown()</code> it works (without the repeat rate, of course). The <a href="http://libnds.devkitpro.org/a00020.html" rel="nofollow noreferrer">documentation</a> is no help here.</p>

I had to find the libnds source code to figure this out. Look at the implementation of keysDownRepeat():
```
```
uint32 keysDownRepeat(void) {
    uint32 tmp = keysrepeat;

    keysrepeat = 0;

    return tmp;
}
```
```
It actually returns the keys then resets them back to 0. This wasn't documented. I solved this by storing the result of
```
keysDownRepeat()
```
into a variable and using the variable to check the keys:
```
```
keysSetRepeat(20, 5);

while (lives) {
    scanKeys();
    u32 keys_down_repeat = keysDownRepeat();

    if (keys_down_repeat & (KEY_LEFT | KEY_RIGHT | KEY_UP | KEY_DOWN)) {
        u8 new_x = x;
        u8 new_y = y;

        if (keys_down_repeat & KEY_LEFT) {
            new_x--;
        } else if (keys_down_repeat & KEY_RIGHT) {
            new_x++;
        } else if (keys_down_repeat & KEY_DOWN) {
            new_y++;
        } else if (keys_down_repeat & KEY_UP) {
            new_y--;
        }

        // ...
    }

    // ...

    swiWaitForVBlank();
}
```
```