# Get (raw?) mouse input

**URL:** <https://forum.castle-engine.io/t/get-raw-mouse-input/904>\
**Category:** Uncategorized\
**Created:** [September 21, 2023, 11:59am UTC](https://forum.castle-engine.io/t/get-raw-mouse-input/904 "2023-09-21T11:59:26Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![NiunioMartinez](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/niuniomartinez/32/2232_2.png) [@NiunioMartinez](https://forum.castle-engine.io/u/NiunioMartinez)\
**Post date:** [September 21, 2023, 11:59am UTC](https://forum.castle-engine.io/t/get-raw-mouse-input/904/1 "2023-09-21T11:59:26Z")

</div>

I want to get mouse input from behavior `Update` method so I can use it as user input. The only way I’ve found is to use `TCastleView`, `Motion`, `Press` and `Release` methods to retrieve the input and “send” it somehow to the behavior object (I didn’t test this actually, I’ll try it just after I post this).

Is there a better way to get mouse input?

Also how to hide the mouse cursor and prevent the cursor to exit the window?

---

<div class="post-metadata">

**Author:** ![eugeneloza](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/eugeneloza/32/12_2.png) [@eugeneloza](https://forum.castle-engine.io/u/eugeneloza)\
**Post date:** [September 21, 2023, 1:56pm UTC](https://forum.castle-engine.io/t/get-raw-mouse-input/904/2 "2023-09-21T13:56:06Z")

</div>

In every `TCastleView` you have a `Container.MousePosition` - this is the raw mouse position on Screen. However, looking at implementation of `SetMousePousition` setter it doesn’t actually set the mouse position. If you want to set mouse position to something specific (e.g. to implement your own version of mouse look behavior) you can try [Castle Game Engine: CastleWindow: Class TCastleWindow](https://castle-engine.io/apidoc/html/CastleWindow.TCastleWindow.html#MousePosition) — I was doing it through this, but maybe currently there is something more generic exposed.

Note that “prevent cursor from exiting window” is not very straightforward. As soon as cursor leaves the window we lose track of it. I know there is a way to track mouse cursor absolute coordinates - however I don’t remember if the specific call is available in Castle Engine.

Setting cursor invisible (or to specific image) can be done through `TCastleUiControl.Cursor` [Castle Game Engine: CastleUIControls: Class TCastleUserInterface](https://castle-engine.io/apidoc/html/CastleUIControls.TCastleUserInterface.html#Cursor) - you can set it to `mcNone` or, if some control tries to override the cursor look - `mcForceNone` to make sure it is always invisible. Note that `TCastleView` inherits from `TCastleUiControl` and as such has `Cursor` property.

---

<div class="post-metadata">

**Author:** ![NiunioMartinez](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/niuniomartinez/32/2232_2.png) [@NiunioMartinez](https://forum.castle-engine.io/u/NiunioMartinez)\
**Post date:** [September 22, 2023, 8:44am UTC](https://forum.castle-engine.io/t/get-raw-mouse-input/904/3 "2023-09-22T08:44:22Z")

</div>

So there’s no simple way. I was trying to build an FPS controller as a _Behavior_ instead of as a _Navigator_ but I’m thinking it is not possible. (Tempted to write a rant about why _Behavior_ is better than _Navigator_ IMO 🙄 I’ll not yet but…) I think input (keyboard, mouse, gamepad, joystick…) should be more exposed, maybe as _Components_. Or an approach similar to Allegro ([al\_get\_keyboard\_state](https://allegro-pas.sourceforge.net/docs/5.2/Allegro5.html#al_get_keyboard_state) [al\_get\_mouse\_state](https://allegro-pas.sourceforge.net/docs/5.2/Allegro5.html#al_get_mouse_state) [al\_get\_joystick\_state](https://allegro-pas.sourceforge.net/docs/5.2/Allegro5.html#al_get_joystick_state)) or Unity ([Input](https://docs.unity3d.com/ScriptReference/Input.html)). Maybe it can’t be done because it might need deep changes in the engine core. 🙁 A bunch of calls or properties to hide the cursor and _capture_ the mouse easily are quite useful too.

I’ll try to build a _Navigator_ that moves a _Transform_ instead of the _Camera_. But I still think that a _Behavior_ is a better approach.

---

<div class="post-metadata">

**Author:** ![eugeneloza](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/eugeneloza/32/12_2.png) [@eugeneloza](https://forum.castle-engine.io/u/eugeneloza)\
**Post date:** [September 22, 2023, 9:08am UTC](https://forum.castle-engine.io/t/get-raw-mouse-input/904/4 "2023-09-22T09:08:19Z")

</div>

> [@NiunioMartinez](#):
>
> I was trying to build an FPS controller as a _Behavior_ instead of as a _Navigator_ but I’m thinking it is not possible.

Actually this is very possible. Just instead of checking “if the mouse is outside of the window” one needs to check if the mouse is not in the center and unless it isn’t adjust camera rotation accordingly and move the mouse back to center (this is how Navigator’s MouseLook is implemented). The only drawback here is that if player moves the mouse VERY quickly it can jump outside of the window and hence loose input, but it’s almost impossible even while actually trying to do so 🙂

What is something I’m not aware of (it doesn’t mean it’s not implemented) - is how to keep mouse cursor within the window borders, not just window center (where there is a big margin to the borders). Just yesterday checked and Unity seems simply give some “safety margin” for this cause - if the cursor is like 100 pixels away from border - teleport it to the opposite border : this is absolutely doable with CGE 🙂

EDIT: E.g. I was doing my own mouselook years ago [src/input/decomouse.pas · master · EugeneLoza / Decoherence · GitLab](https://gitlab.com/EugeneLoza/decoherence/-/blob/master/src/input/decomouse.pas?ref_type=heads#L265)

---

<div class="post-metadata">

**Author:** ![michalis](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/michalis/32/3_2.png) [@michalis](https://forum.castle-engine.io/u/michalis)\
**Post date:** [September 22, 2023, 9:07pm UTC](https://forum.castle-engine.io/t/get-raw-mouse-input/904/5 "2023-09-22T21:07:58Z")

</div>

> [@NiunioMartinez](#):
>
> I want to get mouse input from behavior `Update` method so I can use it as user input. The only way I’ve found is to use `TCastleView`, `Motion`, `Press` and `Release` methods to retrieve the input and “send” it somehow to the behavior object (I didn’t test this actually, I’ll try it just after I post this).

It seems what you want is something like `TCastleAxis` which we already worked on with Andrzej Kilijański in [New physics based first/third person 3d and platformer like modular navigation + input axis and others by and3md · Pull Request #533 · castle-engine/castle-engine · GitHub](https://github.com/castle-engine/castle-engine/pull/533) . The idea is to define an axis that you can easily query in e.g. any Update (like from a behavior). I think this exactly addresses your use-case 🙂

I want to extend the `TCastleAxis` to have various alternative bindings (so assign e.g. multiple alternative keys). The idea is to achieve something similar to Godot or Unity inputs.

So… go ahead and test the work in PR [New physics based first/third person 3d and platformer like modular navigation + input axis and others by and3md · Pull Request #533 · castle-engine/castle-engine · GitHub](https://github.com/castle-engine/castle-engine/pull/533/files) 🙂 If you don’t want to wait, you should be able to get the `src/behaviors/input/castleinputaxis.pas` unit from that PR and just use it in your projects – before we merge this PR.

And let me know. We are looking for feedback to the whole [New physics based first/third person 3d and platformer like modular navigation + input axis and others by and3md · Pull Request #533 · castle-engine/castle-engine · GitHub](https://github.com/castle-engine/castle-engine/pull/533/) work, it’s a big PR, it will take some time to review, test, and maybe finalize some API. So all feedback is welcome 🙂

> [@NiunioMartinez](#):
>
> Also how to hide the mouse cursor and prevent the cursor to exit the window?

We have an API to essentially lock the mouse to the window, allowing user to not care about the mouse position, and perform infinite mouse movement. It the practical usage, this means that mouse is hidden.

See the example `examples/user_interface/dragging_test` on [castle-engine/examples/user\_interface/dragging\_test at master · castle-engine/castle-engine · GitHub](https://github.com/castle-engine/castle-engine/tree/master/examples/user_interface/dragging_test) – see there the README and code. It is also automatically used by current TCastleWalkNavigation.MouseLook .

These 2 answers “connect” of course, you can see in PR [New physics based first/third person 3d and platformer like modular navigation + input axis and others by and3md · Pull Request #533 · castle-engine/castle-engine · GitHub](https://github.com/castle-engine/castle-engine/pull/533) how to have mouse look + TCastleAxis.

> [@NiunioMartinez](#):
>
> So there’s no simple way. I was trying to build an FPS controller as a _Behavior_ instead of as a _Navigator_ but I’m thinking it is not possible. (Tempted to write a rant about why _Behavior_ is better than _Navigator_ IMO 🙄 I’ll not yet but…) I think input (keyboard, mouse, gamepad, joystick…) should be more exposed, maybe as _Components_. Or an approach similar to Allegro ([al\_get\_keyboard\_state](https://allegro-pas.sourceforge.net/docs/5.2/Allegro5.html#al_get_keyboard_state) [al\_get\_mouse\_state](https://allegro-pas.sourceforge.net/docs/5.2/Allegro5.html#al_get_mouse_state) [al\_get\_joystick\_state](https://allegro-pas.sourceforge.net/docs/5.2/Allegro5.html#al_get_joystick_state)) or Unity ([Input](https://docs.unity3d.com/ScriptReference/Input.html)). Maybe it can’t be done because it might need deep changes in the engine core. 🙁 A bunch of calls or properties to hide the cursor and _capture_ the mouse easily are quite useful too.
> 
> I’ll try to build a _Navigator_ that moves a _Transform_ instead of the _Camera_. But I still think that a _Behavior_ is a better approach.

No need to write a rant, as it seems we actually think the same 🙂 and the PR [New physics based first/third person 3d and platformer like modular navigation + input axis and others by and3md · Pull Request #533 · castle-engine/castle-engine · GitHub](https://github.com/castle-engine/castle-engine/pull/533/) should make you happy – this is actually exactly addressing this thing. I.e.

- it uses behaviors to perform navigation
- these behaviors move TCastleTransform, in theory any transform. It can be a visible player (like in 2D platformer games or 3D avatar) or a transform with camera (for FPS games)
- and yes, it is somewhat analogous to Unity input system usage.

We’ve been designing this with Andrzej Kilijański for quite some time, and we reached the same conclusions as you above 🙂

---

<div class="post-metadata">

**Author:** ![NiunioMartinez](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/niuniomartinez/32/2232_2.png) [@NiunioMartinez](https://forum.castle-engine.io/u/NiunioMartinez)\
**Post date:** [September 29, 2023, 9:12am UTC](https://forum.castle-engine.io/t/get-raw-mouse-input/904/6 "2023-09-29T09:12:17Z")

</div>

> [@michalis](#):
>
> So… go ahead and test the work in PR [New physics based first/third person 3d and platformer like modular navigation + input axis and others by and3md · Pull Request #533 · castle-engine/castle-engine · GitHub](https://github.com/castle-engine/castle-engine/pull/533/files) 🙂 If you don’t want to wait, you should be able to get the `src/behaviors/input/castleinputaxis.pas` unit from that PR and just use it in your projects – before we merge this PR.

I’ll test it and tell you.

I’m not a GIT user but I’ll do my best.

---

<div class="post-metadata">

**Author:** ![NiunioMartinez](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/niuniomartinez/32/2232_2.png) [@NiunioMartinez](https://forum.castle-engine.io/u/NiunioMartinez)\
**Post date:** [September 29, 2023, 9:51am UTC](https://forum.castle-engine.io/t/get-raw-mouse-input/904/7 "2023-09-29T09:51:45Z")

</div>

I see I have to download the complete PR, not just the `CastleInputAxis` unit. I suppose I have to _merge_ it with `trunk` and compile the engine and editors, haven’t I? Never did it. I suppose I just use `make` in the root directory.

Also have I to use `gh` to get the PR? As I said I’m not a GIT user (despite I have a GitHub user :p).

Sorry for the questions.

---

<div class="post-metadata">

**Author:** ![michalis](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/michalis/32/3_2.png) [@michalis](https://forum.castle-engine.io/u/michalis)\
**Post date:** [September 29, 2023, 12:31pm UTC](https://forum.castle-engine.io/t/get-raw-mouse-input/904/8 "2023-09-29T12:31:44Z")

</div>

You do not need to merge with master. You only have to download the code from given PR:

- In general to get the complete PR code, see the top of the PR [New physics based first/third person 3d and platformer like modular navigation + input axis and others by and3md · Pull Request #533 · castle-engine/castle-engine · GitHub](https://github.com/castle-engine/castle-engine/pull/533/) where it says from which repository and branch it comes.

- Actually, a simpler advise: use GitHub Desktop [https://desktop.github.com/](https://desktop.github.com/) that has ready UI to switch to a PR, see the screenshot:

- Or use VS Code ( [Visual Studio Code | Manual | Castle Game Engine](https://castle-engine.io/vscode) ) that has a GitHub extension, that also allows to easily checkout a given PR by just clicking.

Remember afterwards to build the new engine – see [Compiling from source | Manual | Castle Game Engine](https://castle-engine.io/compiling_from_source.php) .

---

<div class="post-metadata">

**Author:** ![NiunioMartinez](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/niuniomartinez/32/2232_2.png) [@NiunioMartinez](https://forum.castle-engine.io/u/NiunioMartinez)\
**Post date:** [October 3, 2023, 8:49am UTC](https://forum.castle-engine.io/t/get-raw-mouse-input/904/9 "2023-10-03T08:49:06Z")

</div>

Seems that `gh` makes things quite easy. Maybe I should learn more about it and then stop procrastinating the GitHub’s Allegro.pas mirror updates.

Anyway, after getting the PR and following [Compiling from source](https://castle-engine.io/compiling_from_source.php#section_Get_the_CGE_source_code_from_GitHub) instructions I found an error when building the `castle_editor_components` package:

> castleeditorpropedits\_physics\_layer.inc(239,15) Error: Identifier not found “AllLayers”

Before that I build `castle-engine` and installed the `castle_components` package.

---

<div class="post-metadata">

**Author:** ![michalis](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/michalis/32/3_2.png) [@michalis](https://forum.castle-engine.io/u/michalis)\
**Post date:** [October 9, 2023, 2:04pm UTC](https://forum.castle-engine.io/t/get-raw-mouse-input/904/10 "2023-10-09T14:04:34Z")

</div>

> [@NiunioMartinez](#):
>
> Anyway, after getting the PR and following [Compiling from source](https://castle-engine.io/compiling_from_source.php#section_Get_the_CGE_source_code_from_GitHub) instructions I found an error when building the `castle_editor_components` package:
> 
> > castleeditorpropedits\_physics\_layer.inc(239,15) Error: Identifier not found “AllLayers”

I tested compiling `castle_editor_components` from the branch in PR [New physics based first/third person 3d and platformer like modular navigation + input axis and others by and3md · Pull Request #533 · castle-engine/castle-engine · GitHub](https://github.com/castle-engine/castle-engine/pull/533/) and it goes OK for me.

Moreover, the error is a bit weird – the `AllLayers` identifier is in `CastleTransform` unit and, from what I see, that PR didn’t change it actually, it was and is in the same unit in the same file, before and after this PR.

Are you sure the Lazarus compilation doesn’t pick some old Castle Game Engine sources on your disk?

Can you post the full compilation log – it should contain the file paths, and should allow to see whether it uses correct locations for all .pas units. You can get the log e.g. easily by doing it from command-line :

```auto
lazbuild packages/castle_base.lpk > log.txt
lazbuild packages/castle_components.lpk >> log.txt
lazbuild packages/castle_editor_components.lpk >> log.txt

```

(assuming your `lazbuild` executable is on PATH).

---

<div class="post-metadata">

**Author:** ![NiunioMartinez](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/niuniomartinez/32/2232_2.png) [@NiunioMartinez](https://forum.castle-engine.io/u/NiunioMartinez)\
**Post date:** [October 15, 2023, 10:37am UTC](https://forum.castle-engine.io/t/get-raw-mouse-input/904/11 "2023-10-15T10:37:05Z")

</div>

> [@michalis](#):
>
> Are you sure the Lazarus compilation doesn’t pick some old Castle Game Engine sources on your disk?

No, I’m not sure. I have two CGE installed: the one I downloaded from [Download | Castle Game Engine](https://castle-engine.io/download) and the PR I got from GitHub.

> [@michalis](#):
>
> Can you post the full compilation log – it should contain the file paths, and should allow to see whether it uses correct locations for all .pas units. You can get the log e.g. easily by doing it from command-line :

From which directory, the one I downloaded or the PR?

Note: As you see I have no much free time so I can do this only once per week, twice if lucky.

---

<div class="post-metadata">

**Author:** ![michalis](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/michalis/32/3_2.png) [@michalis](https://forum.castle-engine.io/u/michalis)\
**Post date:** [October 16, 2023, 11:49am UTC](https://forum.castle-engine.io/t/get-raw-mouse-input/904/12 "2023-10-16T11:49:38Z")

</div>

> [@NiunioMartinez](#):
>
> From which directory, the one I downloaded or the PR?

Post the log from the build that you want to do, but that fails. So, from the one from PR.

I see you have 2 engines on disk. It is simplest (to resolve the problems) to just “move one aside” to make sure it is not picked up. E.g. just rename the directory of the engine (that temporarily should not be used) to something like `castle-game-engine-not-to-be-used-for-now`.

Once you have built and tested the engine from PR, you can always go back to the “original” engine by just renaming it back.

---

<div class="post-metadata">

**Author:** ![NiunioMartinez](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/niuniomartinez/32/2232_2.png) [@NiunioMartinez](https://forum.castle-engine.io/u/NiunioMartinez)\
**Post date:** [May 27, 2024, 9:17am UTC](https://forum.castle-engine.io/t/get-raw-mouse-input/904/13 "2024-05-27T09:17:51Z")

</div>

Long time, sorry. My hard drive died and then the same by the power supply. Also I have job and personal issues (including a bike accident). It’s a hard life.

I don’t know if I’ll follow with this thing. I was playing with tutorials and such and doing more experiments, maybe later.
