> For the complete documentation index, see [llms.txt](https://tk233.gitbook.io/notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://tk233.gitbook.io/notes/ml-rl/rl-frameworks/case-study-no-mercy-project/python-mouse-and-keyboard-interaction-in-game-environment.md).

# Python Mouse and Keyboard Interaction in Game Environment

## Getting mouse movements

We are interested in getting mouse movement in game.

The following script is executed for reading mouse information

```bash
import time

import win32api

while True:
    # print mouse movement
    movement = win32api.GetCursorPos()
    
    print(movement)
        
    time.sleep(0.5)

```

Differ from typical applications, games will acquire the mouse and force it to be at center of the screen. Therefore, without mouse movements, the reading will always be (960, 540) on a 1080P screen.

The following log shows first moving the mouse down, back to center, moving to right, and then back to center. +X is right, and +Y is down.

<figure><img src="/files/DivSTEiZB6w8giRLGfC4" alt=""><figcaption></figcaption></figure>

## Sending mouse movements

There exist a linear mapping between the mouse movement and the angle of rotation of the in-game viewpoint. The mapping varies based on the mouse sensitivity setting in game, but is independent of the game window resolution.

By default, the mouse sensitivity is set to 15%.

<figure><img src="/files/xVGB89lMKgfsmuEAcQwE" alt=""><figcaption></figcaption></figure>

Under this setting, the mapping is as follows:

Mouse sensitivity: 15%

Screen resolution: 1920 x 1080

Game resolution: any

<table><thead><tr><th width="123"></th><th width="133">Angle Range</th><th width="274">Mouse Movement Value Range</th><th>100% Ratio</th></tr></thead><tbody><tr><td>Yaw (X)</td><td>360</td><td>3636.X</td><td>1.515</td></tr><tr><td>Pitch (Y)</td><td>180</td><td>1800</td><td>1.500</td></tr></tbody></table>

Converting from angle to mouse movement value:

`value = angle * (X_OR_Y_RATIO / MOUSE_SENSITIVITY)`


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://tk233.gitbook.io/notes/ml-rl/rl-frameworks/case-study-no-mercy-project/python-mouse-and-keyboard-interaction-in-game-environment.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
