# Grab mouse using OpenGL

**URL:** <https://discourse.processing.org/t/grab-mouse-using-opengl/28259>\
**Category:** Coding Questions\
**Created:** [March 4, 2021, 1:32pm UTC](https://discourse.processing.org/t/grab-mouse-using-opengl/28259 "2021-03-04T13:32:44Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![yeppii](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/yeppii/32/12180_2.png) [@yeppii](https://discourse.processing.org/u/yeppii)\
**Post date:** [March 4, 2021, 1:32pm UTC](https://discourse.processing.org/t/grab-mouse-using-opengl/28259/1 "2021-03-04T13:32:44Z")

</div>

I am making a game and I want to grab the mouse using OpenGL. How could I do this?

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [March 4, 2021, 2:42pm UTC](https://discourse.processing.org/t/grab-mouse-using-opengl/28259/2 "2021-03-04T14:42:17Z")

</div>

Please explain what you mean by grab

Essentially look at screenX in the reference

---

<div class="post-metadata">

**Author:** ![yeppii](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/yeppii/32/12180_2.png) [@yeppii](https://discourse.processing.org/u/yeppii)\
**Post date:** [March 4, 2021, 2:46pm UTC](https://discourse.processing.org/t/grab-mouse-using-opengl/28259/3 "2021-03-04T14:46:29Z")

</div>

in LWJGL 2, there is a function in the `Mouse` class named `setGrabbed()`. I want similar functionality like that.

---

<div class="post-metadata">

**Author:** ![yeppii](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/yeppii/32/12180_2.png) [@yeppii](https://discourse.processing.org/u/yeppii)\
**Post date:** [March 4, 2021, 2:48pm UTC](https://discourse.processing.org/t/grab-mouse-using-opengl/28259/4 "2021-03-04T14:48:10Z")

</div>

I actually meant “Hide and constrain mouse cursor inside the window”

---

<div class="post-metadata">

**Author:** ![yeppii](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/yeppii/32/12180_2.png) [@yeppii](https://discourse.processing.org/u/yeppii)\
**Post date:** [March 4, 2021, 2:50pm UTC](https://discourse.processing.org/t/grab-mouse-using-opengl/28259/5 "2021-03-04T14:50:24Z")

</div>

reference for `setGrabbed()` function in LWJGL 2: [Mouse (LWJGL API)](http://legacy.lwjgl.org/javadoc/org/lwjgl/input/Mouse.html#setGrabbed(boolean))

---

<div class="post-metadata">

**Author:** ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)\
**Post date:** [March 4, 2021, 3:41pm UTC](https://discourse.processing.org/t/grab-mouse-using-opengl/28259/6 "2021-03-04T15:41:46Z")

</div>

See the link at [Is there mouse locking in processing? - #4 by neilcsmith](https://discourse.processing.org/t/is-there-mouse-locking-in-processing/1633/4) and if you’re using OpenGL, make sure to use the JOGL version near the bottom of the post.

---

<div class="post-metadata">

**Author:** ![yeppii](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/yeppii/32/12180_2.png) [@yeppii](https://discourse.processing.org/u/yeppii)\
**Post date:** [March 4, 2021, 3:47pm UTC](https://discourse.processing.org/t/grab-mouse-using-opengl/28259/7 "2021-03-04T15:47:26Z")

</div>

I am using P3D mode.

---

<div class="post-metadata">

**Author:** ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)\
**Post date:** [March 4, 2021, 3:52pm UTC](https://discourse.processing.org/t/grab-mouse-using-opengl/28259/8 "2021-03-04T15:52:23Z")

</div>

Yes, it wasn’t meant as a question! 😆 Just ignore the bit about `Robot` - it’s better to do this directly with the JOGL classes.

---

<div class="post-metadata">

**Author:** ![yeppii](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/yeppii/32/12180_2.png) [@yeppii](https://discourse.processing.org/u/yeppii)\
**Post date:** [March 4, 2021, 3:53pm UTC](https://discourse.processing.org/t/grab-mouse-using-opengl/28259/9 "2021-03-04T15:53:46Z")

</div>

it didn’t work ☹

---

<div class="post-metadata">

**Author:** ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)\
**Post date:** [March 4, 2021, 5:17pm UTC](https://discourse.processing.org/t/grab-mouse-using-opengl/28259/10 "2021-03-04T17:17:53Z")

</div>

This works for me on Ubuntu -

```auto
import com.jogamp.newt.opengl.GLWindow;

void setup() {
 size(640,480,P3D);
 background(0);
 GLWindow window = (GLWindow)surface.getNative();
 window.confinePointer(true);
 window.setPointerVisible(false);
}

void draw() {
 stroke(255);
 line(pmouseX, pmouseY, mouseX, mouseY);
}

```

Added `draw()` or pressing `ESCAPE` to close the window doesn’t work, and it’s useful to see where the mouse is.

---

<div class="post-metadata">

**Author:** ![yeppii](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/yeppii/32/12180_2.png) [@yeppii](https://discourse.processing.org/u/yeppii)\
**Post date:** [March 4, 2021, 5:21pm UTC](https://discourse.processing.org/t/grab-mouse-using-opengl/28259/11 "2021-03-04T17:21:51Z")

</div>

Ok! I don’t need it! somehow i managed to edit queasycam code to make cursor trap inside the window! it works fine now! now how do i close this question?

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [March 4, 2021, 5:29pm UTC](https://discourse.processing.org/t/grab-mouse-using-opengl/28259/12 "2021-03-04T17:29:49Z")

</div>

No need to _close_ the question - just leave it here.
